From 510f628ddf18c8a7aad5580903944fd9ca075f82 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Mon, 23 Sep 2024 23:04:22 +0200 Subject: [PATCH] Add constructor for multi-release jar files. --- .../net/bytebuddy/dynamic/ClassFileLocator.java | 17 ++++++++++++++--- .../dynamic/ClassFileLocatorForFolderTest.java | 6 ++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/ClassFileLocator.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/ClassFileLocator.java index e834f1f290..1797fa5e12 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/ClassFileLocator.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/ClassFileLocator.java @@ -1152,6 +1152,7 @@ class ForFolder implements ClassFileLocator { @HashCodeAndEqualsPlugin.ValueHandling(HashCodeAndEqualsPlugin.ValueHandling.Sort.IGNORE) private final int[] version; + /** * Creates a new class file locator for a folder structure of class files. * @@ -1159,9 +1160,19 @@ class ForFolder implements ClassFileLocator { * @throws IOException If an I/O exception occurs. */ public ForFolder(File folder) throws IOException { + this(folder, ClassFileVersion.JAVA_V8); + } + + /** + * Creates a new class file locator for a folder structure of class files. + * + * @param folder The base folder of the package structure. + * @param classFileVersion The class file version to consider for multi-release JAR files. + * @throws IOException If an I/O exception occurs. + */ + public ForFolder(File folder, ClassFileVersion classFileVersion) throws IOException { this.folder = folder; - int current = ClassFileVersion.ofThisVm().getJavaVersion(); - if (current < 9) { + if (classFileVersion.getJavaVersion() < 9) { version = new int[0]; } else { File manifest = new File(folder, "META-INF" + File.separatorChar + "MANIFEST.MF"); @@ -1183,7 +1194,7 @@ public ForFolder(File folder) throws IOException { for (int index = 0; index < file.length; index++) { try { int version = Integer.parseInt(file[index].getName()); - if (version <= current && version > 7) { + if (version <= classFileVersion.getJavaVersion() && version > 7) { versions.add(version); } } catch (NumberFormatException ignored) { diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorForFolderTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorForFolderTest.java index 0c4830d30a..ee2a7093c3 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorForFolderTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorForFolderTest.java @@ -1,7 +1,7 @@ package net.bytebuddy.dynamic; +import net.bytebuddy.ClassFileVersion; import net.bytebuddy.test.utility.JavaVersionRule; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -10,7 +10,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; -import java.util.Random; import java.util.jar.Manifest; import static org.hamcrest.CoreMatchers.is; @@ -67,7 +66,6 @@ public void testClose() throws Exception { } @Test - @JavaVersionRule.Enforce(9) public void testSuccessfulVersionLocation() throws Exception { File metaInf = new File(folder, "META-INF"); assertThat(metaInf.mkdir(), is(true)); @@ -91,7 +89,7 @@ public void testSuccessfulVersionLocation() throws Exception { } finally { outputStream.close(); } - ClassFileLocator classFileLocator = new ClassFileLocator.ForFolder(folder); + ClassFileLocator classFileLocator = new ClassFileLocator.ForFolder(folder, ClassFileVersion.JAVA_V9); ClassFileLocator.Resolution resolution = classFileLocator.locate(FOO + "." + BAR); assertThat(resolution.isResolved(), is(true)); assertThat(resolution.resolve(), is(new byte[]{VALUE, VALUE * 2}));