Search in sources :

Example 1 with OutputFileObject

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);
        }
    }
}
Also used : OutputFileObject(com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject) ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with OutputFileObject

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);
}
Also used : Status(com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status) Context(com.sun.tools.javac.util.Context) OutputFileObject(com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject) StringWriter(java.io.StringWriter) JavacTask(com.sun.source.util.JavacTask) LinkedHashMap(java.util.LinkedHashMap) PrintWriter(java.io.PrintWriter)

Aggregations

OutputFileObject (com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Status (com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status)1 JavacTask (com.sun.source.util.JavacTask)1 Context (com.sun.tools.javac.util.Context)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ZipOutputStream (java.util.zip.ZipOutputStream)1