CompilationUnitTree getCompilationUnit() { for (CompilationUnitTree ct : compUnits) { for (Tree clazz : ct.getTypeDecls()) { if (clazz.equals(currentClass)) { return ct; } } } return null; }
/** Returns true if all declarations inside the given compilation unit have been visited. */ private boolean finishedCompilation(CompilationUnitTree tree) { OUTER: for (Tree decl : tree.getTypeDecls()) { switch (decl.getKind()) { case EMPTY_STATEMENT: // ignore ";" at the top level, which counts as an empty type decl continue OUTER; case IMPORT: // The spec disallows mixing imports and empty top-level declarations (";"), but // javac has a bug that causes it to accept empty declarations interspersed with imports: // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-August/006968.html // // Any import declarations after the first semi are incorrectly added to the list // of type declarations, so we have to skip over them here. continue OUTER; default: break; } if (!seen.contains(decl)) { return false; } } return true; } }
private static VisitorState getCheckState(VisitorState state) { // Gets the VisitorState to start from when checking how to qualify the name. This won't work // correctly in all cases 1) it assumes there is only 1 top level type; 2) it doesn't look for // all of the locations where the symbol-to-be-replaced is used in the compilation unit. // Really, we should gather all of the usages first, and check them all. // It is assumed that this will work sufficiently until proven otherwise. CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); if (compilationUnit.getTypeDecls().isEmpty()) { return state; } return state.withPath(TreePath.getPath(compilationUnit, compilationUnit.getTypeDecls().get(0))); }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getSourceFile() == null) { return NO_MATCH; } String name = tree.getSourceFile().getName(); int idx = name.lastIndexOf('/'); if (idx != -1) { name = name.substring(idx + 1); } if (!name.equals("package-info.java")) { return NO_MATCH; } if (tree.getTypeDecls().isEmpty()) { return NO_MATCH; } return describeMatch(tree.getTypeDecls().get(0)); } }
/** * Generates the {@link SuppressionInfo} for a {@link CompilationUnitTree}. This differs in that * {@code isGenerated} is determined by inspecting the annotations of the outermost class so that * matchers on {@link CompilationUnitTree} will also be suppressed. */ public SuppressionInfo forCompilationUnit(CompilationUnitTree tree, VisitorState state) { AtomicBoolean generated = new AtomicBoolean(false); new SimpleTreeVisitor<Void, Void>() { @Override public Void visitClass(ClassTree node, Void unused) { ClassSymbol symbol = ASTHelpers.getSymbol(node); generated.compareAndSet(false, symbol != null && isGenerated(symbol, state)); return null; } }.visit(tree.getTypeDecls(), null); return new SuppressionInfo(suppressWarningsStrings, customSuppressions, generated.get()); }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { List<Tree> toDelete = new ArrayList<>(); for (Tree member : tree.getTypeDecls()) { if (member.getKind() == Tree.Kind.EMPTY_STATEMENT) { toDelete.add(member); } } if (toDelete.isEmpty()) { return Description.NO_MATCH; } SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); for (Tree member : toDelete) { fixBuilder.delete(member); } return describeMatch(toDelete.get(0), fixBuilder.build()); } }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getTypeDecls().size() <= 1) { for (Tree member : tree.getTypeDecls()) { if (member instanceof ClassTree) { ClassTree classMember = (ClassTree) member; "Expected at most one top-level class declaration, instead found: %s", Joiner.on(", ").join(names)); return buildDescription(firstNonNull(tree.getPackageName(), tree.getTypeDecls().get(0))) .setMessage(message) .build();
@Override public void finished(TaskEvent e) { if (e.getKind() != TaskEvent.Kind.ANALYZE) return; TypeElement elem = e.getTypeElement(); for(Tree t : e.getCompilationUnit().getTypeDecls()) { if (t.getKind() == Tree.Kind.CLASS) { if (((JCClassDecl)t).sym.equals(elem)) { currentClass = (ClassTree)t; break; } } } if (currentClass != null) { verify(currentClass, elem); } }
@Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { if (tree.getTypeDecls().isEmpty() || tree.getPackageName() == null) { return Description.NO_MATCH; for (Tree member : tree.getTypeDecls()) { if (member instanceof ClassTree) { ClassTree classMember = (ClassTree) member;
@Override public void finished(TaskEvent e) { if (e.getKind() != TaskEvent.Kind.ANALYZE) return; if (processingEnv == null) { return; } TypeElement elem = e.getTypeElement(); for(Tree t : e.getCompilationUnit().getTypeDecls()) { if (t.getKind() == Tree.Kind.CLASS) { if (((JCClassDecl)t).sym.equals(elem)) { currentClass = (ClassTree)t; break; } } } if (currentClass != null) { verify(currentClass, elem); } }
@Override public Void visitCompilationUnit(CompilationUnitTree node, Void p) { for (Tree t : node.getTypeDecls()) { visit (t,p); } return null; }
private static VisitorState getCheckState(VisitorState state) { // Gets the VisitorState to start from when checking how to qualify the name. This won't work // correctly in all cases 1) it assumes there is only 1 top level type; 2) it doesn't look for // all of the locations where the symbol-to-be-replaced is used in the compilation unit. // Really, we should gather all of the usages first, and check them all. // It is assumed that this will work sufficiently until proven otherwise. CompilationUnitTree compilationUnit = state.getPath().getCompilationUnit(); if (compilationUnit.getTypeDecls().isEmpty()) { return state; } return state.withPath(TreePath.getPath(compilationUnit, compilationUnit.getTypeDecls().get(0))); }
public void run(CompilationController co) throws Exception { co.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = co.getCompilationUnit(); if(!cut.getTypeDecls().isEmpty()){ treePathHandle = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co); } } }, false);
private boolean isTopLevelClass(Tree tree, CompilationUnitTree root) { if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind()) || (tree.getKind() == Tree.Kind.EXPRESSION_STATEMENT && ((ExpressionStatementTree) tree).getExpression().getKind() == Tree.Kind.ERRONEOUS)) { for (Tree t : root.getTypeDecls()) { if (tree == t) { return true; } } } return false; }
@Override public Void visitCompilationUnit(CompilationUnitTree tree, Void d) { //ignore package X.Y.Z;: //scan(tree.getPackageDecl(), p); scan(tree.getImports(), d); scan(tree.getPackageAnnotations(), d); scan(tree.getTypeDecls(), d); return null; }
private CompilationUnitTree modifyCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index, Tree typeDeclaration, Operation op) { CompilationUnitTree copy = CompilationUnit( compilationUnit.getPackageAnnotations(), compilationUnit.getPackageName(), compilationUnit.getImports(), c(compilationUnit.getTypeDecls(), index, typeDeclaration, op), compilationUnit.getSourceFile() ); return copy; }
private CompilationUnitTree modifyPackageAnnotation(CompilationUnitTree cut, int index, AnnotationTree annotation, Operation op) { CompilationUnitTree copy = CompilationUnit( c(cut.getPackageAnnotations(), index, annotation, op), cut.getPackageName(), cut.getImports(), cut.getTypeDecls(), cut.getSourceFile() ); return copy; }
public R visitCompilationUnit(CompilationUnitTree node, P p) { R r = scan(node.getPackageAnnotations(), p); r = scanAndReduce(node.getPackageName(), p, r); r = scanAndReduce(node.getImports(), p, r); r = scanAndReduce(node.getTypeDecls(), p, r); return r; }
public R visitCompilationUnit(CompilationUnitTree node, P p) { R r = scan(node.getPackageAnnotations(), p); r = scanAndReduce(node.getPackageName(), p, r); r = scanAndReduce(node.getImports(), p, r); r = scanAndReduce(node.getTypeDecls(), p, r); return r; }
@Override public Tree visitCompilationUnit(CompilationUnitTree tree, Void p) { CompilationUnitTree n = make.CompilationUnit(tree.getPackage(), tree.getImports(), tree.getTypeDecls(), tree.getSourceFile()); model.setElement(n, model.getElement(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }