Search in sources :

Example 56 with JavaCompiler

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

the class ErrorProneJavaCompilerTest method testMaturityResetsAfterOverride.

@Test
public void testMaturityResetsAfterOverride() throws Exception {
    DiagnosticTestHelper diagnosticHelper = new DiagnosticTestHelper();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, UTF_8), true);
    ErrorProneInMemoryFileManager fileManager = new ErrorProneInMemoryFileManager();
    JavaCompiler errorProneJavaCompiler = new ErrorProneJavaCompiler();
    List<String> args = Lists.newArrayList("-d", tempDir.getRoot().getAbsolutePath(), "-proc:none", "-Xep:EmptyIf");
    List<JavaFileObject> sources = fileManager.forResources(BadShiftAmount.class, "testdata/EmptyIfStatementPositiveCases.java");
    fileManager.close();
    JavaCompiler.CompilationTask task = errorProneJavaCompiler.getTask(printWriter, null, diagnosticHelper.collector, args, null, sources);
    boolean succeeded = task.call();
    assertThat(succeeded).isFalse();
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticMessage(containsString("[EmptyIf]")));
    assertTrue(matcher.matches(diagnosticHelper.getDiagnostics()));
    diagnosticHelper.clearDiagnostics();
    args.remove("-Xep:EmptyIf");
    task = errorProneJavaCompiler.getTask(printWriter, null, diagnosticHelper.collector, args, null, sources);
    fileManager.close();
    succeeded = task.call();
    assertThat(succeeded).isTrue();
    assertThat(diagnosticHelper.getDiagnostics()).isEmpty();
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) JavaFileObject(javax.tools.JavaFileObject) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 57 with JavaCompiler

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

the class ErrorProneJavaCompilerTest method testSeverityResetsAfterOverride.

@Test
public void testSeverityResetsAfterOverride() throws Exception {
    DiagnosticTestHelper diagnosticHelper = new DiagnosticTestHelper();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, UTF_8), true);
    ErrorProneInMemoryFileManager fileManager = new ErrorProneInMemoryFileManager();
    JavaCompiler errorProneJavaCompiler = new ErrorProneJavaCompiler();
    List<String> args = Lists.newArrayList("-d", tempDir.getRoot().getAbsolutePath(), "-proc:none", "-Xep:ChainingConstructorIgnoresParameter:WARN");
    List<JavaFileObject> sources = fileManager.forResources(ChainingConstructorIgnoresParameter.class, "testdata/ChainingConstructorIgnoresParameterPositiveCases.java");
    fileManager.close();
    JavaCompiler.CompilationTask task = errorProneJavaCompiler.getTask(printWriter, fileManager, diagnosticHelper.collector, args, null, sources);
    boolean succeeded = task.call();
    assertThat(succeeded).isTrue();
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticMessage(containsString("[ChainingConstructorIgnoresParameter]")));
    assertTrue(matcher.matches(diagnosticHelper.getDiagnostics()));
    // reset state between compilations
    diagnosticHelper.clearDiagnostics();
    fileManager = new ErrorProneInMemoryFileManager();
    sources = fileManager.forResources(ChainingConstructorIgnoresParameter.class, "testdata/ChainingConstructorIgnoresParameterPositiveCases.java");
    fileManager.close();
    args.remove("-Xep:ChainingConstructorIgnoresParameter:WARN");
    task = errorProneJavaCompiler.getTask(printWriter, fileManager, diagnosticHelper.collector, args, null, sources);
    succeeded = task.call();
    assertThat(succeeded).isFalse();
    assertTrue(matcher.matches(diagnosticHelper.getDiagnostics()));
}
Also used : JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) JavaFileObject(javax.tools.JavaFileObject) ChainingConstructorIgnoresParameter(com.google.errorprone.bugpatterns.ChainingConstructorIgnoresParameter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 58 with JavaCompiler

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

the class CompilationTestHelper method checkWellFormed.

private void checkWellFormed(Iterable<JavaFileObject> sources, String[] args) {
    JavaCompiler compiler = JavacTool.create();
    OutputStream outputStream = new ByteArrayOutputStream();
    String[] remainingArgs = null;
    try {
        remainingArgs = ErrorProneOptions.processArgs(args).getRemainingArgs();
    } catch (InvalidCommandLineOptionException e) {
        fail("Exception during argument processing: " + e);
    }
    CompilationTask task = compiler.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, UTF_8)), /*autoFlush=*/
    true), fileManager, null, buildArguments(Arrays.asList(remainingArgs)), null, sources);
    boolean result = task.call();
    assertWithMessage(String.format("Test program failed to compile with non Error Prone error: %s", outputStream.toString())).that(result).isTrue();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) JavaCompiler(javax.tools.JavaCompiler) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompilationTask(javax.tools.JavaCompiler.CompilationTask) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 59 with JavaCompiler

use of javax.tools.JavaCompiler 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 60 with JavaCompiler

use of javax.tools.JavaCompiler 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)

Aggregations

JavaCompiler (javax.tools.JavaCompiler)101 StandardJavaFileManager (javax.tools.StandardJavaFileManager)43 JavaFileObject (javax.tools.JavaFileObject)39 File (java.io.File)31 DiagnosticCollector (javax.tools.DiagnosticCollector)24 ArrayList (java.util.ArrayList)22 JavacTask (com.sun.source.util.JavacTask)20 IOException (java.io.IOException)17 CompilationTask (javax.tools.JavaCompiler.CompilationTask)16 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)15 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)11 Diagnostic (javax.tools.Diagnostic)11 PrintWriter (java.io.PrintWriter)9 Test (org.junit.Test)9 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 FileOutputStream (java.io.FileOutputStream)5 StringWriter (java.io.StringWriter)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStream (java.io.OutputStream)4