Skip to content

Commit c274e42

Browse files
committed
Allow explicit specification of the experimental property.
1 parent 2aec967 commit c274e42

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/ClassFileVersion.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ public static ClassFileVersion ofMinorMajor(int versionNumber) {
212212
* @return The appropriate class file version.
213213
*/
214214
public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
215+
return ofJavaVersionString(javaVersionString, OpenedClassReader.EXPERIMENTAL);
216+
}
217+
218+
/**
219+
* Returns the Java class file by its representation by a version string in accordance to the formats known to <i>javac</i>.
220+
*
221+
* @param javaVersionString The Java version string.
222+
* @param experimental {@code true} if unknown version strings should be parsed as if they were known.
223+
* @return The appropriate class file version.
224+
*/
225+
public static ClassFileVersion ofJavaVersionString(String javaVersionString, boolean experimental) {
215226
if (javaVersionString.equals("1.1")) {
216227
return JAVA_V1;
217228
} else if (javaVersionString.equals("1.2")) {
@@ -259,7 +270,7 @@ public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
259270
} else if (javaVersionString.equals("1.23") || javaVersionString.equals("23")) {
260271
return JAVA_V23;
261272
} else {
262-
if (OpenedClassReader.EXPERIMENTAL) {
273+
if (experimental) {
263274
try {
264275
int version = Integer.parseInt(javaVersionString.startsWith("1.")
265276
? javaVersionString.substring(2)
@@ -282,6 +293,18 @@ public static ClassFileVersion ofJavaVersionString(String javaVersionString) {
282293
* @return A wrapper for the given Java class file version.
283294
*/
284295
public static ClassFileVersion ofJavaVersion(int javaVersion) {
296+
return ofJavaVersion(javaVersion, OpenedClassReader.EXPERIMENTAL);
297+
}
298+
299+
/**
300+
* Creates a class file version for a given major release of Java. Currently, all versions reaching from
301+
* Java 1 to Java 9 are supported.
302+
*
303+
* @param javaVersion The Java version.
304+
* @param experimental {@code true} if unknown Java versions should also be considered.
305+
* @return A wrapper for the given Java class file version.
306+
*/
307+
public static ClassFileVersion ofJavaVersion(int javaVersion, boolean experimental) {
285308
switch (javaVersion) {
286309
case 1:
287310
return JAVA_V1;
@@ -330,7 +353,7 @@ public static ClassFileVersion ofJavaVersion(int javaVersion) {
330353
case 23:
331354
return JAVA_V23;
332355
default:
333-
if (OpenedClassReader.EXPERIMENTAL && javaVersion > 0) {
356+
if (experimental && javaVersion > 0) {
334357
return new ClassFileVersion(BASE_VERSION + javaVersion);
335358
} else {
336359
throw new IllegalArgumentException("Unknown Java version: " + javaVersion);

byte-buddy-dep/src/main/java/net/bytebuddy/utility/AsmClassReader.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ interface Factory {
5656
*/
5757
AsmClassReader make(byte[] binaryRepresentation);
5858

59+
/**
60+
* Creates a class reader for a given class file.
61+
*
62+
* @param binaryRepresentation The class file's binary representation.
63+
* @param experimental {@code true} if unknown Java class files versions should also be considered.
64+
* @return A class reader representation for the supplied class file.
65+
*/
66+
AsmClassReader make(byte[] binaryRepresentation, boolean experimental);
67+
5968
/**
6069
* A default implementation that creates a pure ASM {@link ClassReader}.
6170
*/
@@ -72,6 +81,13 @@ enum Default implements Factory {
7281
public AsmClassReader make(byte[] binaryRepresentation) {
7382
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation));
7483
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
89+
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation, experimental));
90+
}
7591
}
7692
}
7793

byte-buddy-dep/src/main/java/net/bytebuddy/utility/OpenedClassReader.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public AsmClassReader make(byte[] binaryRepresentation) {
6565
return new AsmClassReader.Default(of(binaryRepresentation));
6666
}
6767

68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
72+
return new AsmClassReader.Default(of(binaryRepresentation, experimental));
73+
}
74+
6875
/**
6976
* A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
7077
*
@@ -77,16 +84,28 @@ private static <T> T doPrivileged(PrivilegedAction<T> action) {
7784
return action.run();
7885
}
7986

87+
8088
/**
8189
* Creates a class reader for the given binary representation of a class file.
8290
*
8391
* @param binaryRepresentation The binary representation of a class file to read.
8492
* @return An appropriate class reader.
8593
*/
8694
public static ClassReader of(byte[] binaryRepresentation) {
95+
return of(binaryRepresentation, EXPERIMENTAL);
96+
}
97+
98+
/**
99+
* Creates a class reader for the given binary representation of a class file.
100+
*
101+
* @param binaryRepresentation The binary representation of a class file to read.
102+
* @param experimental {@code true} if unknown class file versions should also be processed.
103+
* @return An appropriate class reader.
104+
*/
105+
public static ClassReader of(byte[] binaryRepresentation, boolean experimental) {
87106
ClassFileVersion classFileVersion = ClassFileVersion.ofClassFile(binaryRepresentation), latest = ClassFileVersion.latest();
88107
if (classFileVersion.isGreaterThan(latest)) {
89-
if (EXPERIMENTAL) {
108+
if (experimental) {
90109
binaryRepresentation[4] = (byte) (latest.getMinorVersion() >>> 8);
91110
binaryRepresentation[5] = (byte) latest.getMinorVersion();
92111
binaryRepresentation[6] = (byte) (latest.getMajorVersion() >>> 8);

0 commit comments

Comments
 (0)