This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch fix-889-legacy-ctsym in repository https://gitbox.apache.org/repos/asf/maven-shade-plugin.git
commit f0a517d797f99dde1808372b0958a32da44f7019 Author: Elliotte Rusty Harold <[email protected]> AuthorDate: Sun Nov 1 14:29:11 2026 +0000 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 --- .../maven/plugins/shade/PlatformModuleIndex.java | 132 ++++++++++++++++++--- .../plugins/shade/PlatformModuleIndexTest.java | 60 ++++++++++ 2 files changed, 177 insertions(+), 15 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..392c3be 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,9 +110,9 @@ final class PlatformModuleIndex { return; } try (JarFile archive = new JarFile(ctSym)) { - Enumeration<JarEntry> entries = archive.entries(); - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); + List<JarEntry> entries = Collections.list(archive.entries()); + boolean legacyLayout = usesLegacyLayout(entries); + for (JarEntry entry : entries) { if (entry.isDirectory()) { continue; } @@ -110,17 +120,10 @@ final class PlatformModuleIndex { 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)); + if (legacyLayout) { + readLegacyEntry(archive, entry, parts); + } else { + readModularEntry(parts); } } } catch (IOException e) { @@ -128,6 +131,105 @@ 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 void readLegacyEntry(JarFile archive, JarEntry entry, String[] parts) throws IOException { + String releaseSet = parts[0]; + String path = parts[2]; + if (releaseSet.endsWith(MODULES_SUFFIX)) { + if (!MODULE_INFO.equals(path)) { + 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); + } + }); + } else if (path.endsWith(".sig")) { + String className = parts[1] + "/" + path.substring(0, path.length() - ".sig".length()); + forEachRelease(releaseSet, 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..7750c58 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,27 @@ public class PlatformModuleIndexTest { assertTrue(index.hasModule("java.base", 21)); } + @Test + public void indexesLegacyHistoricalPlatformOwnership() throws Exception { + File jdkHome = syntheticLegacyJdk(); + + 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 rejectsJdkWithoutHistoricalPlatformSignatures() throws Exception { File jdkHome = newFolder("jdk"); @@ -93,6 +114,29 @@ public class PlatformModuleIndexTest { return jdkHome; } + private File syntheticLegacyJdk() 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()))) { + 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"); + 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 +156,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});
