99import org .apache .commons .io .IOUtils ;
1010import org .apache .commons .lang3 .StringUtils ;
1111import org .objectweb .asm .Opcodes ;
12+ import org .objectweb .asm .commons .Remapper ;
13+ import org .objectweb .asm .tree .ClassNode ;
1214
1315import java .io .ByteArrayOutputStream ;
1416import java .io .File ;
@@ -44,7 +46,7 @@ public byte[] packBytes(JarPackerConfig jarPackerConfig) {
4446 classes .putAll (virtualMachineClasses );
4547 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
4648 try (JarOutputStream targetJar = new JarOutputStream (outputStream , manifest )) {
47- addDependencies (targetJar , relocatePrefix );
49+ addAsmDependencies (targetJar , relocatePrefix );
4850 addClassesToJar (targetJar , jarPackerConfig .getClassBytes (), relocatePrefix );
4951 for (Map .Entry <String , byte []> entry : classes .entrySet ()) {
5052 String className = entry .getKey ();
@@ -70,35 +72,11 @@ private Manifest createManifest(String agentClass, String mainClass) {
7072 }
7173
7274 @ SneakyThrows
73- private void addDependencies (JarOutputStream targetJar , String relocatePrefix ) {
75+ private void addAsmDependencies (JarOutputStream targetJar , String relocatePrefix ) {
7476 String baseName = Opcodes .class .getPackage ().getName ().replace ('.' , '/' );
75- addDependency (targetJar , Opcodes .class , baseName , relocatePrefix );
76- }
77-
78- @ SneakyThrows
79- private void addClassesToJar (JarOutputStream targetJar , Map <String , byte []> bytes , String relocatePrefix ) {
80- String dependencyPackage = Opcodes .class .getPackage ().getName ();
81- for (Map .Entry <String , byte []> entry : bytes .entrySet ()) {
82- addClassEntry (targetJar ,
83- entry .getKey (),
84- entry .getValue (),
85- dependencyPackage ,
86- relocatePrefix );
87- }
88- }
89-
90- @ SneakyThrows
91- private void addClassEntry (JarOutputStream targetJar , String className , byte [] classBytes ,
92- String dependencyPackage , String relocatePrefix ) {
93- targetJar .putNextEntry (new JarEntry (className .replace ('.' , '/' ) + ".class" ));
94- byte [] processedBytes = ClassBytesShrink .shrink (ClassRenameUtils .relocateClass (classBytes , dependencyPackage , relocatePrefix + dependencyPackage ), true );
95- targetJar .write (processedBytes );
96- targetJar .closeEntry ();
97- }
98-
99- @ SneakyThrows
100- public static void addDependency (JarOutputStream targetJar , Class <?> baseClass , String baseName , String relocatePrefix ) {
101- URL sourceUrl = baseClass .getProtectionDomain ().getCodeSource ().getLocation ();
77+ String commonBaseName = Remapper .class .getPackage ().getName ().replace ('.' , '/' );
78+ String treeBaseName = ClassNode .class .getPackage ().getName ().replace ('.' , '/' );
79+ URL sourceUrl = Opcodes .class .getProtectionDomain ().getCodeSource ().getLocation ();
10280 String sourceUrlString = sourceUrl .toString ();
10381 if (sourceUrlString .contains ("!BOOT-INF" )) {
10482 String path = sourceUrlString .substring ("jar:nested:" .length ());
@@ -118,7 +96,10 @@ public static void addDependency(JarOutputStream targetJar, Class<?> baseClass,
11896 JarEntry entry = entries .nextElement ();
11997 String entryName = entry .getName ();
12098 if (entryName .equals ("META-INF/MANIFEST.MF" )
121- || entryName .contains ("module-info.class" )) {
99+ || entryName .contains ("module-info.class" )
100+ || !entryName .startsWith (baseName )
101+ || entryName .startsWith (commonBaseName )
102+ || entryName .startsWith (treeBaseName )) {
122103 continue ;
123104 }
124105 if (!entry .isDirectory ()) {
@@ -144,6 +125,19 @@ public static void addDependency(JarOutputStream targetJar, Class<?> baseClass,
144125 }
145126 }
146127
128+ @ SneakyThrows
129+ private void addClassesToJar (JarOutputStream targetJar , Map <String , byte []> bytes , String relocatePrefix ) {
130+ String dependencyPackage = Opcodes .class .getPackage ().getName ();
131+ for (Map .Entry <String , byte []> entry : bytes .entrySet ()) {
132+ String className = entry .getKey ();
133+ byte [] classBytes = entry .getValue ();
134+ targetJar .putNextEntry (new JarEntry (className .replace ('.' , '/' ) + ".class" ));
135+ byte [] processedBytes = ClassBytesShrink .shrink (ClassRenameUtils .relocateClass (classBytes , dependencyPackage , relocatePrefix + dependencyPackage ), true );
136+ targetJar .write (processedBytes );
137+ targetJar .closeEntry ();
138+ }
139+ }
140+
147141 /**
148142 * Extracts a JAR file to a temporary directory
149143 *
0 commit comments