Search in sources :

Example 21 with Diagnostic

use of javax.tools.Diagnostic 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 22 with Diagnostic

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

the class ErrorProneCompilerIntegrationTest method ignoreGeneratedSuperInvocations.

// TODO(cushon) - how can we distinguish between synthetic super() calls and real ones?
@Ignore
@Test
public void ignoreGeneratedSuperInvocations() throws Exception {
    compilerBuilder.report(ScannerSupplier.fromBugCheckerClasses(SuperCallMatcher.class));
    compiler = compilerBuilder.build();
    Result exitCode = compiler.compile(Arrays.asList(compiler.fileManager().forSourceLines("Test.java", "public class Test {", "  public Test() {}", "}")));
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = not(hasItem(diagnosticMessage(containsString("[SuperCallMatcher]"))));
    assertTrue("Warning should be found. " + diagnosticHelper.describe(), matcher.matches(diagnosticHelper.getDiagnostics()));
    assertThat(outputStream.toString(), exitCode, is(Result.OK));
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Diagnostic(javax.tools.Diagnostic) Result(com.sun.tools.javac.main.Main.Result) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 23 with Diagnostic

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

the class ErrorProneCompilerIntegrationTest method unhandledExceptionsAreReportedWithoutBugParadeLink.

@Test
public void unhandledExceptionsAreReportedWithoutBugParadeLink() throws Exception {
    compilerBuilder.report(ScannerSupplier.fromBugCheckerClasses(Throwing.class));
    compiler = compilerBuilder.build();
    Result exitCode = compiler.compile(compiler.fileManager().forResources(getClass(), "testdata/MultipleTopLevelClassesWithErrors.java", "testdata/ExtendedMultipleTopLevelClassesWithErrors.java"));
    assertThat(outputStream.toString(), exitCode, is(Result.ERROR));
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticMessage(CoreMatchers.<String>allOf(containsString("IllegalStateException: test123"), containsString("unhandled exception was thrown by the Error Prone"))));
    assertTrue("Error should be reported. " + diagnosticHelper.describe(), matcher.matches(diagnosticHelper.getDiagnostics()));
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Diagnostic(javax.tools.Diagnostic) Matchers.containsString(org.hamcrest.Matchers.containsString) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 24 with Diagnostic

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

the class ErrorProneCompilerIntegrationTest method flagEnablesCheck.

@Test
public void flagEnablesCheck() throws Exception {
    String[] testFile = { "public class Test {", "  public Test() {", "    if (true);", "  }", "}" };
    Result exitCode = compiler.compile(Arrays.asList(compiler.fileManager().forSourceLines("Test.java", testFile)));
    outputStream.flush();
    assertThat(diagnosticHelper.getDiagnostics()).isEmpty();
    assertThat(outputStream.toString(), exitCode, is(Result.OK));
    String[] args = { "-Xep:EmptyIf" };
    exitCode = compiler.compile(args, Arrays.asList(compiler.fileManager().forSourceLines("Test.java", testFile)));
    outputStream.flush();
    Matcher<? super Iterable<Diagnostic<? extends JavaFileObject>>> matcher = hasItem(diagnosticMessage(containsString("[EmptyIf]")));
    assertTrue("Error should be found. " + diagnosticHelper.describe(), matcher.matches(diagnosticHelper.getDiagnostics()));
    assertThat(outputStream.toString(), exitCode, is(Result.ERROR));
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Diagnostic(javax.tools.Diagnostic) Matchers.containsString(org.hamcrest.Matchers.containsString) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 25 with Diagnostic

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

Aggregations

Diagnostic (javax.tools.Diagnostic)31 JavaFileObject (javax.tools.JavaFileObject)30 Test (org.junit.Test)13 JavaCompiler (javax.tools.JavaCompiler)11 Result (com.sun.tools.javac.main.Main.Result)10 DiagnosticCollector (javax.tools.DiagnosticCollector)9 File (java.io.File)7 IOException (java.io.IOException)7 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)7 PrintWriter (java.io.PrintWriter)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 ArrayList (java.util.ArrayList)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DiagnosticListener (javax.tools.DiagnosticListener)4 ImmutableList (com.google.common.collect.ImmutableList)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 JavacTask (com.sun.source.util.JavacTask)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2