use of com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject in project bazel by bazelbuild.
the class JavacTurbine method emitClassJar.
/** Write the class output from a successful compilation to the output jar. */
private static void emitClassJar(Path outputJar, ImmutableMap<String, OutputFileObject> files) throws IOException {
try (OutputStream fos = Files.newOutputStream(outputJar);
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos, ZIPFILE_BUFFER_SIZE))) {
for (Map.Entry<String, OutputFileObject> entry : files.entrySet()) {
if (entry.getValue().location != StandardLocation.CLASS_OUTPUT) {
continue;
}
String name = entry.getKey();
byte[] bytes = entry.getValue().asBytes();
if (bytes == null) {
continue;
}
if (name.endsWith(".class")) {
bytes = processBytecode(bytes);
}
ZipUtil.storeEntry(name, bytes, zipOut);
}
}
}
use of com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject 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);
}
Aggregations