This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new c77f985 fix: read legacy ct.sym layout in module-info analysis (#890)
c77f985 is described below
commit c77f985ca3b63a7e2544487cc83c552e11a8de74
Author: Elliotte Rusty Harold <[email protected]>
AuthorDate: Fri Nov 6 11:50:05 2026 +0000
fix: read legacy ct.sym layout in module-info analysis (#890)
* fix: read legacy ct.sym layout in module-info analysis
JDK 9 through 15 store historical platform class signatures without a
module directory in ct.sym, and keep module descriptors under
<release>-modules. Index those signatures so module requirements can be
inferred when the analysis runs on such JDKs.
Fixes #889
* Read legacy module descriptors before class signatures
Legacy ct.sym class ownership was resolved in entry order, so a class
signature
that appeared before its module descriptor could lose its owner permanently
when the module no longer exists in the current JDK. Read all legacy module
descriptors first, then class signatures, and add a class-first regression
test.
---
.../maven/plugins/shade/PlatformModuleIndex.java | 167 ++++++++++++++++++---
.../plugins/shade/PlatformModuleIndexTest.java | 74 +++++++++
2 files changed, 219 insertions(+), 22 deletions(-)
diff --git
a/src/main/java/org/apache/maven/plugins/shade/PlatformModuleIndex.java
b/src/main/java/org/apache/maven/plugins/shade/PlatformModuleIndex.java
index 5ce397f..9349773 100644
--- a/src/main/java/org/apache/maven/plugins/shade/PlatformModuleIndex.java
+++ b/src/main/java/org/apache/maven/plugins/shade/PlatformModuleIndex.java
@@ -24,6 +24,7 @@ import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
@@ -33,6 +34,9 @@ import java.util.jar.JarFile;
import org.apache.maven.plugin.MojoExecutionException;
import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ModuleVisitor;
+import org.objectweb.asm.Opcodes;
/**
* Release-aware ownership index for classes in a selected JDK.
@@ -40,10 +44,16 @@ import org.objectweb.asm.ClassReader;
final class PlatformModuleIndex {
private static final int FIRST_MODULE_RELEASE = 9;
+ private static final String MODULE_INFO = "module-info.sig";
+
+ private static final String MODULES_SUFFIX = "-modules";
+
private final File jdkHome;
private final Map<Integer, Map<String, SortedSet<String>>> classOwners =
new HashMap<>();
+ private final Map<Integer, Map<String, SortedSet<String>>> packageOwners =
new HashMap<>();
+
private final Map<Integer, Set<String>> modules = new HashMap<>();
private final int release;
@@ -51,8 +61,8 @@ final class PlatformModuleIndex {
PlatformModuleIndex(File jdkHome) throws MojoExecutionException {
this.jdkHome = normalizeJdkHome(jdkHome);
this.release = readCurrentRelease();
- readHistoricalReleases();
readCurrentReleaseClasses();
+ readHistoricalReleases();
}
int getRelease() {
@@ -100,27 +110,15 @@ final class PlatformModuleIndex {
return;
}
try (JarFile archive = new JarFile(ctSym)) {
- Enumeration<JarEntry> entries = archive.entries();
- while (entries.hasMoreElements()) {
- JarEntry entry = entries.nextElement();
- if (entry.isDirectory()) {
- continue;
- }
- String[] parts = entry.getName().split("/", 3);
- if (parts.length < 3) {
- continue;
- }
- String releaseSet = parts[0];
- String module = parts[1];
- String path = parts[2];
- if ("module-info.sig".equals(path)) {
- forEachRelease(
- releaseSet,
- value -> modules.computeIfAbsent(value, key -> new
TreeSet<>())
- .add(module));
- } else if (path.endsWith(".sig")) {
- String className = path.substring(0, path.length() -
".sig".length());
- forEachRelease(releaseSet, value -> addOwner(value,
className, module));
+ List<JarEntry> entries = Collections.list(archive.entries());
+ if (usesLegacyLayout(entries)) {
+ readLegacyEntries(archive, entries);
+ } else {
+ for (JarEntry entry : entries) {
+ String[] parts = entryParts(entry);
+ if (parts != null) {
+ readModularEntry(parts);
+ }
}
}
} catch (IOException e) {
@@ -128,6 +126,131 @@ final class PlatformModuleIndex {
}
}
+ private static boolean usesLegacyLayout(List<JarEntry> entries) {
+ for (JarEntry entry : entries) {
+ String name = entry.getName();
+ int separator = name.indexOf('/');
+ if (separator > 0 && name.substring(0,
separator).endsWith(MODULES_SUFFIX)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void readModularEntry(String[] parts) {
+ String releaseSet = parts[0];
+ String module = parts[1];
+ String path = parts[2];
+ if (MODULE_INFO.equals(path)) {
+ forEachRelease(
+ releaseSet,
+ value -> modules.computeIfAbsent(value, key -> new
TreeSet<>())
+ .add(module));
+ } else if (path.endsWith(".sig")) {
+ String className = path.substring(0, path.length() -
".sig".length());
+ forEachRelease(releaseSet, value -> addOwner(value, className,
module));
+ }
+ }
+
+ private static String[] entryParts(JarEntry entry) {
+ if (entry.isDirectory()) {
+ return null;
+ }
+ String[] parts = entry.getName().split("/", 3);
+ return parts.length < 3 ? null : parts;
+ }
+
+ private void readLegacyEntries(JarFile archive, List<JarEntry> entries)
throws IOException {
+ for (JarEntry entry : entries) {
+ String[] parts = entryParts(entry);
+ if (parts != null && parts[0].endsWith(MODULES_SUFFIX)) {
+ readLegacyModuleDescriptor(archive, entry, parts);
+ }
+ }
+ for (JarEntry entry : entries) {
+ String[] parts = entryParts(entry);
+ if (parts != null && !parts[0].endsWith(MODULES_SUFFIX)) {
+ readLegacyClass(parts);
+ }
+ }
+ }
+
+ private void readLegacyModuleDescriptor(JarFile archive, JarEntry entry,
String[] parts) throws IOException {
+ String releaseSet = parts[0];
+ if (!MODULE_INFO.equals(parts[2])) {
+ return;
+ }
+ String module = parts[1];
+ SortedSet<String> packages = readExportedPackages(archive, entry);
+ String releases = releaseSet.substring(0, releaseSet.length() -
MODULES_SUFFIX.length());
+ forEachRelease(releases, value -> {
+ modules.computeIfAbsent(value, key -> new TreeSet<>()).add(module);
+ for (String packaze : packages) {
+ packageOwners
+ .computeIfAbsent(value, key -> new HashMap<>())
+ .computeIfAbsent(packaze, key -> new TreeSet<>())
+ .add(module);
+ }
+ });
+ }
+
+ private void readLegacyClass(String[] parts) {
+ String path = parts[2];
+ if (!path.endsWith(".sig")) {
+ return;
+ }
+ String className = parts[1] + "/" + path.substring(0, path.length() -
".sig".length());
+ forEachRelease(parts[0], value -> addLegacyOwner(value, className));
+ }
+
+ private static SortedSet<String> readExportedPackages(JarFile archive,
JarEntry entry) throws IOException {
+ SortedSet<String> packages = new TreeSet<>();
+ try (InputStream input = archive.getInputStream(entry)) {
+ new ClassReader(input)
+ .accept(
+ new ClassVisitor(Opcodes.ASM9) {
+ @Override
+ public ModuleVisitor visitModule(String name,
int access, String version) {
+ return new ModuleVisitor(Opcodes.ASM9) {
+ @Override
+ public void visitExport(String
packaze, int access, String... modules) {
+ packages.add(packaze);
+ }
+
+ @Override
+ public void visitOpen(String packaze,
int access, String... modules) {
+ packages.add(packaze);
+ }
+
+ @Override
+ public void visitPackage(String
packaze) {
+ packages.add(packaze);
+ }
+ };
+ }
+ },
+ 0);
+ }
+ return packages;
+ }
+
+ private void addLegacyOwner(int targetRelease, String className) {
+ int separator = className.lastIndexOf('/');
+ String packaze = separator < 0 ? "" : className.substring(0,
separator);
+ SortedSet<String> owners = packageOwners
+ .getOrDefault(targetRelease, Collections.emptyMap())
+ .get(packaze);
+ if (owners == null || owners.isEmpty()) {
+ owners = classOwners.getOrDefault(release,
Collections.emptyMap()).get(className);
+ }
+ if (owners == null || owners.isEmpty()) {
+ return;
+ }
+ for (String owner : owners) {
+ addOwner(targetRelease, className, owner);
+ }
+ }
+
private void readCurrentReleaseClasses() throws MojoExecutionException {
File jmods = new File(jdkHome, "jmods");
File[] files = jmods.listFiles((directory, name) ->
name.endsWith(".jmod"));
diff --git
a/src/test/java/org/apache/maven/plugins/shade/PlatformModuleIndexTest.java
b/src/test/java/org/apache/maven/plugins/shade/PlatformModuleIndexTest.java
index ea74fc4..81a6eb8 100644
--- a/src/test/java/org/apache/maven/plugins/shade/PlatformModuleIndexTest.java
+++ b/src/test/java/org/apache/maven/plugins/shade/PlatformModuleIndexTest.java
@@ -60,6 +60,36 @@ public class PlatformModuleIndexTest {
assertTrue(index.hasModule("java.base", 21));
}
+ @Test
+ public void indexesLegacyHistoricalPlatformOwnership() throws Exception {
+ File jdkHome = syntheticLegacyJdk(false);
+
+ PlatformModuleIndex index = new PlatformModuleIndex(jdkHome);
+
+ assertEquals(21, index.getRelease());
+ assertEquals(singletonSortedSet("java.base"),
index.findOwners("java/lang/Object", 9));
+ assertEquals(singletonSortedSet("java.sql"),
index.findOwners("java/sql/Connection", 9));
+ assertEquals(singletonSortedSet("java.sql"),
index.findOwners("java/sql/Connection", 10));
+ assertTrue(index.findOwners("java/sql/Driver", 9).isEmpty());
+ assertEquals(singletonSortedSet("java.sql"),
index.findOwners("java/sql/Driver", 10));
+ assertEquals(singletonSortedSet("java.xml"),
index.findOwners("javax/xml/parsers/Parser", 9));
+ assertTrue(index.findOwners("javax/xml/parsers/Parser", 10).isEmpty());
+ assertTrue(index.hasModule("java.base", 9));
+ assertTrue(index.hasModule("java.sql", 10));
+ assertTrue(index.hasModule("java.xml", 9));
+ assertFalse(index.hasModule("java.xml", 10));
+ assertTrue(index.hasModule("java.base", 21));
+ }
+
+ @Test
+ public void
indexesLegacyHistoricalPlatformOwnershipWhenClassesPrecedeDescriptors() throws
Exception {
+ File jdkHome = syntheticLegacyJdk(true);
+
+ PlatformModuleIndex index = new PlatformModuleIndex(jdkHome);
+
+ assertEquals(singletonSortedSet("java.xml"),
index.findOwners("javax/xml/parsers/Parser", 9));
+ }
+
@Test
public void rejectsJdkWithoutHistoricalPlatformSignatures() throws
Exception {
File jdkHome = newFolder("jdk");
@@ -93,6 +123,34 @@ public class PlatformModuleIndexTest {
return jdkHome;
}
+ private File syntheticLegacyJdk(boolean classesBeforeDescriptors) throws
IOException {
+ File jdkHome = newFolder("jdk");
+ File jmods = new File(jdkHome, "jmods");
+ File lib = new File(jdkHome, "lib");
+ assertTrue(jmods.mkdirs());
+ assertTrue(lib.mkdirs());
+
+ writeJmod(new File(jmods, "java.base.jmod"), "java.base", Opcodes.V21,
"java/lang/Object");
+ writeJmod(new File(jmods, "java.sql.jmod"), "java.sql", Opcodes.V21,
"java/sql/Driver");
+
+ try (JarOutputStream output = new
JarOutputStream(Files.newOutputStream(new File(lib, "ct.sym").toPath()))) {
+ if (classesBeforeDescriptors) {
+ writeEmptyEntry(output, "9/javax/xml/parsers/Parser.sig");
+ }
+ writeModuleInfo(output, "9-modules/java.base/module-info.sig",
"java.base", "java/lang");
+ writeModuleInfo(output, "9-modules/java.sql/module-info.sig",
"java.sql", "java/sql");
+ writeModuleInfo(output, "9-modules/java.xml/module-info.sig",
"java.xml", "javax/xml/parsers");
+ writeModuleInfo(output, "A-modules/java.sql/module-info.sig",
"java.sql", "java/sql");
+ writeEmptyEntry(output, "9A/java/lang/Object.sig");
+ writeEmptyEntry(output, "9A/java/sql/Connection.sig");
+ if (!classesBeforeDescriptors) {
+ writeEmptyEntry(output, "9/javax/xml/parsers/Parser.sig");
+ }
+ writeEmptyEntry(output, "A/java/sql/Driver.sig");
+ }
+ return jdkHome;
+ }
+
private File newFolder(String name) throws IOException {
return
Files.createDirectory(temporaryFolder.toPath().resolve(name)).toFile();
}
@@ -112,6 +170,22 @@ public class PlatformModuleIndexTest {
}
}
+ private static void writeModuleInfo(JarOutputStream output, String name,
String module, String... exports)
+ throws IOException {
+ ClassWriter writer = new ClassWriter(0);
+ writer.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null,
null, null);
+ ModuleVisitor visitor = writer.visitModule(module, 0, null);
+ for (String packaze : exports) {
+ visitor.visitExport(packaze, 0);
+ }
+ visitor.visitEnd();
+ writer.visitEnd();
+
+ output.putNextEntry(new JarEntry(name));
+ output.write(writer.toByteArray());
+ output.closeEntry();
+ }
+
private static void writeEmptyEntry(JarOutputStream output, String name)
throws IOException {
output.putNextEntry(new JarEntry(name));
output.write(new byte[] {0});