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();
}
use of com.sun.source.util.JavacTask in project ceylon-compiler by ceylon.
the class T6402077 method main.
public static void main(String... args) throws IOException {
class MyFileObject extends SimpleJavaFileObject {
MyFileObject() {
super(URI.create("myfo:///Test.java"), SOURCE);
}
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
// 0123456789012345678901234
return "class Test { Test() { } }";
}
}
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
List<JavaFileObject> compilationUnits = Collections.<JavaFileObject>singletonList(new MyFileObject());
JavacTask task = (JavacTask) javac.getTask(null, null, null, null, null, compilationUnits);
Trees trees = Trees.instance(task);
CompilationUnitTree toplevel = task.parse().iterator().next();
Tree tree = ((ClassTree) toplevel.getTypeDecls().get(0)).getMembers().get(0);
long pos = trees.getSourcePositions().getStartPosition(toplevel, tree);
if (pos != 13)
throw new AssertionError(String.format("Start pos for %s is incorrect (%s)!", tree, pos));
pos = trees.getSourcePositions().getEndPosition(toplevel, tree);
if (pos != 23)
throw new AssertionError(String.format("End pos for %s is incorrect (%s)!", tree, pos));
}
Aggregations