use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class Warn5 method test.
static void test(SourceLevel sourceLevel, XlintOption xlint, TrustMe trustMe, SuppressLevel suppressLevel, ModifierKind modKind, MethodKind methKind, SignatureKind sig, BodyKind body) throws Exception {
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavaSource source = new JavaSource(trustMe, suppressLevel, modKind, methKind, sig, body);
DiagnosticChecker dc = new DiagnosticChecker();
JavacTask ct = (JavacTask) tool.getTask(null, fm, dc, Arrays.asList(xlint.getXlintOption(), "-source", sourceLevel.sourceKey), null, Arrays.asList(source));
ct.analyze();
check(sourceLevel, dc, source, xlint, trustMe, suppressLevel, modKind, methKind, sig, body);
}
use of com.sun.source.util.JavacTask in project bazel by bazelbuild.
the class BlazeJavacMain method compile.
public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
List<String> javacArguments = arguments.javacOptions();
try {
javacArguments = processPluginArgs(arguments.plugins(), javacArguments);
} catch (InvalidCommandLineException e) {
return BlazeJavacResult.error(e.getMessage());
}
Context context = new Context();
setupBlazeJavaCompiler(arguments.plugins(), context);
boolean ok = false;
StringWriter errOutput = new StringWriter();
// TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
// it and handling exceptions directly in callers.
PrintWriter errWriter = new PrintWriter(errOutput);
Listener diagnostics = new Listener(context);
BlazeJavaCompiler compiler;
try (JavacFileManager fileManager = new ClassloaderMaskingFileManager()) {
JavacTask task = JavacTool.create().getTask(errWriter, fileManager, diagnostics, javacArguments, ImmutableList.of(), /*classes*/
fileManager.getJavaFileObjectsFromPaths(arguments.sourceFiles()), context);
if (arguments.processors() != null) {
task.setProcessors(arguments.processors());
}
fileManager.setContext(context);
setLocations(fileManager, arguments);
try {
ok = task.call();
} catch (PropagatedException e) {
throw e.getCause();
}
} catch (Throwable t) {
t.printStackTrace(errWriter);
ok = false;
} finally {
compiler = (BlazeJavaCompiler) JavaCompiler.instance(context);
if (ok) {
// or empty source files.
if (compiler.skippedFlowEvents() > 0 && compiler.flowEvents() == 0) {
errWriter.println("Expected at least one FLOW event");
ok = false;
}
}
}
errWriter.flush();
return new BlazeJavacResult(ok, filterDiagnostics(diagnostics.build()), errOutput.toString(), compiler);
}
use of com.sun.source.util.JavacTask in project bazel by bazelbuild.
the class JavacTurbineCompiler method compile.
static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {
Map<String, OutputFileObject> files = new LinkedHashMap<>();
Status status;
StringWriter sw = new StringWriter();
Context context = new Context();
try (PrintWriter pw = new PrintWriter(sw)) {
setupContext(context, request.strictJavaDepsPlugin());
CacheFSInfo.preRegister(context);
try (ZipOutputFileManager fm = new ZipOutputFileManager(files)) {
JavacTask task = JavacTool.create().getTask(pw, fm, null, /*diagnostics*/
request.javacOptions(), ImmutableList.of(), /*classes*/
fm.getJavaFileObjectsFromPaths(request.sources()), context);
fm.setContext(context);
fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, Collections.<Path>emptyList());
fm.setLocationFromPaths(StandardLocation.CLASS_PATH, request.classPath());
fm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, request.bootClassPath());
fm.setLocationFromPaths(StandardLocation.ANNOTATION_PROCESSOR_PATH, request.processorClassPath());
status = task.call() ? Status.OK : Status.ERROR;
} catch (Throwable t) {
t.printStackTrace(pw);
status = Status.ERROR;
}
}
return new JavacTurbineCompileResult(ImmutableMap.copyOf(files), status, sw, context);
}
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();
}
Aggregations