Search in sources :

Example 1 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.

the class CodeTransformerTestHelper method transform.

public JavaFileObject transform(JavaFileObject original) {
    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(original));
    try {
        SourceFile sourceFile = SourceFile.create(original);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(trees, JCCompilationUnit.class));
        DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, ImportOrganizer.STATIC_FIRST_ORGANIZER);
        transformer().apply(new TreePath(tree), task.getContext(), diff);
        diff.applyDifferences(sourceFile);
        return JavaFileObjects.forSourceString(Iterables.getOnlyElement(Iterables.filter(tree.getTypeDecls(), JCClassDecl.class)).sym.getQualifiedName().toString(), sourceFile.getSourceText());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile)

Example 2 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl 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(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), 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) BufferedWriter(java.io.BufferedWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 3 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.

the class T6330997 method main.

public static void main(String... args) {
    increaseMajor("T1.class", 1);
    increaseMajor("T2.class", 2);
    javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
    JavaCompiler compiler = JavaCompiler.instance(task.getContext());
    try {
        compiler.resolveIdent("T1").complete();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed: unexpected exception while reading class T1");
    }
    try {
        compiler.resolveIdent("T2").complete();
    } catch (BadClassFile e) {
        System.err.println("Passed: expected completion failure " + e.getClass().getName());
        return;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failed: unexpected exception while reading class T2");
    }
    throw new RuntimeException("Failed: no error reported");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) BadClassFile(com.sun.tools.javac.jvm.ClassReader.BadClassFile) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 4 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.

the class TestResolveIdent method main.

public static void main(String[] args) throws IOException {
    javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
    JavaCompiler compiler = JavaCompiler.instance(task.getContext());
    System.out.println(compiler.resolveIdent(getDeprecatedClass().getCanonicalName()));
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Example 5 with JavacTaskImpl

use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.

the class T6435291 method main.

public static void main(String... args) {
    javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
    JavaCompiler compiler = JavaCompiler.instance(task.getContext());
    try {
        compiler.resolveIdent("T").complete();
    } catch (BadClassFile e) {
        System.err.println("Passed: expected completion failure " + e.getClass().getName());
        return;
    } catch (Exception e) {
        throw new RuntimeException("Failed: unexpected exception");
    }
    throw new RuntimeException("Failed: no error reported");
}
Also used : JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) BadClassFile(com.sun.tools.javac.jvm.ClassReader.BadClassFile) JavaCompiler(com.sun.tools.javac.main.JavaCompiler)

Aggregations

JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)29 JavaCompiler (javax.tools.JavaCompiler)10 JavaCompiler (com.sun.tools.javac.main.JavaCompiler)8 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)7 IOException (java.io.IOException)5 JavaFileObject (javax.tools.JavaFileObject)5 StandardJavaFileManager (javax.tools.StandardJavaFileManager)5 VariableTree (com.sun.source.tree.VariableTree)4 BadClassFile (com.sun.tools.javac.jvm.ClassReader.BadClassFile)4 Context (com.sun.tools.javac.util.Context)4 Element (javax.lang.model.element.Element)4 TypeElement (javax.lang.model.element.TypeElement)4 DiagnosticCollector (javax.tools.DiagnosticCollector)4 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)3 SourceFile (com.google.errorprone.apply.SourceFile)3 JCTree (com.sun.tools.javac.tree.JCTree)3 File (java.io.File)3 BinaryTree (com.sun.source.tree.BinaryTree)2 ClassTree (com.sun.source.tree.ClassTree)2 MethodTree (com.sun.source.tree.MethodTree)2