use of com.sun.source.util.JavacTask in project buck by facebook.
the class TracingTaskListener method setupTracing.
public static void setupTracing(JavaCompiler.CompilationTask task, JavacPhaseTracer tracing, Object next) {
if (!(task instanceof JavacTask)) {
return;
}
final JavacTask javacTask = (JavacTask) task;
// The main part of Buck doesn't have access to `TaskListener`, since that's defined in the
// compiler jar. So we had to pass it as an object and downcast.
final TaskListener nextListener = (TaskListener) next;
javacTask.setTaskListener(new TracingTaskListener(tracing, nextListener));
}
use of com.sun.source.util.JavacTask in project jdk8u_jdk by JetBrains.
the class Compiler method compileOne.
private File compileOne(Type type) {
if (this.flags.contains(Flags.USECACHE)) {
File dir = cache.get(type.getName());
if (dir != null) {
return dir;
}
}
List<JavaFileObject> files = new ArrayList<>();
SourceProcessor accum = (name, src) -> files.add(new SourceFile(name, src));
for (Type dep : type.typeDependencies()) {
dep.generateAsDependency(accum, type.methodDependencies());
}
type.generate(accum);
JavacTask ct = (JavacTask) this.systemJavaCompiler.getTask(null, this.fm, null, null, null, files);
File destDir = null;
do {
int value = counter.incrementAndGet();
destDir = new File(root, Integer.toString(value));
} while (destDir.exists());
if (this.flags.contains(Flags.VERBOSE)) {
System.out.println("Compilation unit for " + type.getName() + " : compiled into " + destDir);
for (JavaFileObject jfo : files) {
System.out.println(jfo.toString());
}
}
try {
destDir.mkdirs();
this.fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
} catch (IOException e) {
throw new RuntimeException("IOException encountered during compilation");
}
Boolean result = ct.call();
if (result == Boolean.FALSE) {
throw new RuntimeException("Compilation failure in " + type.getName() + " unit");
}
if (this.flags.contains(Flags.USECACHE)) {
File existing = cache.putIfAbsent(type.getName(), destDir);
if (existing != null) {
deleteDir(destDir);
return existing;
}
} else {
this.tempDirs.add(destDir);
}
return destDir;
}
use of com.sun.source.util.JavacTask in project st-js by st-js.
the class Generator method parseAndResolve.
// JavaCompiler throws an Error, so catching Throwable is intentional
@SuppressWarnings("PMD.AvoidCatchingThrowable")
private <JS> CompilationUnitTree parseAndResolve(File inputFile, GenerationContext<JS> context, ClassLoader builtProjectClassLoader, String sourceEncoding) {
JavaCompiler.CompilationTask task = null;
JavacTask javacTask = null;
try {
JavaCompiler compiler = getCompiler(builtProjectClassLoader, sourceEncoding);
Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromFiles(Collections.singleton(inputFile));
List<String> options = Arrays.asList("-proc:none");
task = compiler.getTask(null, classLoaderFileManager, null, options, null, fileObjects);
javacTask = (JavacTask) task;
context.setTrees(Trees.instance(javacTask));
context.setElements(javacTask.getElements());
context.setTypes(javacTask.getTypes());
Timers.start("parse-java");
CompilationUnitTree cu = javacTask.parse().iterator().next();
Timers.end("parse-java");
Timers.start("analyze-java");
javacTask.analyze();
Timers.end("analyze-java");
context.setCompilationUnit(cu);
return cu;
} catch (Throwable e) {
throw new JavascriptFileGenerationException(new SourcePosition(context.getInputFile(), 0, 0), "Cannot parse the Java file", e);
}
}
use of com.sun.source.util.JavacTask in project error-prone by google.
the class ErrorProneJavacPluginTest method applyFixes.
@Test
public void applyFixes() 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 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:IN_PLACE", "-XDcompilePolicy=byfile"), ImmutableList.of(), fileManager.getJavaFileObjects(fileA, fileB));
assertThat(task.call()).named(Joiner.on('\n').join(diagnosticCollector.getDiagnostics())).isTrue();
assertThat(Files.readAllLines(fileA, UTF_8)).containsExactly(//
"class A implements Runnable {", " @Override public void run() {}", "}").inOrder();
assertThat(Files.readAllLines(fileB, UTF_8)).containsExactly(//
"class B implements Runnable {", " @Override public void run() {}", "}").inOrder();
}
use of com.sun.source.util.JavacTask in project error-prone by google.
the class ErrorProneJavacPluginTest method hello.
@Test
public void hello() throws IOException {
FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path source = fileSystem.getPath("Test.java");
Files.write(source, ImmutableList.of("import java.util.HashSet;", "import java.util.Set;", "class Test {", " public static void main(String[] args) {", " Set<Short> s = new HashSet<>();", " for (short i = 0; i < 100; i++) {", " s.add(i);", " s.remove(i - 1);", " }", " System.out.println(s.size());", " }", "}"), 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", "-XDcompilePolicy=byfile"), ImmutableList.of(), fileManager.getJavaFileObjects(source));
assertThat(task.call()).isFalse();
Diagnostic<? extends JavaFileObject> diagnostic = diagnosticCollector.getDiagnostics().stream().filter(d -> d.getKind() == Diagnostic.Kind.ERROR).collect(onlyElement());
assertThat(diagnostic.getMessage(ENGLISH)).contains("[CollectionIncompatibleType]");
}
Aggregations