Search in sources :

Example 6 with CompilationUnitTree

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

the class ClassName method matchCompilationUnit.

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
    if (tree.getTypeDecls().isEmpty() || tree.getPackageName() == null) {
        return Description.NO_MATCH;
    }
    String filename = Files.getNameWithoutExtension(tree.getSourceFile().getName());
    List<String> names = new ArrayList<>();
    for (Tree member : tree.getTypeDecls()) {
        if (member instanceof ClassTree) {
            ClassTree classMember = (ClassTree) member;
            if (isSuppressed(classMember)) {
                // and @SuppressWarnings can't be applied to packages.
                return Description.NO_MATCH;
            }
            if (classMember.getSimpleName().contentEquals(filename)) {
                return Description.NO_MATCH;
            }
            if (classMember.getModifiers().getFlags().contains(Modifier.PUBLIC)) {
                // the error.
                return Description.NO_MATCH;
            }
            names.add(classMember.getSimpleName().toString());
        }
    }
    String message = String.format("Expected a class declaration named %s inside %s.java, instead found: %s", filename, filename, Joiner.on(", ").join(names));
    return buildDescription(tree.getPackageName()).setMessage(message).build();
}
Also used : ArrayList(java.util.ArrayList) 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 7 with CompilationUnitTree

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

the class GuardedByBinderTest method bind.

private String bind(String className, String exprString, JavaFileObject fileObject) {
    JavaCompiler javaCompiler = JavacTool.create();
    JavacTaskImpl task = (JavacTaskImpl) javaCompiler.getTask(new PrintWriter(System.err, true), fileManager, null, Collections.<String>emptyList(), null, Arrays.asList(fileObject));
    Iterable<? extends CompilationUnitTree> compilationUnits = task.parse();
    task.analyze();
    for (CompilationUnitTree compilationUnit : compilationUnits) {
        FindClass finder = new FindClass();
        finder.visitTopLevel((JCTree.JCCompilationUnit) compilationUnit);
        for (JCTree.JCClassDecl classDecl : finder.decls) {
            if (classDecl.getSimpleName().contentEquals(className)) {
                Optional<GuardedByExpression> guardExpression = GuardedByBinder.bindString(exprString, GuardedBySymbolResolver.from(ASTHelpers.getSymbol(classDecl), compilationUnit, task.getContext(), null));
                if (!guardExpression.isPresent()) {
                    throw new IllegalGuardedBy(exprString);
                }
                return guardExpression.get().debugPrint();
            }
        }
    }
    throw new AssertionError("Couldn't find a class with the given name: " + className);
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) JCTree(com.sun.tools.javac.tree.JCTree) PrintWriter(java.io.PrintWriter)

Example 8 with CompilationUnitTree

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

the class T6598108 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;
    final JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject()));
    CompilationUnitTree cut = ct.parse().iterator().next();
    TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
    Scope s = Trees.instance(ct).getScope(tp);
    TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
    if (Trees.instance(ct).isAccessible(s, type)) {
        //"false" would be expected here.
        throw new IllegalStateException("");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) TreePath(com.sun.source.util.TreePath) Scope(com.sun.source.tree.Scope) TypeElement(javax.lang.model.element.TypeElement) JavaCompiler(javax.tools.JavaCompiler) JavacTask(com.sun.source.util.JavacTask)

Example 9 with CompilationUnitTree

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

the class T6557752 method main.

public static void main(String[] args) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    Iterable<? extends CompilationUnitTree> asts = task.parse();
    task.analyze();
    trees = Trees.instance(task);
    MyVisitor myVisitor = new MyVisitor();
    for (CompilationUnitTree ast : asts) {
        myVisitor.compilationUnit = ast;
        myVisitor.scan(ast, null);
    }
    if (!myVisitor.foundError) {
        throw new AssertionError("Expected error not found!");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler)

Example 10 with CompilationUnitTree

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

the class GenStubs method run.

boolean run(String sourcepath, File outdir, List<String> classes) {
    //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
    if (sourcepath == null)
        throw new IllegalArgumentException("sourcepath not set");
    if (outdir == null)
        throw new IllegalArgumentException("source output dir not set");
    JavacTool tool = JavacTool.create();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    try {
        fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
        fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
        List<JavaFileObject> files = new ArrayList<JavaFileObject>();
        for (String c : classes) {
            JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
            if (fo == null)
                error("class not found: " + c);
            else
                files.add(fo);
        }
        JavacTask t = tool.getTask(null, fm, null, null, null, files);
        Iterable<? extends CompilationUnitTree> trees = t.parse();
        for (CompilationUnitTree tree : trees) {
            makeStub(fm, tree);
        }
    } catch (IOException e) {
        error("IO error " + e, e);
    }
    return (errors == 0);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)32 JavacTask (com.sun.source.util.JavacTask)11 JavaCompiler (javax.tools.JavaCompiler)11 ClassTree (com.sun.source.tree.ClassTree)9 Tree (com.sun.source.tree.Tree)9 ArrayList (java.util.ArrayList)8 JavaFileObject (javax.tools.JavaFileObject)8 IOException (java.io.IOException)7 JavacTool (com.sun.tools.javac.api.JavacTool)5 JCTree (com.sun.tools.javac.tree.JCTree)5 Trees (com.sun.source.util.Trees)4 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 MethodTree (com.sun.source.tree.MethodTree)3 VariableTree (com.sun.source.tree.VariableTree)3 TreeScanner (com.sun.source.util.TreeScanner)3 File (java.io.File)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 StandardJavaFileManager (javax.tools.StandardJavaFileManager)3 ImmutableList (com.google.common.collect.ImmutableList)2 InputFile (com.google.devtools.j2objc.file.InputFile)2