aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcushon <cushon@google.com>2019-07-27 15:27:51 -0700
committerLiam Miller-Cushon <cushon@google.com>2019-07-31 18:55:40 -0400
commit314e9abfc556d3b85206639ddf73738e4396fe0b (patch)
tree82e33ef3eb8d4dda91fd4e3f883b31bccbf36058
parent2f5526b2079192717efa98536fbccf71fb49b3a0 (diff)
downloadturbine-314e9abfc556d3b85206639ddf73738e4396fe0b.tar.gz
Save non-class resources from the classpath
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=260323424
-rw-r--r--java/com/google/turbine/binder/ClassPath.java5
-rw-r--r--java/com/google/turbine/binder/ClassPathBinder.java12
-rw-r--r--java/com/google/turbine/binder/CtSymClassBinder.java5
-rw-r--r--java/com/google/turbine/binder/JimageClassBinder.java5
-rw-r--r--javatests/com/google/turbine/binder/ClassPathBinderTest.java17
5 files changed, 41 insertions, 3 deletions
diff --git a/java/com/google/turbine/binder/ClassPath.java b/java/com/google/turbine/binder/ClassPath.java
index d3461bf..eeea7c5 100644
--- a/java/com/google/turbine/binder/ClassPath.java
+++ b/java/com/google/turbine/binder/ClassPath.java
@@ -16,6 +16,7 @@
package com.google.turbine.binder;
+import com.google.common.base.Supplier;
import com.google.turbine.binder.bound.ModuleInfo;
import com.google.turbine.binder.bytecode.BytecodeBoundClass;
import com.google.turbine.binder.env.Env;
@@ -24,7 +25,7 @@ import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.binder.sym.ModuleSymbol;
/**
- * A compilation classpath, e.g. the user or platform class path. Maybe backed by a search path of
+ * A compilation classpath, e.g. the user or platform class path. May be backed by a search path of
* jar files, or a jrtfs filesystem.
*/
public interface ClassPath {
@@ -36,4 +37,6 @@ public interface ClassPath {
/** The classpath's top level index. */
TopLevelIndex index();
+
+ Supplier<byte[]> resource(String path);
}
diff --git a/java/com/google/turbine/binder/ClassPathBinder.java b/java/com/google/turbine/binder/ClassPathBinder.java
index 5d8db86..8aead80 100644
--- a/java/com/google/turbine/binder/ClassPathBinder.java
+++ b/java/com/google/turbine/binder/ClassPathBinder.java
@@ -53,6 +53,7 @@ public class ClassPathBinder {
Map<ClassSymbol, BytecodeBoundClass> transitive = new LinkedHashMap<>();
Map<ClassSymbol, BytecodeBoundClass> map = new HashMap<>();
Map<ModuleSymbol, ModuleInfo> modules = new HashMap<>();
+ Map<String, Supplier<byte[]>> resources = new HashMap<>();
Env<ClassSymbol, BytecodeBoundClass> benv =
new Env<ClassSymbol, BytecodeBoundClass>() {
@Override
@@ -62,7 +63,7 @@ public class ClassPathBinder {
};
for (Path path : paths) {
try {
- bindJar(path, map, modules, benv, transitive);
+ bindJar(path, map, modules, benv, transitive, resources);
} catch (IOException e) {
throw new IOException("error reading " + path, e);
}
@@ -89,6 +90,11 @@ public class ClassPathBinder {
public TopLevelIndex index() {
return index;
}
+
+ @Override
+ public Supplier<byte[]> resource(String path) {
+ return resources.get(path);
+ }
};
}
@@ -97,12 +103,14 @@ public class ClassPathBinder {
Map<ClassSymbol, BytecodeBoundClass> env,
Map<ModuleSymbol, ModuleInfo> modules,
Env<ClassSymbol, BytecodeBoundClass> benv,
- Map<ClassSymbol, BytecodeBoundClass> transitive)
+ Map<ClassSymbol, BytecodeBoundClass> transitive,
+ Map<String, Supplier<byte[]>> resources)
throws IOException {
// TODO(cushon): don't leak file descriptors
for (Zip.Entry ze : new Zip.ZipIterable(path)) {
String name = ze.name();
if (!name.endsWith(".class")) {
+ resources.put(name, toByteArrayOrDie(ze));
continue;
}
if (name.startsWith(TRANSITIVE_PREFIX)) {
diff --git a/java/com/google/turbine/binder/CtSymClassBinder.java b/java/com/google/turbine/binder/CtSymClassBinder.java
index 84fa966..a6f1b3d 100644
--- a/java/com/google/turbine/binder/CtSymClassBinder.java
+++ b/java/com/google/turbine/binder/CtSymClassBinder.java
@@ -105,6 +105,11 @@ public class CtSymClassBinder {
public TopLevelIndex index() {
return index;
}
+
+ @Override
+ public Supplier<byte[]> resource(String input) {
+ return null;
+ }
};
}
diff --git a/java/com/google/turbine/binder/JimageClassBinder.java b/java/com/google/turbine/binder/JimageClassBinder.java
index 4c14bd6..53aa634 100644
--- a/java/com/google/turbine/binder/JimageClassBinder.java
+++ b/java/com/google/turbine/binder/JimageClassBinder.java
@@ -256,5 +256,10 @@ public class JimageClassBinder {
public TopLevelIndex index() {
return index;
}
+
+ @Override
+ public Supplier<byte[]> resource(String input) {
+ return null;
+ }
}
}
diff --git a/javatests/com/google/turbine/binder/ClassPathBinderTest.java b/javatests/com/google/turbine/binder/ClassPathBinderTest.java
index 3f41706..c11d814 100644
--- a/javatests/com/google/turbine/binder/ClassPathBinderTest.java
+++ b/javatests/com/google/turbine/binder/ClassPathBinderTest.java
@@ -44,7 +44,10 @@ import com.google.turbine.type.AnnoInfo;
import com.google.turbine.type.Type.ClassTy;
import java.io.IOError;
import java.io.IOException;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -161,4 +164,18 @@ public class ClassPathBinderTest {
assertThat(e).hasMessageThat().contains("NOT_A_JAR");
}
}
+
+ @Test
+ public void resources() throws Exception {
+ Path path = temporaryFolder.newFile("tmp.jar").toPath();
+ try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(path))) {
+ jos.putNextEntry(new JarEntry("foo/bar/hello.txt"));
+ jos.write("hello".getBytes(UTF_8));
+ jos.putNextEntry(new JarEntry("foo/bar/Baz.class"));
+ jos.write("goodbye".getBytes(UTF_8));
+ }
+ ClassPath classPath = ClassPathBinder.bindClasspath(ImmutableList.of(path));
+ assertThat(new String(classPath.resource("foo/bar/hello.txt").get(), UTF_8)).isEqualTo("hello");
+ assertThat(classPath.resource("foo/bar/Baz.class")).isNull();
+ }
}