Search in sources :

Example 16 with DiagnosticCollector

use of javax.tools.DiagnosticCollector in project error-prone by google.

the class BugCheckerRefactoringTestHelper method doCompile.

private JCCompilationUnit doCompile(final JavaFileObject input, Iterable<JavaFileObject> files, Context context) throws IOException {
    JavacTool tool = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
    context.put(ErrorProneOptions.class, ErrorProneOptions.empty());
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, options, /*classes=*/
    null, files, context);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    task.analyze();
    JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(Iterables.filter(trees, JCCompilationUnit.class), compilationUnit -> compilationUnit.getSourceFile() == input));
    Iterable<Diagnostic<? extends JavaFileObject>> errorDiagnostics = Iterables.filter(diagnosticsCollector.getDiagnostics(), d -> d.getKind() == Diagnostic.Kind.ERROR);
    if (!Iterables.isEmpty(errorDiagnostics)) {
        fail("compilation failed unexpectedly: " + errorDiagnostics);
    }
    return tree;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Iterables(com.google.common.collect.Iterables) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) HashMap(java.util.HashMap) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) ErrorProneScannerTransformer(com.google.errorprone.scanner.ErrorProneScannerTransformer) ErrorProneScanner(com.google.errorprone.scanner.ErrorProneScanner) ImmutableList(com.google.common.collect.ImmutableList) CharStreams(com.google.common.io.CharStreams) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Fix(com.google.errorprone.fixes.Fix) DiagnosticCollector(javax.tools.DiagnosticCollector) Truth.assertAbout(com.google.common.truth.Truth.assertAbout) JavaFileObjects(com.google.testing.compile.JavaFileObjects) TreePath(com.sun.source.util.TreePath) BugChecker(com.google.errorprone.bugpatterns.BugChecker) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaSourceSubjectFactory.javaSource(com.google.testing.compile.JavaSourceSubjectFactory.javaSource) JavaFileObject(javax.tools.JavaFileObject) SourceFile(com.google.errorprone.apply.SourceFile) List(java.util.List) Description(com.google.errorprone.matchers.Description) Context(com.sun.tools.javac.util.Context) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaFileObject(javax.tools.JavaFileObject) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 17 with DiagnosticCollector

use of javax.tools.DiagnosticCollector 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 18 with DiagnosticCollector

use of javax.tools.DiagnosticCollector in project hibernate-orm by hibernate.

the class CompilationStatement method compile.

private void compile(List<File> sourceFiles) throws Exception {
    List<String> options = createJavaOptions();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(sourceFiles);
    compileSources(options, compiler, diagnostics, fileManager, compilationUnits);
    compilationDiagnostics.addAll(diagnostics.getDiagnostics());
    fileManager.close();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 19 with DiagnosticCollector

use of javax.tools.DiagnosticCollector in project androidannotations by androidannotations.

the class ProcessorTestHelper method compileFiles.

public CompileResult compileFiles(Collection<File> compilationUnits) {
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, null, null)) {
        CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, compilerOptions, null, fileManager.getJavaFileObjectsFromFiles(compilationUnits));
        List<Processor> processors = new ArrayList<>();
        for (Class<? extends Processor> processorClass : processorsClasses) {
            try {
                processors.add(processorClass.newInstance());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        task.setProcessors(processors);
        task.call();
    } catch (IOException e) {
    // we should always be able to close the manager
    }
    return new CompileResult(diagnosticCollector.getDiagnostics());
}
Also used : Processor(javax.annotation.processing.Processor) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) CompilationTask(javax.tools.JavaCompiler.CompilationTask) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 20 with DiagnosticCollector

use of javax.tools.DiagnosticCollector in project otter by alibaba.

the class JdkCompiler method compile.

public Class compile(JavaSource javaSource) {
    try {
        final DiagnosticCollector<JavaFileObject> errs = new DiagnosticCollector<JavaFileObject>();
        JdkCompileTask compileTask = new JdkCompileTask(new JdkCompilerClassLoader(this.getClass().getClassLoader()), options);
        String fullName = javaSource.getPackageName() + "." + javaSource.getClassName();
        Class newClass = compileTask.compile(fullName, javaSource.getSource(), errs);
        return newClass;
    } catch (JdkCompileException ex) {
        DiagnosticCollector<JavaFileObject> diagnostics = ex.getDiagnostics();
        throw new CompileExprException("compile error, source : \n" + javaSource + ", " + diagnostics.getDiagnostics(), ex);
    } catch (Exception ex) {
        throw new CompileExprException("compile error, source : \n" + javaSource, ex);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JdkCompilerClassLoader(com.alibaba.otter.shared.common.utils.compile.model.JdkCompilerClassLoader) DiagnosticCollector(javax.tools.DiagnosticCollector) CompileExprException(com.alibaba.otter.shared.common.utils.compile.exception.CompileExprException) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException) CompileExprException(com.alibaba.otter.shared.common.utils.compile.exception.CompileExprException) JdkCompileException(com.alibaba.otter.shared.common.utils.compile.exception.JdkCompileException)

Aggregations

DiagnosticCollector (javax.tools.DiagnosticCollector)28 JavaFileObject (javax.tools.JavaFileObject)28 JavaCompiler (javax.tools.JavaCompiler)24 StandardJavaFileManager (javax.tools.StandardJavaFileManager)16 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 Diagnostic (javax.tools.Diagnostic)9 CompilationTask (javax.tools.JavaCompiler.CompilationTask)9 File (java.io.File)8 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)8 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 ImmutableList (com.google.common.collect.ImmutableList)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)2 SourceFile (com.google.errorprone.apply.SourceFile)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2