use of com.sun.source.util.JavacTask in project bazel by bazelbuild.
the class JavacTurbineTest method compileLib.
private void compileLib(Path jar, Collection<Path> classpath, Iterable<? extends JavaFileObject> units) throws IOException {
final Path outdir = temp.newFolder().toPath();
JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8);
fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(outdir));
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
List<String> options = Arrays.asList("-d", outdir.toString());
JavacTool tool = JavacTool.create();
JavacTask task = tool.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fm, null, options, null, units);
assertThat(task.call()).isTrue();
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
Files.walkFileTree(outdir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
JarEntry je = new JarEntry(outdir.relativize(path).toString());
jos.putNextEntry(je);
Files.copy(path, jos);
return FileVisitResult.CONTINUE;
}
});
}
}
use of com.sun.source.util.JavacTask in project jdk8u_jdk by JetBrains.
the class FDTest method run.
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
JavacTask ct = (JavacTask) tool.getTask(null, fm, diagChecker, null, null, Arrays.asList(source));
try {
ct.analyze();
} catch (Throwable ex) {
fail("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
}
check();
}
use of com.sun.source.util.JavacTask in project j2objc by google.
the class JavacParser method parse.
@Override
public CompilationUnit parse(String mainType, String path, String source) {
try {
JavacEnvironment parserEnv = createEnvironment(path, source);
JavacTask task = parserEnv.task();
CompilationUnitTree unit = task.parse().iterator().next();
task.analyze();
processDiagnostics(parserEnv.diagnostics());
return TreeConverter.convertCompilationUnit(options, parserEnv, unit);
} catch (IOException e) {
ErrorUtil.fatalError(e, path);
}
return null;
}
use of com.sun.source.util.JavacTask in project error-prone by google.
the class ErrorProneJavacPluginTest method noPolicyGiven.
@Test
public void noPolicyGiven() throws IOException {
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path source = fileSystem.getPath("Test.java");
Files.write(source, "class Test {}".getBytes(UTF_8));
JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
StringWriter sw = new StringWriter();
JavacTask task = JavacTool.create().getTask(new PrintWriter(sw, true), fileManager, diagnosticCollector, ImmutableList.of("-Xplugin:ErrorProne"), ImmutableList.of(), fileManager.getJavaFileObjects(source));
try {
task.call();
fail();
} catch (Throwable expected) {
assertThat(expected).hasMessageThat().contains("The default compilation policy (by-todo) is not supported");
}
}
use of com.sun.source.util.JavacTask in project error-prone by google.
the class ErrorProneJavacPluginTest method applyToPatchFile.
@Test
public void applyToPatchFile() throws IOException {
// TODO(b/63064865): Test is broken on Windows. Disable for now.
Assume.assumeFalse(StandardSystemProperty.OS_NAME.value().startsWith("Windows"));
Path tmp = temporaryFolder.newFolder().toPath();
Path patchDir = temporaryFolder.newFolder().toPath();
Files.createDirectories(patchDir);
Path patchFile = patchDir.resolve("error-prone.patch");
// verify that any existing content in the patch file is deleted
Files.write(patchFile, ImmutableList.of("--- C.java", "--- D.java"), UTF_8);
Path fileA = tmp.resolve("A.java");
Path fileB = tmp.resolve("B.java");
Files.write(fileA, ImmutableList.of(//
"class A implements Runnable {", " public void run() {}", "}"), UTF_8);
Files.write(fileB, ImmutableList.of(//
"class B implements Runnable {", " public void run() {}", "}"), UTF_8);
JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
JavacTask task = JavacTool.create().getTask(null, fileManager, diagnosticCollector, ImmutableList.of("-Xplugin:ErrorProne" + " -XepPatchChecks:MissingOverride -XepPatchLocation:" + patchDir.toString(), "-XDcompilePolicy=byfile"), ImmutableList.of(), fileManager.getJavaFileObjects(fileA, fileB));
assertThat(task.call()).named(Joiner.on('\n').join(diagnosticCollector.getDiagnostics())).isTrue();
assertThat(Files.readAllLines(patchFile, UTF_8).stream().filter(l -> l.startsWith("--- ")).map(l -> Paths.get(l.substring("--- ".length())).getFileName().toString()).collect(toImmutableList())).containsExactly("A.java", "B.java");
}
Aggregations