List of usage examples for org.eclipse.jdt.core.dom ASTVisitor ASTVisitor
public ASTVisitor()
From source file:ast.StructureParse.java
public void setCode(JTextPane textCode) { ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource(textCode.getText().toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); _package = new DefaultMutableTreeNode("root"); codeTree.setRootVisible(false);// w w w . j a v a 2s .c om cu.accept(new ASTVisitor() { Set names = new HashSet(); /* * Package declaration */ @Override public boolean visit(PackageDeclaration node) { _package = new DefaultMutableTreeNode(node.getName().toString()); codeTree.setRootVisible(true); System.out.println("Package : " + node.getName().toString()); return false; } /* * Class Declaration. * sub node from package */ @Override public boolean visit(TypeDeclaration node) { String type; if (node.isInterface()) type = "interface "; else type = "class "; _class = new DefaultMutableTreeNode(type + node.getName()); System.out.println(type + node.getName()); _package.add(_class); String superClass = node.getSuperclass() + ""; if (!(superClass.equals("null") || (superClass.equals("")))) { _class.add(new DefaultMutableTreeNode("extends " + superClass)); System.out.println("Extends : " + superClass); } for (Object getInterface : node.superInterfaces()) { _class.add(new DefaultMutableTreeNode("implements " + getInterface)); System.out.println("Implements : " + getInterface); } return true; } /* * method declaration * sub node from class */ @Override public boolean visit(MethodDeclaration node) { _method = new DefaultMutableTreeNode(node.getName()); _class.add(_method); System.out.println("MethodDeclaration : " + node.getName()); return true; } /* * field declaration * sub node from class */ // @Override // public boolean visit(FieldDeclaration node) // { // _field=new DefaultMutableTreeNode(node.toString()); // _class.add(_field); // Object o = node.fragments().get(0); // if(o instanceof VariableDeclarationFragment){ // _method.add(new DefaultMutableTreeNode(o.toString())); // } // return false; // } /* * Variable declaration fragment AST node type * used in field declarations * local variable declarations * sub node from method */ @Override public boolean visit(VariableDeclarationFragment node) { if (node.getParent() instanceof FieldDeclaration) { FieldDeclaration declaration = ((FieldDeclaration) node.getParent()); _class.add(new DefaultMutableTreeNode(declaration.getType().toString())); System.out.println("FieldDeclaration : " + declaration.getType().toString()); } else { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("VariableDeclarationFragment : " + node.toString()); } return false; // do not continue to avoid usage info } @Override public boolean visit(VariableDeclarationStatement node) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("VariableDeclarationStatement : " + node.toString()); return false; } /* * Simple Name * A simple name is an identifier other than a keyword, boolean literal ("true", "false") or null literal ("null"). */ @Override public boolean visit(SimpleName node) { if (this.names.contains(node.getIdentifier())) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("SimpleNode Identifier : " + node.toString()); } return false; } /* * Alternate constructor invocation (calling constructor) statement AST node type * For JLS2: */ @Override public boolean visit(SuperConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node); _method.add(_constructorCall); System.out.println("SuperConstructorInvocation : " + node); return false; } /* * method call */ @Override public boolean visit(MethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); _method.add(_methodCall); System.out.println("MethodInvocation : " + node); return false; } @Override public boolean visit(SuperMethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); _method.add(_methodCall); System.out.println("SuperMethodInvocation : " + node); return false; } @Override public boolean visit(ReturnStatement node) { _method.add(new DefaultMutableTreeNode(node.toString())); System.out.println("ReturnStatement : " + node.toString()); return false; } @Override public boolean visit(IfStatement node) { String elseExist = ""; _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")"); _method.add(_if); System.out.println("if : " + node.getExpression()); enclose(node.getThenStatement().toString(), _if); elseExist = node.getElseStatement() + ""; if (!(elseExist.equals("") || elseExist.equals("null"))) { _else = new DefaultMutableTreeNode("else"); _method.add(_else); System.out.println("else : " + node.getElseStatement().toString()); enclose(node.getElseStatement().toString(), _else); } return false; } @Override public boolean visit(EnhancedForStatement node) { _for = new DefaultMutableTreeNode( "for (" + node.getParameter() + " : " + node.getExpression() + ")"); _method.add(_for); System.out.println("EnchancedFor : (" + node.getParameter() + " : " + node.getExpression() + ")"); enclose(node.getBody() + "", _for); return false; } @Override public boolean visit(ForStatement node) { /* * because initial may more than 1. */ String initial = ""; for (int i = 0; i < node.initializers().size(); i++) { initial += node.initializers().get(i); if (node.initializers().size() - 1 != i) initial += ", "; } /* * because increment may more than 1 */ String inc = ""; for (int i = 0; i < node.updaters().size(); i++) { inc += node.updaters().get(i); if (node.updaters().size() - 1 != i) inc += ", "; } _for = new DefaultMutableTreeNode( "for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); _method.add(_for); System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(WhileStatement node) { _while = new DefaultMutableTreeNode("while " + node.getExpression()); _method.add(_while); System.out.println("WhileStatement : " + node.getExpression()); enclose(node.getBody().toString(), _while); return false; } @Override public boolean visit(DoStatement node) { _do = new DefaultMutableTreeNode("do"); _method.add(_do); System.out.println("Do"); enclose(node.getBody().toString(), _do); _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")"); _method.add(_while); System.out.println("WhileDo : " + node.getExpression()); return false; } @Override public boolean visit(TryStatement node) { String ada = ""; _try = new DefaultMutableTreeNode("try"); _method.add(_try); System.out.println("try"); enclose(node.getBody().toString(), _try); ada = node.getFinally() + ""; if (!(ada.equals("") || ada.equals("null"))) { _final = new DefaultMutableTreeNode("finally"); _method.add(_final); System.out.println("finall"); enclose(node.getFinally().toString(), _final); } return false; } @Override public boolean visit(CatchClause node) { _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")"); _method.add(_catch); System.out.println("catch : " + node.getException()); enclose(node.getBody().toString(), _catch); return false; } @Override public boolean visit(Assignment node) { _assignment = new DefaultMutableTreeNode(node.toString()); _method.add(_assignment); System.out.println("Assignment : " + node.toString()); return false; } @Override public boolean visit(ConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); _method.add(_constructorCall); System.out.println(node.toString()); return false; } @Override public boolean visit(AnonymousClassDeclaration node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); _method.add(_constructorCall); System.out.println("AnonymousClassDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayAccess node) { _class = new DefaultMutableTreeNode(node.toString()); _method.add(_class); System.out.println("AbstrackTypeDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayCreation node) { _array = new DefaultMutableTreeNode(node.toString()); _method.add(_array); System.out.println("ArrayCreation : " + node.toString()); return false; } @Override public boolean visit(ArrayInitializer node) { _array = new DefaultMutableTreeNode(node.toString()); System.out.println("ArrayInitialize : " + node.toString()); _method.add(_array); return false; } @Override public boolean visit(AssertStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("AssertStatement : " + node.toString()); _method.add(_statement); return false; } @Override public boolean visit(ContinueStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("ContinueStatement : " + node.toString()); _method.add(_statement); return false; } @Override public boolean visit(SwitchStatement node) { _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")"); System.out.println("switch (" + node.getExpression() + ")"); _method.add(_switch); List getStatement = node.statements(); for (Object st : getStatement) { Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString()); if (_caseMatch.find()) { _case = new DefaultMutableTreeNode(_caseMatch.group()); _switch.add(_case); } enclose(st.toString(), _case); Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString()); if (_breakMatch.find()) { _break = new DefaultMutableTreeNode(_breakMatch.group()); _case.add(_break); } } return false; } @Override public boolean visit(ClassInstanceCreation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); System.out.println("ClassInstanceCreation : " + node.toString()); _method.add(_constructorCall); return false; } }); // model = new DefaultTreeModel(_package); // model.reload(); // codeTree.setModel(model); codeTree.setModel(new DefaultTreeModel(_package) { public void reload(TreeNode node) { if (node != null) { fireTreeStructureChanged(this, getPathToRoot(node), null, null); } } }); codeTree.setCellRenderer(new TreeRender()); // ((DefaultTreeModel)codeTree.getModel()).reload(); for (int i = 0; i < codeTree.getRowCount(); i++) codeTree.expandRow(i); }
From source file:ast.StructureParse.java
private void enclose(String body, DefaultMutableTreeNode parentNode) { ASTParser parser = ASTParser.newParser(AST.JLS2); /*//www .j a va2s .c o m * make parse result into OPP structure, because AST wanna be that. */ String setClass = "class fo{\nvoid foo(){ \n" + body + "\n}\n}"; parser.setSource(setClass.toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); //ASTNode node = parser.createAST(null); parser.setResolveBindings(true); try { final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { Set names2 = new HashSet(); @Override public boolean visit(SimpleName node) { if (this.names2.contains(node.getIdentifier())) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("SimpleNode : " + node.toString()); } return true; } @Override public boolean visit(EnhancedForStatement node) { _for = new DefaultMutableTreeNode( "for (" + node.getParameter() + " : " + node.getExpression() + ")"); parentNode.add(_for); System.out.println("for (" + node.getParameter() + " : " + node.getExpression() + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(ForStatement node) { /* * because initial may more than 1. */ String initial = ""; for (int i = 0; i < node.initializers().size(); i++) { initial += node.initializers().get(i); if (node.initializers().size() - 1 != i) initial += ", "; } /* * because increment may more than 1 */ String inc = ""; for (int i = 0; i < node.updaters().size(); i++) { inc += node.updaters().get(i); if (node.updaters().size() - 1 != i) inc += ", "; } _for = new DefaultMutableTreeNode( "for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); parentNode.add(_for); System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")"); enclose(node.getBody().toString(), _for); return false; } @Override public boolean visit(IfStatement node) { String elseExist = ""; _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")"); System.out.println("if (+" + node.getExpression() + ")"); parentNode.add(_if); enclose(node.getThenStatement().toString(), _if); elseExist = node.getElseStatement() + ""; if (!(elseExist.equals("") || (elseExist.equals("null")))) { _else = new DefaultMutableTreeNode("else"); System.out.println("else"); parentNode.add(_else); enclose(node.getElseStatement().toString(), _else); } return false; } @Override public boolean visit(VariableDeclarationFragment node) { if (node.getParent() instanceof FieldDeclaration) { FieldDeclaration declaration = ((FieldDeclaration) node.getParent()); _class.add(new DefaultMutableTreeNode(declaration.getType().toString())); } else { System.out.println("VariableDeclarationFragment : " + node.toString()); parentNode.add(new DefaultMutableTreeNode(node.toString())); } return false; // do not continue to avoid usage info } @Override public boolean visit(ReturnStatement node) { parentNode.add(new DefaultMutableTreeNode(node.toString())); System.out.println("Return : " + node.toString()); return false; } @Override public boolean visit(SuperConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node); parentNode.add(_constructorCall); System.out.println("SuperConstructorInvocation : " + node); return false; } @Override public boolean visit(MethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("MethodInvocation : " + node); return true; } @Override public boolean visit(SuperMethodInvocation node) { _methodCall = new DefaultMutableTreeNode(node); parentNode.add(_methodCall); System.out.println("SuperMethodInvocation : " + node); return false; } @Override public boolean visit(WhileStatement node) { _while = new DefaultMutableTreeNode("while " + node.getExpression()); parentNode.add(_while); System.out.println("WhileStatement : " + node.getExpression()); enclose(node.getBody().toString(), _while); return false; } @Override public boolean visit(DoStatement node) { _do = new DefaultMutableTreeNode("do"); parentNode.add(_do); System.out.println("do"); enclose(node.getBody().toString(), _do); _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")"); parentNode.add(_while); return false; } @Override public boolean visit(TryStatement node) { String ada = ""; _try = new DefaultMutableTreeNode("try"); parentNode.add(_try); System.out.println("try"); enclose(node.getBody().toString(), _try); ada = node.getFinally() + ""; if (!(ada.equals("") || (ada.equals("null")))) { _final = new DefaultMutableTreeNode("finally"); parentNode.add(_final); System.out.println("finally"); enclose(node.getFinally().toString(), _final); } return false; } @Override public boolean visit(CatchClause node) { _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")"); parentNode.add(_catch); System.out.println("catch : " + node.getException()); enclose(node.getBody().toString(), _catch); return false; } @Override public boolean visit(Assignment node) { _assignment = new DefaultMutableTreeNode(node.toString()); parentNode.add(_assignment); System.out.println("Assignment : " + node.toString()); return false; } @Override public boolean visit(ConstructorInvocation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println(node.toString()); return false; } @Override public boolean visit(AnonymousClassDeclaration node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); parentNode.add(_constructorCall); System.out.println("AnonymousClassDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayAccess node) { _class = new DefaultMutableTreeNode(node.toString()); parentNode.add(_class); System.out.println("AbstrackTypeDeclaration : " + node.toString()); return false; } @Override public boolean visit(ArrayCreation node) { _array = new DefaultMutableTreeNode(node.toString()); _method.add(_array); System.out.println("ArrayCreation : " + node.toString()); return false; } @Override public boolean visit(ArrayInitializer node) { _array = new DefaultMutableTreeNode(node.toString()); System.out.println("ArrayInitialize : " + node.toString()); parentNode.add(_array); return false; } @Override public boolean visit(AssertStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("AssertStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(ContinueStatement node) { _statement = new DefaultMutableTreeNode(node.toString()); System.out.println("ContinueStatement : " + node.toString()); parentNode.add(_statement); return false; } @Override public boolean visit(SwitchStatement node) { _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")"); System.out.println("switch (" + node.getExpression() + ")"); parentNode.add(_switch); List getStatement = node.statements(); for (Object st : getStatement) { Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString()); if (_caseMatch.find()) { _case = new DefaultMutableTreeNode(_caseMatch.group()); _switch.add(_case); } enclose(st.toString(), _case); Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString()); if (_breakMatch.find()) { _break = new DefaultMutableTreeNode(_breakMatch.group()); _case.add(_break); } } return false; } @Override public boolean visit(ClassInstanceCreation node) { _constructorCall = new DefaultMutableTreeNode(node.toString()); System.out.println("ClassInstanceCreation : " + node.toString()); parentNode.add(_constructorCall); return false; } }); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:ch.acanda.eclipse.pmd.java.resolution.SuppressWarningsQuickFix.java
License:Open Source License
private BodyDeclaration findBodyDeclaration(final ASTNode node) { final BodyDeclaration[] bodyDeclaration = new BodyDeclaration[1]; node.accept(new ASTVisitor() { @Override/*w w w.j a v a 2s.co m*/ public boolean visit(final EnumDeclaration node) { bodyDeclaration[0] = node; return false; } @Override public boolean visit(final TypeDeclaration node) { bodyDeclaration[0] = node; return false; } @Override public boolean visit(final AnnotationTypeDeclaration node) { bodyDeclaration[0] = node; return false; } }); return bodyDeclaration[0]; }
From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java
License:Open Source License
private void fixEnumConstantIndents(ASTNode astRoot) { if (this.options.use_tabs_only_for_leading_indentations) { // enum constants should be indented like other declarations, not like wrapped elements astRoot.accept(new ASTVisitor() { @Override//from w w w . j a v a 2 s . com public boolean visit(EnumConstantDeclaration node) { WrapPreparator.this.tm.firstTokenIn(node, TokenNameIdentifier).setWrapPolicy(null); return true; } }); } }
From source file:com.facebook.buck.java.JavaFileParser.java
License:Apache License
public ImmutableSortedSet<String> getExportedSymbolsFromString(String code) { final CompilationUnit compilationUnit = makeCompilationUnitFromSource(code); final ImmutableSortedSet.Builder<String> symbolsBuilder = ImmutableSortedSet.naturalOrder(); compilationUnit.accept(new ASTVisitor() { @Override// w w w. j av a 2s.com public boolean visit(TypeDeclaration node) { // Local classes can be declared inside of methods. Skip over these. if (node.getParent() instanceof TypeDeclarationStatement) { return true; } symbolsBuilder.add(getFullyQualifiedTypeName(node)); return true; } @Override public boolean visit(EnumDeclaration node) { symbolsBuilder.add(getFullyQualifiedTypeName(node)); return true; } @Override public boolean visit(AnnotationTypeDeclaration node) { symbolsBuilder.add(getFullyQualifiedTypeName(node)); return true; } }); return symbolsBuilder.build(); }
From source file:com.facebook.buck.jvm.java.JavaFileParser.java
License:Apache License
public JavaFileFeatures extractFeaturesFromJavaCode(String code) { // For now, we will harcode this. Ultimately, we probably want to make this configurable via // .buckconfig. For example, the Buck project itself is diligent about disallowing wildcard // imports, but the one exception is the Java code generated via Thrift in src-gen. boolean shouldThrowForUnsupportedWildcardImport = false; AtomicBoolean isPoisonedByUnsupportedWildcardImport = new AtomicBoolean(false); CompilationUnit compilationUnit = makeCompilationUnitFromSource(code); ImmutableSortedSet.Builder<String> providedSymbols = ImmutableSortedSet.naturalOrder(); ImmutableSortedSet.Builder<String> requiredSymbols = ImmutableSortedSet.naturalOrder(); ImmutableSortedSet.Builder<String> exportedSymbols = ImmutableSortedSet.naturalOrder(); ImmutableSortedSet.Builder<String> requiredSymbolsFromExplicitImports = ImmutableSortedSet.naturalOrder(); compilationUnit.accept(new ASTVisitor() { @Nullable/*from ww w. ja va 2 s . c o m*/ private String packageName; /** Maps simple name to fully-qualified name. */ private Map<String, String> simpleImportedTypes = new HashMap<>(); /** * Maps wildcard import prefixes, such as {@code "java.util"} to the types in the * respective package if a wildcard import such as {@code import java.util.*} is used. */ private Map<String, ImmutableSet<String>> wildcardImports = new HashMap<>(); @Override public boolean visit(PackageDeclaration node) { Preconditions.checkState(packageName == null, "There should be at most one package declaration"); packageName = node.getName().getFullyQualifiedName(); return false; } // providedSymbols @Override public boolean visit(TypeDeclaration node) { // Local classes can be declared inside of methods. Skip over these. if (node.getParent() instanceof TypeDeclarationStatement) { return true; } String fullyQualifiedName = getFullyQualifiedTypeName(node); if (fullyQualifiedName != null) { providedSymbols.add(fullyQualifiedName); } @SuppressWarnings("unchecked") List<Type> interfaceTypes = node.superInterfaceTypes(); for (Type interfaceType : interfaceTypes) { tryAddType(interfaceType, DependencyType.EXPORTED); } Type superclassType = node.getSuperclassType(); if (superclassType != null) { tryAddType(superclassType, DependencyType.EXPORTED); } return true; } @Override public boolean visit(EnumDeclaration node) { String fullyQualifiedName = getFullyQualifiedTypeName(node); if (fullyQualifiedName != null) { providedSymbols.add(fullyQualifiedName); } return true; } @Override public boolean visit(AnnotationTypeDeclaration node) { String fullyQualifiedName = getFullyQualifiedTypeName(node); if (fullyQualifiedName != null) { providedSymbols.add(fullyQualifiedName); } return true; } // requiredSymbols /** * Uses heuristics to try to figure out what type of QualifiedName this is. Returns a * non-null value if this is believed to be a reference that qualifies as a "required * symbol" relationship. */ @Override public boolean visit(QualifiedName node) { QualifiedName ancestor = findMostQualifiedAncestor(node); ASTNode parent = ancestor.getParent(); if (!(parent instanceof PackageDeclaration) && !(parent instanceof ImportDeclaration)) { String symbol = ancestor.getFullyQualifiedName(); // If it does not start with an uppercase letter, it is probably because it is a // property lookup. if (CharMatcher.javaUpperCase().matches(symbol.charAt(0))) { addTypeFromDotDelimitedSequence(symbol, DependencyType.REQUIRED); } } return false; } /** * @param expr could be "Example", "Example.field", "com.example.Example". Note it could * also be a built-in type, such as "java.lang.Integer", in which case it will not be * added to the set of required symbols. */ private void addTypeFromDotDelimitedSequence(String expr, DependencyType dependencyType) { // At this point, symbol could be `System.out`. We want to reduce it to `System` and // then check it against JAVA_LANG_TYPES. if (startsWithUppercaseChar(expr)) { int index = expr.indexOf('.'); if (index >= 0) { String leftmostComponent = expr.substring(0, index); if (JAVA_LANG_TYPES.contains(leftmostComponent)) { return; } } } expr = qualifyWithPackageNameIfNecessary(expr); addSymbol(expr, dependencyType); } @Override public boolean visit(ImportDeclaration node) { String fullyQualifiedName = node.getName().getFullyQualifiedName(); // Apparently, "on demand" means "uses a wildcard," such as "import java.util.*". // Although we can choose to prohibit these in our own code, it is much harder to // enforce for third-party code. As such, we will tolerate these for some of the common // cases. if (node.isOnDemand()) { ImmutableSet<String> value = SUPPORTED_WILDCARD_IMPORTS.get(fullyQualifiedName); if (value != null) { wildcardImports.put(fullyQualifiedName, value); return false; } else if (shouldThrowForUnsupportedWildcardImport) { throw new RuntimeException(String.format( "Use of wildcard 'import %s.*' makes it impossible to statically determine " + "required symbols in this file. Please enumerate explicit imports.", fullyQualifiedName)); } else { isPoisonedByUnsupportedWildcardImport.set(true); return false; } } // Only worry about the dependency on the enclosing type. Optional<String> simpleName = getSimpleNameFromFullyQualifiedName(fullyQualifiedName); if (simpleName.isPresent()) { String name = simpleName.get(); int index = fullyQualifiedName.indexOf("." + name); String enclosingType = fullyQualifiedName.substring(0, index + name.length() + 1); requiredSymbolsFromExplicitImports.add(enclosingType); simpleImportedTypes.put(name, enclosingType); } else { LOG.warn("Suspicious import lacks obvious enclosing type: %s", fullyQualifiedName); // The one example we have seen of this in the wild is // "org.whispersystems.curve25519.java.curve_sigs". In practice, we still need to add // it as a required symbol in this case. requiredSymbols.add(fullyQualifiedName); } return false; } @Override public boolean visit(MethodInvocation node) { if (node.getExpression() == null) { return true; } String receiver = node.getExpression().toString(); if (looksLikeAType(receiver)) { addTypeFromDotDelimitedSequence(receiver, DependencyType.REQUIRED); } return true; } /** An annotation on a member with zero arguments. */ @Override public boolean visit(MarkerAnnotation node) { DependencyType dependencyType = findDependencyTypeForAnnotation(node); addSimpleTypeName(node.getTypeName(), dependencyType); return true; } /** An annotation on a member with named arguments. */ @Override public boolean visit(NormalAnnotation node) { DependencyType dependencyType = findDependencyTypeForAnnotation(node); addSimpleTypeName(node.getTypeName(), dependencyType); return true; } /** An annotation on a member with a single, unnamed argument. */ @Override public boolean visit(SingleMemberAnnotation node) { DependencyType dependencyType = findDependencyTypeForAnnotation(node); addSimpleTypeName(node.getTypeName(), dependencyType); return true; } private DependencyType findDependencyTypeForAnnotation(Annotation annotation) { ASTNode parentNode = annotation.getParent(); if (parentNode == null) { return DependencyType.REQUIRED; } if (parentNode instanceof BodyDeclaration) { // Note that BodyDeclaration is an abstract class. Its subclasses are things like // FieldDeclaration and MethodDeclaration. We want to be sure that an annotation on // any non-private declaration is considered an exported symbol. BodyDeclaration declaration = (BodyDeclaration) parentNode; int modifiers = declaration.getModifiers(); if ((modifiers & Modifier.PRIVATE) == 0) { return DependencyType.EXPORTED; } } return DependencyType.REQUIRED; } @Override public boolean visit(SimpleType node) { // This method is responsible for finding the overwhelming majority of the required // symbols in the AST. tryAddType(node, DependencyType.REQUIRED); return true; } // exportedSymbols @Override public boolean visit(MethodDeclaration node) { // Types from private method signatures need not be exported. if ((node.getModifiers() & Modifier.PRIVATE) != 0) { return true; } Type returnType = node.getReturnType2(); if (returnType != null) { tryAddType(returnType, DependencyType.EXPORTED); } @SuppressWarnings("unchecked") List<SingleVariableDeclaration> params = node.parameters(); for (SingleVariableDeclaration decl : params) { tryAddType(decl.getType(), DependencyType.EXPORTED); } @SuppressWarnings("unchecked") List<Type> exceptions = node.thrownExceptionTypes(); for (Type exception : exceptions) { tryAddType(exception, DependencyType.EXPORTED); } return true; } @Override public boolean visit(FieldDeclaration node) { // Types from private fields need not be exported. if ((node.getModifiers() & Modifier.PRIVATE) == 0) { tryAddType(node.getType(), DependencyType.EXPORTED); } return true; } private void tryAddType(Type type, DependencyType dependencyType) { if (type.isSimpleType()) { SimpleType simpleType = (SimpleType) type; Name simpleTypeName = simpleType.getName(); String simpleName = simpleTypeName.toString(); // For a Type such as IExample<T>, both "IExample" and "T" will be submitted here as // simple types. As such, we use this imperfect heuristic to filter out "T" from being // added. Note that this will erroneously exclude "URI". In practice, this should // generally be OK. For example, assuming "URI" is also imported, then at least it // will end up in the set of required symbols. To this end, we perform a second check // for "all caps" types to see if there is a corresponding import and if it should be // exported rather than simply required. if (!CharMatcher.javaUpperCase().matchesAllOf(simpleName) || (dependencyType == DependencyType.EXPORTED && simpleImportedTypes.containsKey(simpleName))) { addSimpleTypeName(simpleTypeName, dependencyType); } } else if (type.isArrayType()) { ArrayType arrayType = (ArrayType) type; tryAddType(arrayType.getElementType(), dependencyType); } else if (type.isParameterizedType()) { ParameterizedType parameterizedType = (ParameterizedType) type; tryAddType(parameterizedType.getType(), dependencyType); @SuppressWarnings("unchecked") List<Type> argTypes = parameterizedType.typeArguments(); for (Type argType : argTypes) { tryAddType(argType, dependencyType); } } } private void addSimpleTypeName(Name simpleTypeName, DependencyType dependencyType) { String simpleName = simpleTypeName.toString(); if (JAVA_LANG_TYPES.contains(simpleName)) { return; } String fullyQualifiedNameForSimpleName = simpleImportedTypes.get(simpleName); if (fullyQualifiedNameForSimpleName != null) { // May need to promote from required to exported in this case. if (dependencyType == DependencyType.EXPORTED) { addSymbol(fullyQualifiedNameForSimpleName, DependencyType.EXPORTED); } return; } // For well-behaved source code, this will always be empty, so don't even bother to // create the iterator most of the time. if (!wildcardImports.isEmpty()) { for (Map.Entry<String, ImmutableSet<String>> entry : wildcardImports.entrySet()) { Set<String> types = entry.getValue(); if (types.contains(simpleName)) { String packageName = entry.getKey(); addSymbol(packageName + "." + simpleName, dependencyType); return; } } } String symbol = simpleTypeName.getFullyQualifiedName(); symbol = qualifyWithPackageNameIfNecessary(symbol); addSymbol(symbol, dependencyType); } private void addSymbol(String symbol, DependencyType dependencyType) { ((dependencyType == DependencyType.REQUIRED) ? requiredSymbols : exportedSymbols).add(symbol); } private String qualifyWithPackageNameIfNecessary(String symbol) { if (!startsWithUppercaseChar(symbol)) { return symbol; } // If the symbol starts with a capital letter, then we assume that it is a reference to // a type in the same package. int index = symbol.indexOf('.'); if (index >= 0) { symbol = symbol.substring(0, index); } if (packageName != null) { symbol = packageName + "." + symbol; } return symbol; } }); // TODO(mbolin): Special treatment for exportedSymbols when poisoned by wildcard import. ImmutableSortedSet<String> totalExportedSymbols = exportedSymbols.build(); // If we were poisoned by an unsupported wildcard import, then we should rely exclusively on // the explicit imports to determine the required symbols. Set<String> totalRequiredSymbols = new HashSet<>(); if (isPoisonedByUnsupportedWildcardImport.get()) { totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build()); } else { totalRequiredSymbols.addAll(requiredSymbolsFromExplicitImports.build()); totalRequiredSymbols.addAll(requiredSymbols.build()); } // Make sure that required and exported symbols are disjoint sets. totalRequiredSymbols.removeAll(totalExportedSymbols); return new JavaFileFeatures(providedSymbols.build(), ImmutableSortedSet.copyOf(totalRequiredSymbols), totalExportedSymbols); }
From source file:com.github.parzonka.ccms.engine.SortElementsOperation.java
License:Open Source License
private ASTRewrite sortCompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit ast, final TextEditGroup group) { ast.accept(new ASTVisitor() { @Override/*from ww w .j a v a2 s .c om*/ public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) { final List<AbstractTypeDeclaration> types = compilationUnit.types(); for (final Iterator<AbstractTypeDeclaration> iter = types.iterator(); iter.hasNext();) { final AbstractTypeDeclaration typeDeclaration = iter.next(); typeDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(typeDeclaration.getStartPosition())); compilationUnit.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(typeDeclaration))); } return true; } @Override public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) { final List bodyDeclarations = annotationTypeDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); annotationTypeDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) { final List bodyDeclarations = anonymousClassDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); anonymousClassDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(TypeDeclaration typeDeclaration) { final List bodyDeclarations = typeDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); typeDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } return true; } @Override public boolean visit(EnumDeclaration enumDeclaration) { final List bodyDeclarations = enumDeclaration.bodyDeclarations(); for (final Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { final BodyDeclaration bodyDeclaration = (BodyDeclaration) iter.next(); bodyDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(bodyDeclaration.getStartPosition())); enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(bodyDeclaration))); } final List enumConstants = enumDeclaration.enumConstants(); for (final Iterator iter = enumConstants.iterator(); iter.hasNext();) { final EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) iter.next(); enumConstantDeclaration.setProperty(CompilationUnitSorter.RELATIVE_ORDER, new Integer(enumConstantDeclaration.getStartPosition())); enumDeclaration.setProperty(CONTAINS_MALFORMED_NODES, Boolean.valueOf(isMalformed(enumConstantDeclaration))); } return true; } }); final ASTRewrite rewriter = ASTRewrite.create(ast.getAST()); final boolean[] hasChanges = new boolean[] { false }; ast.accept(new ASTVisitor() { /** * This * * @param elements * @param listRewrite */ private void sortElements(List<BodyDeclaration> elements, ListRewrite listRewrite) { if (elements.size() == 0) return; final List<BodyDeclaration> myCopy = new ArrayList<BodyDeclaration>(); myCopy.addAll(elements); // orderexperiment final List<Signature> methods = new ArrayList<Signature>(); for (final BodyDeclaration bd : myCopy) { if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) { methods.add(new Signature((MethodDeclaration) bd)); } } Collections.sort(methods, ((BodyDeclarationComparator) SortElementsOperation.this.comparator) .getMethodDeclarationComparator()); logger.info("Sorting of methods based on appearance:"); int j = 0; for (final Signature sig : methods) { logger.info("Method [{}] : {}", ++j, sig); } Collections.sort(myCopy, SortElementsOperation.this.comparator); logger.info("Final sorting order just before the AST-Rewrite:"); for (final BodyDeclaration bd : myCopy) { if (bd.getNodeType() == ASTNode.METHOD_DECLARATION) { logger.info("{}", new Signature((MethodDeclaration) bd)); } else { logger.info("{}", bd.toString()); } } for (int i = 0; i < elements.size(); i++) { final BodyDeclaration oldNode = elements.get(i); final BodyDeclaration newNode = myCopy.get(i); if (oldNode != newNode) { if (oldNode.getNodeType() == ASTNode.METHOD_DECLARATION && newNode.getNodeType() == ASTNode.METHOD_DECLARATION) { final Signature oldMethodSignature = new Signature((MethodDeclaration) oldNode); final Signature newMethodSignature = new Signature((MethodDeclaration) newNode); logger.trace("Swapping [{}]for [{}]", oldMethodSignature, newMethodSignature); } else { logger.trace("Swapping [{}]for [{}]", oldNode.getNodeType(), newNode.getNodeType()); } listRewrite.replace(oldNode, rewriter.createMoveTarget(newNode), group); hasChanges[0] = true; } } } @Override public boolean visit(org.eclipse.jdt.core.dom.CompilationUnit compilationUnit) { if (checkMalformedNodes(compilationUnit)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(compilationUnit.types(), rewriter.getListRewrite(compilationUnit, org.eclipse.jdt.core.dom.CompilationUnit.TYPES_PROPERTY)); return true; } @Override public boolean visit(AnnotationTypeDeclaration annotationTypeDeclaration) { if (checkMalformedNodes(annotationTypeDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(annotationTypeDeclaration.bodyDeclarations(), rewriter.getListRewrite( annotationTypeDeclaration, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) { if (checkMalformedNodes(anonymousClassDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(anonymousClassDeclaration.bodyDeclarations(), rewriter.getListRewrite( anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(TypeDeclaration typeDeclaration) { if (checkMalformedNodes(typeDeclaration)) { logger.warn("Malformed nodes. Aborting sorting of current element."); return true; } sortElements(typeDeclaration.bodyDeclarations(), rewriter.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY)); return true; } @Override public boolean visit(EnumDeclaration enumDeclaration) { if (checkMalformedNodes(enumDeclaration)) { return true; // abort sorting of current element } sortElements(enumDeclaration.bodyDeclarations(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.BODY_DECLARATIONS_PROPERTY)); sortElements(enumDeclaration.enumConstants(), rewriter.getListRewrite(enumDeclaration, EnumDeclaration.ENUM_CONSTANTS_PROPERTY)); return true; } }); if (!hasChanges[0]) return null; return rewriter; }
From source file:com.google.currysrc.processors.BaseJavadocTagClasses.java
License:Apache License
@Override public final void process(Context context, CompilationUnit cu) { final List<AbstractTypeDeclaration> toHide = Lists.newArrayList(); cu.accept(new ASTVisitor() { @Override//from w w w . j a va 2s. c om public boolean visit(TypeDeclaration node) { return visitAbstract(node); } @Override public boolean visit(EnumDeclaration node) { return visitAbstract(node); } private boolean visitAbstract(AbstractTypeDeclaration node) { if (mustTag(node)) { toHide.add(node); } return false; } }); ASTRewrite rewrite = context.rewrite(); for (AbstractTypeDeclaration node : Lists.reverse(toHide)) { JavadocUtils.addJavadocTag(rewrite, node, tagText); } }
From source file:com.google.dart.java2dart.SyntaxTranslator.java
License:Open Source License
private static boolean hasConstructorInvocation(org.eclipse.jdt.core.dom.ASTNode node) { final AtomicBoolean result = new AtomicBoolean(); node.accept(new ASTVisitor() { @Override//w w w .j av a 2s . c om public boolean visit(ConstructorInvocation node) { result.set(true); return false; } }); return result.get(); }
From source file:com.google.dart.java2dart.SyntaxTranslator.java
License:Open Source License
@Override public boolean visit(org.eclipse.jdt.core.dom.ClassInstanceCreation node) { IMethodBinding binding = node.resolveConstructorBinding(); String signature = JavaUtils.getJdtSignature(binding); TypeName typeNameNode = (TypeName) translate(node.getType()); final List<Expression> arguments = translateArguments(binding, node.arguments()); final ClassDeclaration innerClass; {/*from w w w. j a v a 2 s. c om*/ AnonymousClassDeclaration anoDeclaration = node.getAnonymousClassDeclaration(); if (anoDeclaration != null) { ITypeBinding superclass = anoDeclaration.resolveBinding().getSuperclass(); signature = superclass.getKey() + StringUtils.substringAfter(signature, ";"); String name = typeNameNode.getName().getName().replace('.', '_'); name = name + "_" + context.generateTechnicalAnonymousClassIndex(); innerClass = declareInnerClass(binding, anoDeclaration, name, ArrayUtils.EMPTY_STRING_ARRAY); typeNameNode = typeName(name); // prepare enclosing type final ITypeBinding enclosingTypeBinding = getEnclosingTypeBinding(node); final SimpleIdentifier enclosingTypeRef; final AtomicBoolean addEnclosingTypeRef = new AtomicBoolean(); { if (enclosingTypeBinding != null) { enclosingTypeRef = identifier(enclosingTypeBinding.getName() + "_this"); // add enclosing class references innerClass.accept(new RecursiveASTVisitor<Void>() { @Override public Void visitMethodInvocation(MethodInvocation node) { if (node.getTarget() == null) { IMethodBinding methodBinding = (IMethodBinding) context.getNodeBinding(node); if (methodBinding != null && methodBinding.getDeclaringClass() == enclosingTypeBinding) { addEnclosingTypeRef.set(true); node.setTarget(enclosingTypeRef); } } return super.visitMethodInvocation(node); } @Override public Void visitSimpleIdentifier(SimpleIdentifier node) { if (!(node.getParent() instanceof PropertyAccess) && !(node.getParent() instanceof PrefixedIdentifier)) { Object binding = context.getNodeBinding(node); if (binding instanceof IVariableBinding) { IVariableBinding variableBinding = (IVariableBinding) binding; if (variableBinding.isField() && variableBinding.getDeclaringClass() == enclosingTypeBinding) { addEnclosingTypeRef.set(true); replaceNode(node.getParent(), node, propertyAccess(enclosingTypeRef, node)); } } } return super.visitSimpleIdentifier(node); } }); } else { enclosingTypeRef = null; } } // declare referenced final variables XXX final String finalName = name; anoDeclaration.accept(new ASTVisitor() { final Set<org.eclipse.jdt.core.dom.IVariableBinding> addedParameters = Sets.newHashSet(); final List<FormalParameter> constructorParameters = Lists.newArrayList(); int index; @Override public void endVisit(AnonymousClassDeclaration node) { if (!constructorParameters.isEmpty()) { // add parameters to the existing "inner" constructor XXX for (ClassMember classMember : innerClass.getMembers()) { if (classMember instanceof ConstructorDeclaration) { ConstructorDeclaration innerConstructor = (ConstructorDeclaration) classMember; innerConstructor.getParameters().getParameters().addAll(constructorParameters); return; } } // create new "inner" constructor innerClass.getMembers().add(index, constructorDeclaration(identifier(finalName), null, formalParameterList(constructorParameters), null)); } super.endVisit(node); } @Override public void endVisit(SimpleName node) { IBinding nameBinding = node.resolveBinding(); if (nameBinding instanceof org.eclipse.jdt.core.dom.IVariableBinding) { org.eclipse.jdt.core.dom.IVariableBinding variableBinding = (org.eclipse.jdt.core.dom.IVariableBinding) nameBinding; org.eclipse.jdt.core.dom.MethodDeclaration enclosingMethod = getEnclosingMethod(node); if (!variableBinding.isField() && enclosingMethod != null && variableBinding.getDeclaringMethod() != enclosingMethod.resolveBinding() && addedParameters.add(variableBinding)) { TypeName parameterTypeName = translateTypeName(variableBinding.getType()); String parameterName = variableBinding.getName(); SimpleIdentifier parameterNameNode = identifier(parameterName); innerClass.getMembers().add(index++, fieldDeclaration(parameterTypeName, variableDeclaration(parameterNameNode))); constructorParameters.add(fieldFormalParameter(null, null, parameterNameNode)); arguments.add(parameterNameNode); context.putReference(parameterNameNode, variableBinding, null); } } super.endVisit(node); } @Override public boolean visit(AnonymousClassDeclaration node) { if (addEnclosingTypeRef.get()) { TypeName parameterTypeName = translateTypeName(enclosingTypeBinding); innerClass.getMembers().add(index++, fieldDeclaration(false, Keyword.FINAL, parameterTypeName, variableDeclaration(enclosingTypeRef))); constructorParameters.add(fieldFormalParameter(null, null, enclosingTypeRef)); arguments.add(thisExpression()); } return super.visit(node); } }); } else { innerClass = null; } } InstanceCreationExpression creation = instanceCreationExpression(Keyword.NEW, typeNameNode, null, arguments); context.putNodeBinding(creation, binding); context.putAnonymousDeclaration(creation, innerClass); context.getConstructorDescription(binding).instanceCreations.add(creation); return done(creation); }