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