Search in sources :

Example 21 with ClassTree

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

the class PrivateConstructorForUtilityClass method isInPrivateScope.

private static boolean isInPrivateScope(VisitorState state) {
    TreePath treePath = state.getPath();
    do {
        Tree currentLeaf = treePath.getLeaf();
        if (ClassTree.class.isInstance(currentLeaf)) {
            ClassTree currentClassTree = (ClassTree) currentLeaf;
            if (currentClassTree.getModifiers().getFlags().contains(PRIVATE)) {
                return true;
            }
        }
        treePath = treePath.getParentPath();
    } while (treePath != null);
    return false;
}
Also used : TreePath(com.sun.source.util.TreePath) ClassTree(com.sun.source.tree.ClassTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) BlockTree(com.sun.source.tree.BlockTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree)

Example 22 with ClassTree

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

the class T6654037 method main.

public static void main(String[] args) throws Exception {
    //NOI18N
    final String bootPath = System.getProperty("sun.boot.class.path");
    final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    assert tool != null;
    String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";
    JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
    CompilationUnitTree cut = ct.parse().iterator().next();
    ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
    MethodTree method = (MethodTree) clazz.getMembers().get(0);
    VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
    BinaryTree cond = (BinaryTree) condSt.getInitializer();
    JCTree condJC = (JCTree) cond;
    if (condJC.pos != 93)
        throw new IllegalStateException("Unexpected position=" + condJC.pos);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) BinaryTree(com.sun.source.tree.BinaryTree) JavaCompiler(javax.tools.JavaCompiler) JCTree(com.sun.tools.javac.tree.JCTree)

Example 23 with ClassTree

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

the class T6665791 method main.

public static void main(String[] args) throws Exception {
    write(test_java, test);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> units = manager.getJavaFileObjects(test_java);
    final StringWriter sw = new StringWriter();
    JavacTask task = (JavacTask) compiler.getTask(sw, manager, null, null, null, units);
    new TreeScanner<Boolean, Void>() {

        @Override
        public Boolean visitClass(ClassTree arg0, Void arg1) {
            sw.write(arg0.toString());
            return super.visitClass(arg0, arg1);
        }
    }.scan(task.parse(), null);
    System.out.println("output:");
    System.out.println(sw.toString());
    String found = sw.toString().replaceAll("\\s+", " ").trim();
    String expect = test.replaceAll("\\s+", " ").trim();
    if (!expect.equals(found)) {
        System.out.println("expect: " + expect);
        System.out.println("found:  " + found);
        throw new Exception("unexpected output");
    }
}
Also used : StringWriter(java.io.StringWriter) ClassTree(com.sun.source.tree.ClassTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) IOException(java.io.IOException)

Example 24 with ClassTree

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

the class T6402077 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) {
            //      0123456789012345678901234
            return "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();
    Tree tree = ((ClassTree) toplevel.getTypeDecls().get(0)).getMembers().get(0);
    long pos = trees.getSourcePositions().getStartPosition(toplevel, tree);
    if (pos != 13)
        throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree, pos));
    pos = trees.getSourcePositions().getEndPosition(toplevel, tree);
    if (pos != 23)
        throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree, pos));
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Trees(com.sun.source.util.Trees) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) 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