@Override protected Iterable<? extends AnnotationTree> getChildNodes(T tree, VisitorState state) { if (tree instanceof ClassTree) { return ((ClassTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof VariableTree) { return ((VariableTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof MethodTree) { return ((MethodTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof CompilationUnitTree) { return ((CompilationUnitTree) tree).getPackageAnnotations(); } else if (tree instanceof AnnotatedTypeTree) { return ((AnnotatedTypeTree) tree).getAnnotations(); } else if (tree instanceof PackageTree) { return ((PackageTree) tree).getAnnotations(); } else { throw new IllegalArgumentException( "Cannot access annotations from tree of type " + tree.getClass()); } } }
private static void analyzePackageAnnotations( final CompilationUnitTree cut, final Source src, final EndPosTable endPosTable) { java.util.List<? extends AnnotationTree> annotations = cut.getPackageAnnotations(); for (final AnnotationTree at : annotations) { analyzeAnnotationTree(src, endPosTable, at); } }
@Override public String visitCompilationUnit(CompilationUnitTree node, Void v) { ident = 0; if (!node.getPackageAnnotations().isEmpty()) { throw new AssertionError("Annotations on packages not supported"); } ExpressionTree packageName = node.getPackageName(); if (packageName != null) { output.append("package "); output.append(packageName); output.append("\n\n"); } output.append("uses java.lang.*\n"); List<? extends ImportTree> imports = node.getImports(); for (ImportTree imp : imports) { output.append(imp.accept(this, v)); } output.append("\n"); List<? extends Tree> typeDecls = node.getTypeDecls(); for (Tree typeDecl : typeDecls) { output.append(typeDecl.accept(this, v)); } return output.toString(); }
@Override public Void visitCompilationUnit(CompilationUnitTree node, Void p) { scan(node.getPackageAnnotations(), p); return scan(node.getTypeDecls(), p); } }.scan(cut, null);
if( !node.getPackageAnnotations().isEmpty() )
@Override public Void visitCompilationUnit(CompilationUnitTree expected, Tree actual) { Optional<CompilationUnitTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } parallelScan(expected.getPackageAnnotations(), other.get().getPackageAnnotations()); scan(expected.getPackageName(), other.get().getPackageName()); parallelScan(expected.getImports(), other.get().getImports()); parallelScan(expected.getTypeDecls(), other.get().getTypeDecls()); return null; }
@Override public Void visitCompilationUnit(CompilationUnitTree expected, Tree actual) { Optional<CompilationUnitTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } parallelScan(expected.getPackageAnnotations(), other.get().getPackageAnnotations()); scan(expected.getPackageName(), other.get().getPackageName()); parallelScan( expected.getImports(), filter.filterImports( ImmutableList.copyOf(expected.getImports()), ImmutableList.copyOf(other.get().getImports()))); parallelScan(expected.getTypeDecls(), other.get().getTypeDecls()); return null; }
@Override public Void visitCompilationUnit(CompilationUnitTree expected, Tree actual) { Optional<CompilationUnitTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } parallelScan(expected.getPackageAnnotations(), other.get().getPackageAnnotations()); scan(expected.getPackageName(), other.get().getPackageName()); parallelScan( expected.getImports(), filter.filterImports( ImmutableList.copyOf(expected.getImports()), ImmutableList.copyOf(other.get().getImports()))); parallelScan(expected.getTypeDecls(), other.get().getTypeDecls()); return null; }
@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; }
@Override public Void visitCompilationUnit(CompilationUnitTree tree, EnumSet<UseTypes> d) { //ignore package X.Y.Z;: //scan(tree.getPackageDecl(), p); tl.moveBefore(tree.getImports()); scan(tree.getImports(), d); tl.moveBefore(tree.getPackageAnnotations()); scan(tree.getPackageAnnotations(), d); tl.moveToEnd(tree.getImports()); scan(tree.getTypeDecls(), d); return null; }
private CompilationUnitTree modifyCompUnitImport(CompilationUnitTree compilationUnit, int index, ImportTree importt, Operation op) { CompilationUnitTree copy = CompilationUnit( compilationUnit.getPackageAnnotations(), compilationUnit.getPackageName(), c(compilationUnit.getImports(), index, importt, op), compilationUnit.getTypeDecls(), compilationUnit.getSourceFile() ); return copy; }
@Override protected Iterable<? extends AnnotationTree> getChildNodes(T tree, VisitorState state) { if (tree instanceof ClassTree) { return ((ClassTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof VariableTree) { return ((VariableTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof MethodTree) { return ((MethodTree) tree).getModifiers().getAnnotations(); } else if (tree instanceof CompilationUnitTree) { return ((CompilationUnitTree) tree).getPackageAnnotations(); } else if (tree instanceof AnnotatedTypeTree) { return ((AnnotatedTypeTree) tree).getAnnotations(); } else if (tree instanceof PackageTree) { return ((PackageTree) tree).getAnnotations(); } else { throw new IllegalArgumentException( "Cannot access annotations from tree of type " + tree.getClass()); } } }
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; }
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; }
protected final CompilationUnitTree rewriteChildren(CompilationUnitTree tree) { ExpressionTree pid = (ExpressionTree)translate(tree.getPackageName()); importAnalysis.setCompilationUnit(tree); importAnalysis.setPackage(tree.getPackageName()); List<? extends ImportTree> imps = translate(tree.getImports()); importAnalysis.setImports(imps); List<? extends AnnotationTree> annotations = translate(tree.getPackageAnnotations()); List<? extends Tree> types = translate(tree.getTypeDecls()); Set<? extends Element> newImports = importAnalysis.getImports(); if (copy != null && newImports != null && !newImports.isEmpty()) { imps = GeneratorUtilities.get(copy).addImports(tree, newImports).getImports(); } if (!annotations.equals(tree.getPackageAnnotations()) || pid!=tree.getPackageName() || !imps.equals(tree.getImports()) || !types.equals(tree.getTypeDecls())) { CompilationUnitTree n = make.CompilationUnit(annotations, pid, imps, types, tree.getSourceFile()); model.setElement(n, model.getElement(tree)); copyCommentTo(tree,n); model.setPos(n, model.getPos(tree)); tree = n; } return tree; }
/** * Create a new CompilationUnitTree from a template. * * @param sourceRoot a source root under which the new file is created * @param path a relative path to file separated by '/' * @param kind the kind of Element to use for the template, can be null or * CLASS, INTERFACE, ANNOTATION_TYPE, ENUM, PACKAGE * @return new CompilationUnitTree created from a template * @throws IOException when an exception occurs while creating the template * @since 0.101 */ public CompilationUnitTree createFromTemplate(FileObject sourceRoot, String path, ElementKind kind) throws IOException { String[] nameComponent = FileObjects.getFolderAndBaseName(path, '/'); JavaFileObject sourceFile = FileObjects.templateFileObject(sourceRoot, nameComponent[0], nameComponent[1]); FileObject template = FileUtil.getConfigFile(copy.template(kind)); FileObject targetFile = copy.doCreateFromTemplate(template, sourceFile); CompilationUnitTree templateCUT = copy.impl.getJavacTask().parse(FileObjects.sourceFileObject(targetFile, targetFile.getParent())).iterator().next(); CompilationUnitTree importComments = GeneratorUtilities.get(copy).importComments(templateCUT, templateCUT); CompilationUnitTree result = copy.getTreeMaker().CompilationUnit(importComments.getPackageAnnotations(), sourceRoot, path, importComments.getImports(), importComments.getTypeDecls()); return result; }