Search in sources :

Example 6 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class RefasterRuleCompilerAnalyzer method finished.

@Override
public void finished(TaskEvent taskEvent) {
    if (taskEvent.getKind() != Kind.ANALYZE) {
        return;
    }
    if (JavaCompiler.instance(context).errorCount() > 0) {
        return;
    }
    ClassTree tree = JavacTrees.instance(context).getTree(taskEvent.getTypeElement());
    if (tree == null) {
        return;
    }
    Collection<? extends CodeTransformer> rules = RefasterRuleBuilderScanner.extractRules(tree, context);
    try (ObjectOutputStream output = new ObjectOutputStream(Files.newOutputStream(destinationPath))) {
        output.writeObject(CompositeCodeTransformer.compose(rules));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassTree(com.sun.source.tree.ClassTree) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 7 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class TemplateIntegrationTest method extractRefasterRule.

private CodeTransformer extractRefasterRule(JavaFileObject object) {
    compile(object);
    ClassTree classTree = Iterables.getOnlyElement(FluentIterable.from(compilationUnits).transformAndConcat(new Function<CompilationUnitTree, Iterable<? extends Tree>>() {

        @Override
        public Iterable<? extends Tree> apply(CompilationUnitTree input) {
            return input.getTypeDecls();
        }
    }).filter(ClassTree.class));
    return Iterables.getOnlyElement(RefasterRuleBuilderScanner.extractRules(classTree, context));
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) FluentIterable(com.google.common.collect.FluentIterable) ClassTree(com.sun.source.tree.ClassTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 8 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class ImmutableAnalysis method areFieldsImmutable.

/**
   * Check a single class' fields for immutability.
   *
   * @param immutableTyParams the in-scope immutable type parameters
   * @param classType the type to check the fields of
   */
Violation areFieldsImmutable(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType classType) {
    ClassSymbol classSym = (ClassSymbol) classType.tsym;
    if (classSym.members() == null) {
        return Violation.absent();
    }
    Filter<Symbol> instanceFieldFilter = new Filter<Symbol>() {

        @Override
        public boolean accepts(Symbol symbol) {
            return symbol.getKind() == ElementKind.FIELD && !symbol.isStatic();
        }
    };
    Map<Symbol, Tree> declarations = new HashMap<>();
    if (tree.isPresent()) {
        for (Tree member : tree.get().getMembers()) {
            Symbol sym = ASTHelpers.getSymbol(member);
            if (sym != null) {
                declarations.put(sym, member);
            }
        }
    }
    // javac gives us members in reverse declaration order
    // handling them in declaration order leads to marginally better diagnostics
    List<Symbol> members = ImmutableList.copyOf(classSym.members().getSymbols(instanceFieldFilter)).reverse();
    for (Symbol member : members) {
        Optional<Tree> memberTree = Optional.fromNullable(declarations.get(member));
        Violation info = isFieldImmutable(memberTree, immutableTyParams, classSym, classType, (VarSymbol) member);
        if (info.isPresent()) {
            return info;
        }
    }
    return Violation.absent();
}
Also used : Filter(com.sun.tools.javac.util.Filter) HashMap(java.util.HashMap) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) Symbol(com.sun.tools.javac.code.Symbol) TypeVariableSymbol(com.sun.tools.javac.code.Symbol.TypeVariableSymbol) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 9 with ClassTree

use of com.sun.source.tree.ClassTree in project error-prone by google.

the class RefasterScanner method visitClass.

@Override
public Void visitClass(ClassTree node, Context context) {
    Symbol sym = ASTHelpers.getSymbol(node);
    if (sym == null || !sym.getQualifiedName().contentEquals(rule().qualifiedTemplateClass())) {
        ListBuffer<JCStatement> statements = new ListBuffer<>();
        for (Tree tree : node.getMembers()) {
            if (tree instanceof JCStatement) {
                statements.append((JCStatement) tree);
            } else {
                tree.accept(this, context);
            }
        }
        scan(TreeMaker.instance(context).Block(0, statements.toList()), context);
    }
    return null;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ListBuffer(com.sun.tools.javac.util.ListBuffer) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) IfTree(com.sun.source.tree.IfTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 10 with ClassTree

use of com.sun.source.tree.ClassTree in project ceylon-compiler by ceylon.

the class T6404194 method main.

public static void main(String... args) throws IOException {
    class MyFileObject extends SimpleJavaFileObject {

        MyFileObject() {
            super(URI.create("myfo:///Test.java"), SOURCE);
        }

        @Override
        public String getCharContent(boolean ignoreEncodingErrors) {
            //      01234567890123456 7890 123456789012345
            return "@SuppressWarning(\"foo\") @Deprecated class Test { Test() { } }";
        }
    }
    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
    JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
    Trees trees = Trees.instance(task);
    CompilationUnitTree toplevel = task.parse().iterator().next();
    ClassTree classTree = (ClassTree) toplevel.getTypeDecls().get(0);
    List<? extends Tree> annotations = classTree.getModifiers().getAnnotations();
    Tree tree1 = annotations.get(0);
    Tree tree2 = annotations.get(1);
    long pos = trees.getSourcePositions().getStartPosition(toplevel, tree1);
    if (pos != 0)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree1);
    if (pos != 23)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree1, pos));
    pos = trees.getSourcePositions().getStartPosition(toplevel, tree2);
    if (pos != 24)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree2, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree2);
    if (pos != 35)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree2, pos));
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Trees(com.sun.source.util.Trees) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) JavacTask(com.sun.source.util.JavacTask)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)24 Tree (com.sun.source.tree.Tree)18 MethodTree (com.sun.source.tree.MethodTree)13 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)8 Symbol (com.sun.tools.javac.code.Symbol)7 ArrayList (java.util.ArrayList)6 VisitorState (com.google.errorprone.VisitorState)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)5 BlockTree (com.sun.source.tree.BlockTree)4 TreePath (com.sun.source.util.TreePath)4 ImmutableList (com.google.common.collect.ImmutableList)3 BinaryTree (com.sun.source.tree.BinaryTree)3 ExpressionTree (com.sun.source.tree.ExpressionTree)3 IdentifierTree (com.sun.source.tree.IdentifierTree)3 MemberSelectTree (com.sun.source.tree.MemberSelectTree)3 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)3 ReturnTree (com.sun.source.tree.ReturnTree)3 VariableTree (com.sun.source.tree.VariableTree)3 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)3