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()));
}
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));
}
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()));
}
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));
}
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;
}
Aggregations