Skip to content

Commit

Permalink
Use .turbine instead of .class as the file extension for repackag…
Browse files Browse the repository at this point in the history
…ed transitive deps

The entries are not complete class files (methods and other constructs are removed), and should only be ready by turbine. Using a different file extension avoids issues with other tools that scan the jar for `.class` entries.

PiperOrigin-RevId: 669424702
  • Loading branch information
cushon authored and Javac Team committed Aug 30, 2024
1 parent eeb5879 commit 5bd2dfa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
20 changes: 15 additions & 5 deletions java/com/google/turbine/binder/ClassPathBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public final class ClassPathBinder {
*/
public static final String TRANSITIVE_PREFIX = "META-INF/TRANSITIVE/";

/**
* The suffix for repackaged transitive dependencies; see {@link
* com.google.turbine.deps.Transitive}.
*/
public static final String TRANSITIVE_SUFFIX = ".turbine";

/** Creates an environment containing symbols in the given classpath. */
public static ClassPath bindClasspath(Collection<Path> paths) throws IOException {
// TODO(cushon): this is going to require an env eventually,
Expand Down Expand Up @@ -110,14 +116,14 @@ private static void bindJar(
// 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)) {
if (!name.endsWith(TRANSITIVE_SUFFIX)) {
continue;
}
ClassSymbol sym =
new ClassSymbol(
name.substring(TRANSITIVE_PREFIX.length(), name.length() - ".class".length()));
name.substring(
TRANSITIVE_PREFIX.length(), name.length() - TRANSITIVE_SUFFIX.length()));
transitive.computeIfAbsent(
sym,
new Function<ClassSymbol, BytecodeBoundClass>() {
Expand All @@ -128,6 +134,10 @@ public BytecodeBoundClass apply(ClassSymbol sym) {
});
continue;
}
if (!name.endsWith(".class")) {
resources.put(name, toByteArrayOrDie(ze));
continue;
}
if (name.substring(name.lastIndexOf('/') + 1).equals("module-info.class")) {
ModuleInfo moduleInfo =
BytecodeBinder.bindModuleInfo(path.toString(), toByteArrayOrDie(ze));
Expand Down
4 changes: 3 additions & 1 deletion java/com/google/turbine/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ private static void writeOutput(
}
for (Map.Entry<String, byte[]> entry : transitive.entrySet()) {
addEntry(
jos, ClassPathBinder.TRANSITIVE_PREFIX + entry.getKey() + ".class", entry.getValue());
jos,
ClassPathBinder.TRANSITIVE_PREFIX + entry.getKey() + ClassPathBinder.TRANSITIVE_SUFFIX,
entry.getValue());
}
for (Map.Entry<String, byte[]> entry : lowered.entrySet()) {
addEntry(jos, entry.getKey() + ".class", entry.getValue());
Expand Down
30 changes: 15 additions & 15 deletions javatests/com/google/turbine/deps/TransitiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public void transitive() throws Exception {
.containsExactly(
"META-INF/",
"META-INF/MANIFEST.MF",
"META-INF/TRANSITIVE/a/A.class",
"META-INF/TRANSITIVE/a/A$Anno.class",
"META-INF/TRANSITIVE/a/A$Inner.class",
"META-INF/TRANSITIVE/a/A.turbine",
"META-INF/TRANSITIVE/a/A$Anno.turbine",
"META-INF/TRANSITIVE/a/A$Inner.turbine",
"b/B.class")
.inOrder();

ClassFile a = ClassReader.read(null, readJar(libb).get("META-INF/TRANSITIVE/a/A.class"));
ClassFile a = ClassReader.read(null, readJar(libb).get("META-INF/TRANSITIVE/a/A.turbine"));
// methods and non-constant fields are removed
assertThat(getOnlyElement(a.fields()).name()).isEqualTo("CONST");
assertThat(a.methods()).isEmpty();
Expand All @@ -142,7 +142,7 @@ public void transitive() throws Exception {

// annotation interface methods are preserved
assertThat(
ClassReader.read(null, readJar(libb).get("META-INF/TRANSITIVE/a/A$Anno.class"))
ClassReader.read(null, readJar(libb).get("META-INF/TRANSITIVE/a/A$Anno.turbine"))
.methods())
.hasSize(1);

Expand Down Expand Up @@ -186,10 +186,10 @@ public void transitive() throws Exception {
.containsExactly(
"META-INF/",
"META-INF/MANIFEST.MF",
"META-INF/TRANSITIVE/b/B.class",
"META-INF/TRANSITIVE/a/A.class",
"META-INF/TRANSITIVE/a/A$Anno.class",
"META-INF/TRANSITIVE/a/A$Inner.class",
"META-INF/TRANSITIVE/b/B.turbine",
"META-INF/TRANSITIVE/a/A.turbine",
"META-INF/TRANSITIVE/a/A$Anno.turbine",
"META-INF/TRANSITIVE/a/A$Inner.turbine",
"c/C.class")
.inOrder();

Expand Down Expand Up @@ -256,8 +256,8 @@ public void anonymous() throws Exception {
.containsExactly(
"META-INF/",
"META-INF/MANIFEST.MF",
"META-INF/TRANSITIVE/a/A.class",
"META-INF/TRANSITIVE/a/A$I.class",
"META-INF/TRANSITIVE/a/A.turbine",
"META-INF/TRANSITIVE/a/A$I.turbine",
"b/B.class")
.inOrder();
}
Expand Down Expand Up @@ -297,9 +297,9 @@ public void childClass() throws Exception {
.containsExactly(
"META-INF/",
"META-INF/MANIFEST.MF",
"META-INF/TRANSITIVE/a/A$I.class",
"META-INF/TRANSITIVE/a/S.class",
"META-INF/TRANSITIVE/a/A.class",
"META-INF/TRANSITIVE/a/A$I.turbine",
"META-INF/TRANSITIVE/a/S.turbine",
"META-INF/TRANSITIVE/a/A.turbine",
"b/B$I.class",
"b/B.class")
.inOrder();
Expand Down Expand Up @@ -338,7 +338,7 @@ public void packageInfo() throws Exception {
.containsExactly(
"META-INF/",
"META-INF/MANIFEST.MF",
"META-INF/TRANSITIVE/p/package-info.class",
"META-INF/TRANSITIVE/p/package-info.turbine",
"p/P.class")
.inOrder();
}
Expand Down

0 comments on commit 5bd2dfa

Please sign in to comment.