Search in sources :

Example 21 with CompilationUnitTree

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

the class SimpleDateFormatConstant method threadLocalFix.

private static Fix threadLocalFix(VariableTree tree, VisitorState state, final VarSymbol sym) {
    SuggestedFix.Builder fix = SuggestedFix.builder().replace(tree.getType(), String.format("ThreadLocal<%s>", state.getSourceForNode(tree.getType()))).prefixWith(tree.getInitializer(), "ThreadLocal.withInitial(() -> ").postfixWith(tree.getInitializer(), ")");
    CompilationUnitTree unit = state.getPath().getCompilationUnit();
    unit.accept(new TreeScanner<Void, Void>() {

        @Override
        public Void visitIdentifier(IdentifierTree tree, Void unused) {
            if (Objects.equals(ASTHelpers.getSymbol(tree), sym)) {
                fix.postfixWith(tree, ".get()");
            }
            return null;
        }
    }, null);
    return fix.build();
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) IdentifierTree(com.sun.source.tree.IdentifierTree)

Example 22 with CompilationUnitTree

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

the class CompilerBasedTest method compile.

protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
    JavaCompiler compiler = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, ImmutableList.<String>of(), null, ImmutableList.of(fileObject));
    try {
        this.sourceFile = SourceFile.create(fileObject);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        for (CompilationUnitTree tree : trees) {
            scanner.scan((JCCompilationUnit) tree);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    this.context = task.getContext();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) IOException(java.io.IOException)

Example 23 with CompilationUnitTree

use of com.sun.source.tree.CompilationUnitTree in project j2objc by google.

the class JavacParser method parseFiles.

@Override
public void parseFiles(Collection<String> paths, Handler handler, SourceVersion sourceVersion) {
    List<File> files = new ArrayList<>();
    for (String path : paths) {
        files.add(new File(path));
    }
    try {
        JavacEnvironment env = createEnvironment(files, null, false);
        List<CompilationUnitTree> units = new ArrayList<>();
        for (CompilationUnitTree unit : env.task().parse()) {
            units.add(unit);
        }
        env.task().analyze();
        processDiagnostics(env.diagnostics());
        if (ErrorUtil.errorCount() == 0) {
            for (CompilationUnitTree ast : units) {
                com.google.devtools.j2objc.ast.CompilationUnit unit = TreeConverter.convertCompilationUnit(options, env, (JCTree.JCCompilationUnit) ast);
                processDiagnostics(env.diagnostics());
                handler.handleParsedUnit(unit.getSourceFilePath(), unit);
            }
        }
    } catch (IOException e) {
        ErrorUtil.fatalError(e, "javac file manager error");
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) JCTree(com.sun.tools.javac.tree.JCTree) IOException(java.io.IOException) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) InputFile(com.google.devtools.j2objc.file.InputFile) File(java.io.File)

Example 24 with CompilationUnitTree

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

the class Test method test.

void test(File test) throws Exception {
    JavacTool tool1 = JavacTool.create();
    StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
    // parse test file into a tree, and write it out to a stringbuffer using Pretty
    JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    Iterable<? extends CompilationUnitTree> trees = t1.parse();
    for (CompilationUnitTree tree : trees) {
        new Pretty(pw, true).printExpr((JCTree) tree);
    }
    pw.close();
    final String out = sw.toString();
    System.err.println("generated code:\n" + out + "\n");
    // verify the generated code is valid Java by compiling it
    JavacTool tool2 = JavacTool.create();
    JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return out;
        }
    };
    JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
    boolean ok = t2.call();
    if (!ok)
        throw new Exception("compilation of generated code failed");
    File expectedClass = new File(test.getName().replace(".java", ".class"));
    if (!expectedClass.exists())
        throw new Exception(expectedClass + " not found");
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavacTool(com.sun.tools.javac.api.JavacTool) Pretty(com.sun.tools.javac.tree.Pretty) JavacTask(com.sun.source.util.JavacTask)

Example 25 with CompilationUnitTree

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

the class T6963934 method main.

public static void main(String[] args) throws Exception {
    File testSrc = new File(System.getProperty("test.src"));
    File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
    CompilationUnitTree tree = task.parse().iterator().next();
    int count = 0;
    for (ImportTree importTree : tree.getImports()) {
        System.out.println(importTree);
        count++;
    }
    int expected = 7;
    if (count != expected)
        throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) JavacTask(com.sun.source.util.JavacTask) File(java.io.File) ImportTree(com.sun.source.tree.ImportTree)

Aggregations

CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)30 JavacTask (com.sun.source.util.JavacTask)11 JavaCompiler (javax.tools.JavaCompiler)11 ClassTree (com.sun.source.tree.ClassTree)8 Tree (com.sun.source.tree.Tree)8 ArrayList (java.util.ArrayList)8 JavaFileObject (javax.tools.JavaFileObject)8 IOException (java.io.IOException)6 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 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 RegularInputFile (com.google.devtools.j2objc.file.RegularInputFile)2