Skip to content

Commit

Permalink
Add constructor for multi-release jar files.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 23, 2024
1 parent eaf7f4f commit 510f628
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1152,16 +1152,27 @@ 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.
*
* @param folder The base folder of the package structure.
* @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");
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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}));
Expand Down

0 comments on commit 510f628

Please sign in to comment.