Skip to content

Commit

Permalink
Replace ZipOutputFileManager with the standard filemanager and JimFS
Browse files Browse the repository at this point in the history
Now that the standard filemanager supports nio, there's a better-supported way
to keep the compilation outputs in memory. ZipOutputFileManager doesn't
implement the full filemanager contract, is not compatible with the latest
javac 9's.

--
PiperOrigin-RevId: 151082823
MOS_MIGRATED_REVID=151082823
  • Loading branch information
cushon authored and hermione521 committed Mar 24, 2017
1 parent 4443123 commit 58a615c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ java_library(
":javac_turbine_compile_request",
":javac_turbine_compile_result",
":javac_turbine_compiler",
":zip_output_file_manager",
":zip_util",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:JarOwner",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:javac_options",
Expand Down Expand Up @@ -40,7 +39,6 @@ java_library(
name = "javac_turbine_compile_result",
srcs = ["JavacTurbineCompileResult.java"],
deps = [
":zip_output_file_manager",
"//third_party:guava",
"//third_party:jsr305",
"//third_party/java/jdk/langtools:javac",
Expand All @@ -54,9 +52,9 @@ java_library(
":javac_turbine_compile_request",
":javac_turbine_compile_result",
":javac_turbine_java_compiler",
":zip_output_file_manager",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/javac/plugins:dependency",
"//third_party:guava",
"//third_party:jimfs",
"//third_party:jsr305",
"//third_party/java/jdk/langtools:javac",
],
Expand All @@ -74,15 +72,6 @@ java_library(
],
)

java_library(
name = "zip_output_file_manager",
srcs = ["ZipOutputFileManager.java"],
deps = [
"//third_party:jsr305",
"//third_party/java/jdk/langtools:javac",
],
)

java_library(
name = "zip_util",
srcs = ["ZipUtil.java"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule;
import com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule.StrictJavaDeps;
import com.google.devtools.build.buildjar.javac.plugins.dependency.StrictJavaDepsPlugin;
import com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject;
import com.google.turbine.options.TurbineOptions;
import com.google.turbine.options.TurbineOptionsParser;
import com.sun.tools.javac.util.Context;
Expand All @@ -51,7 +50,6 @@
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.ZipOutputStream;
import javax.tools.StandardLocation;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
Expand Down Expand Up @@ -177,8 +175,7 @@ Result compile() throws IOException {

if (sources.isEmpty()) {
// accept compilations with an empty source list for compatibility with JavaBuilder
emitClassJar(
Paths.get(turbineOptions.outputFile()), ImmutableMap.<String, OutputFileObject>of());
emitClassJar(Paths.get(turbineOptions.outputFile()), ImmutableMap.of());
dependencyModule.emitDependencyInformation(/*classpath=*/ "", /*successful=*/ true);
return Result.OK_WITH_REDUCED_CLASSPATH;
}
Expand Down Expand Up @@ -265,17 +262,14 @@ private static JarOwner parseJarOwner(String line) {
}

/** Write the class output from a successful compilation to the output jar. */
private static void emitClassJar(Path outputJar, ImmutableMap<String, OutputFileObject> files)
private static void emitClassJar(Path outputJar, ImmutableMap<String, byte[]> 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;
}
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
String name = entry.getKey();
byte[] bytes = entry.getValue().asBytes();
byte[] bytes = entry.getValue();
if (bytes == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject;

import com.sun.tools.javac.util.Context;

import java.io.StringWriter;

/** The output from a {@link JavacTurbineCompiler} compilation. */
Expand All @@ -29,16 +26,13 @@ enum Status {
OK, ERROR
}

private final ImmutableMap<String, OutputFileObject> files;
private final ImmutableMap<String, byte[]> files;
private final Status status;
private final StringWriter sb;
private final Context context;

JavacTurbineCompileResult(
ImmutableMap<String, OutputFileObject> files,
Status status,
StringWriter sb,
Context context) {
ImmutableMap<String, byte[]> files, Status status, StringWriter sb, Context context) {
this.files = files;
this.status = status;
this.sb = sb;
Expand All @@ -55,8 +49,8 @@ String output() {
return sb.toString();
}

/** The files produced by the compilation's {@link ZipOutputFileManager}. */
ImmutableMap<String, OutputFileObject> files() {
/** The files produced by the compilation. */
ImmutableMap<String, byte[]> files() {
return files;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@

package com.google.devtools.build.java.turbine.javac;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.google.devtools.build.buildjar.javac.plugins.dependency.StrictJavaDepsPlugin;
import com.google.devtools.build.java.turbine.javac.JavacTurbineCompileResult.Status;
import com.google.devtools.build.java.turbine.javac.ZipOutputFileManager.OutputFileObject;
import com.sun.source.util.JavacTask;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.file.CacheFSInfo;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.Context;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.FileSystem;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -38,15 +47,16 @@ public class JavacTurbineCompiler {

static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) throws IOException {

Map<String, OutputFileObject> files = new LinkedHashMap<>();
Map<String, byte[]> 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)) {
try (FileSystem fs = Jimfs.newFileSystem(Configuration.forCurrentPlatform());
JavacFileManager fm = new JavacFileManager(new Context(), false, UTF_8)) {
JavacTask task =
JavacTool.create()
.getTask(
Expand All @@ -58,14 +68,34 @@ static JavacTurbineCompileResult compile(JavacTurbineCompileRequest request) thr
fm.getJavaFileObjectsFromPaths(request.sources()),
context);

Path classes = fs.getPath("/classes");
Files.createDirectories(classes);
Path sources = fs.getPath("/sources");
Files.createDirectories(sources);

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());
fm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, ImmutableList.of(classes));
fm.setLocationFromPaths(StandardLocation.SOURCE_OUTPUT, ImmutableList.of(sources));

status = task.call() ? Status.OK : Status.ERROR;

// collect class output
Files.walkFileTree(
classes,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
throws IOException {
files.put(classes.relativize(path).toString(), Files.readAllBytes(path));
return FileVisitResult.CONTINUE;
}
});

} catch (Throwable t) {
t.printStackTrace(pw);
status = Status.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class JavacTurbineJavaCompiler extends JavaCompiler implements AutoCloseable {
public JavacTurbineJavaCompiler(Context context, @Nullable StrictJavaDepsPlugin strictJavaDeps) {
super(context);
this.strictJavaDeps = strictJavaDeps;
JavaCompiler comp = JavaCompiler.instance(context);
if (strictJavaDeps != null) {
strictJavaDeps.init(context, Log.instance(context), this);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 58a615c

Please sign in to comment.