Java Code Examples for com.github.javaparser.ast.Node#getParentNode()
The following examples show how to use
com.github.javaparser.ast.Node#getParentNode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaParserUtil.java From Recaf with MIT License | 6 votes |
/** * @param code * Code wrapper. * @param pos * Position of caret. * * @return Node of supported type at position. */ private static Node getSelectedNode(SourceCode code, TwoDimensional.Position pos) { // Abort if no analyzed code to parse if (code == null) return null; // Get node at row/column Node node = code.getVerboseNodeAt(pos.getMajor() + 1, pos.getMinor()); // Go up a level until node type is supported while(node != null) { if(node instanceof Resolvable || node instanceof InitializerDeclaration) break; Optional<Node> parent = node.getParentNode(); if(!parent.isPresent()) break; node = parent.get(); } return node; }
Example 2
Source File: MethodBoundaryExtractor.java From scott with MIT License | 6 votes |
private boolean isInTheCorrectClass(MethodDeclaration methodDeclaration) { Node n = methodDeclaration; String containingClassName = ""; while (n != null) { if (n instanceof ClassOrInterfaceDeclaration) { ClassOrInterfaceDeclaration c = (ClassOrInterfaceDeclaration) n; containingClassName = c.getName() + "$" + containingClassName; } Optional<Node> no = n.getParentNode(); if (no.isEmpty()) { n = null; } else { n = no.get(); } } containingClassName = containingClassName.substring(0, containingClassName.length() - 1); return containingClassName.equals(className); }
Example 3
Source File: Sources.java From sundrio with Apache License 2.0 | 5 votes |
public String apply(Node node) { if (node instanceof NamedNode) { String name = ((NamedNode)node).getName(); Node current = node; while (!(current instanceof CompilationUnit)) { current = current.getParentNode(); } CompilationUnit compilationUnit = (CompilationUnit) current; for (ImportDeclaration importDecl : compilationUnit.getImports()) { NameExpr importExpr = importDecl.getName(); if (importExpr instanceof QualifiedNameExpr) { QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr; String className = qualifiedNameExpr.getName(); if (name.equals(className)) { return qualifiedNameExpr.getQualifier().toString(); } } else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) { String importName = importDecl.getName().getName(); return importName.substring(0, importName.length() - name.length() -1); } } try { Class.forName(JAVA_LANG + "." + name); return JAVA_LANG; } catch (ClassNotFoundException ex) { return compilationUnit.getPackage().getPackageName(); } } return null; }
Example 4
Source File: Sources.java From sundrio with Apache License 2.0 | 5 votes |
public Set<ClassRef> apply(Node node) { Set<ClassRef> imports = new LinkedHashSet<ClassRef>(); if (node instanceof NamedNode) { String name = ((NamedNode)node).getName(); Node current = node; while (!(current instanceof CompilationUnit)) { current = current.getParentNode(); } CompilationUnit compilationUnit = (CompilationUnit) current; for (ImportDeclaration importDecl : compilationUnit.getImports()) { String className = null; String packageName = null; NameExpr importExpr = importDecl.getName(); if (importExpr instanceof QualifiedNameExpr) { QualifiedNameExpr qualifiedNameExpr = (QualifiedNameExpr) importExpr; className = qualifiedNameExpr.getName(); packageName = qualifiedNameExpr.getQualifier().toString(); } else if (importDecl.getName().getName().endsWith(SEPARATOR + name)) { String importName = importDecl.getName().getName(); packageName = importName.substring(0, importName.length() - name.length() -1); } if (className != null && !className.isEmpty()) { imports.add(new ClassRefBuilder().withNewDefinition().withName(className).withPackageName(packageName).and().build()); } } } return imports; }
Example 5
Source File: ApiManager.java From actframework with Apache License 2.0 | 5 votes |
private static String name(Node node) { S.Buffer buffer = S.newBuffer(); Node parent = node.getParentNode(); if (null != parent) { buffer.append(name(parent)); } if (node instanceof NamedNode) { buffer.append(".").append(((NamedNode) node).getName()); } else if (node instanceof CompilationUnit) { CompilationUnit unit = $.cast(node); PackageDeclaration pkg = unit.getPackage(); return pkg.getPackageName(); } return buffer.toString(); }
Example 6
Source File: Nodes.java From deadcode4j with Apache License 2.0 | 5 votes |
@Nonnull public static CompilationUnit getCompilationUnit(@Nonnull Node node) { Node loopNode = node; for (; ; ) { if (CompilationUnit.class.isInstance(loopNode)) { return CompilationUnit.class.cast(loopNode); } loopNode = loopNode.getParentNode(); if (loopNode == null) { throw new RuntimeException("Couldn't locate CompilationUnit for [" + node + "]!"); } } }
Example 7
Source File: Nodes.java From deadcode4j with Apache License 2.0 | 5 votes |
@Nonnull public static String getTypeName(@Nonnull Node node) { List<Node> anonymousClasses = newArrayList(); StringBuilder buffy = new StringBuilder(); Node loopNode = node; for (; ; ) { if (ObjectCreationExpr.class.isInstance(loopNode)) { if (!isEmpty(ObjectCreationExpr.class.cast(loopNode).getAnonymousClassBody())) { anonymousClasses.add(loopNode); } } else if (TypeDeclarationStmt.class.isInstance(loopNode)) { anonymousClasses.add(loopNode); } else if (TypeDeclaration.class.isInstance(loopNode) && !TypeDeclarationStmt.class.isInstance(loopNode.getParentNode())) { TypeDeclaration typeDeclaration = TypeDeclaration.class.cast(loopNode); prependSeparatorIfNecessary('$', buffy).insert(0, typeDeclaration.getName()); appendAnonymousClasses(anonymousClasses, typeDeclaration, buffy); } else if (CompilationUnit.class.isInstance(loopNode)) { if (buffy.length() == 0) { buffy.append("package-info"); } final CompilationUnit compilationUnit = CompilationUnit.class.cast(loopNode); if (compilationUnit.getPackage() != null) { prepend(compilationUnit.getPackage().getName(), buffy); } } loopNode = loopNode.getParentNode(); if (loopNode == null) { return buffy.toString(); } } }
Example 8
Source File: Parser.java From SimFix with GNU General Public License v2.0 | 4 votes |
private void explore(String space, Node node) { /*System.out.println(space + node.getBeginLine() + ":" + node.getEndLine() + " type: " + node.getClass().getCanonicalName() + " has children? " + (node.getChildrenNodes().isEmpty() ? "*false*" : "true"));*/ // ignore everything related to comments if (node.getClass().getCanonicalName() .startsWith("com.github.javaparser.ast.comments.")) { return ; } if (node.getClass().getCanonicalName() .equals("com.github.javaparser.ast.body.EnumConstantDeclaration")) { return ; } if (node.getChildrenNodes().isEmpty()) { Integer line_number = node.getParentNode().getBeginLine(); // is it a statement? if (node.getClass().getCanonicalName() .startsWith("com.github.javaparser.ast.stmt.") && node.getBeginLine() == node.getEndLine()) { line_number = node.getBeginLine(); } else if (node.getParentNode().getBeginLine() == node.getParentNode().getEndLine()) { Node clone = node; Node parent = null; // to handle special cases: parameters, binary expressions, etc // search for the next 'Declaration' or 'Statement' while ((parent = clone.getParentNode()) != null) { if ((parent.getClass().getCanonicalName().startsWith("com.github.javaparser.ast.stmt.")) || (parent.getClass().getCanonicalName() .equals("com.github.javaparser.ast.body.VariableDeclarator")) || (parent.getClass().getCanonicalName().startsWith("com.github.javaparser.ast.body.") && parent.getClass().getCanonicalName().endsWith("Declaration"))) { line_number = parent.getBeginLine(); break; } clone = parent; } } Set<Integer> lines = null; if (this.statements.containsKey(line_number)) { lines = this.statements.get(line_number); } else { lines = new LinkedHashSet<Integer>(); lines.add(line_number); } lines.add(node.getBeginLine()); this.statements.put(line_number, lines); } else { for (Node child : node.getChildrenNodes()) { explore(space + " ", child); } } }
Example 9
Source File: ExtractUtil.java From wisdom with Apache License 2.0 | 4 votes |
private ClassOrInterfaceDeclaration getClassDeclarationOf(Node n) { while(!(n instanceof ClassOrInterfaceDeclaration)) { n = n.getParentNode(); } return (ClassOrInterfaceDeclaration) n; }