Skip to content

Commit

Permalink
LDEV-5106 - update ASM library to support Java up to 24 and improve e…
Browse files Browse the repository at this point in the history
…xception in case java version is not supported
  • Loading branch information
michaeloffner committed Oct 15, 2024
2 parents 338c8f8 + a83683c commit 81c0a2d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 60 deletions.
10 changes: 5 additions & 5 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -296,31 +296,31 @@
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-analysis</artifactId>
<version>9.6</version>
<version>9.7.1</version>
</dependency>
<dependency>
<groupId>org.lucee</groupId>
Expand Down
92 changes: 44 additions & 48 deletions core/src/main/java/lucee/transformer/bytecode/util/ASMUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,8 @@ public static int getJavaVersionForBytecodeGeneration() {
else if ("20".equals(vs) || "20.0".equals(vs)) javaBytecodeVersion = Opcodes.V20;
else if ("21".equals(vs) || "21.0".equals(vs)) javaBytecodeVersion = Opcodes.V21;
else if ("22".equals(vs) || "22.0".equals(vs)) javaBytecodeVersion = Opcodes.V22;
else if ("23".equals(vs) || "23.0".equals(vs)) javaBytecodeVersion = 0 << 16 | 67; // FUTURE use constant when exist
else if ("23".equals(vs) || "23.0".equals(vs)) javaBytecodeVersion = Opcodes.V23;
else if ("24".equals(vs) || "24.0".equals(vs)) javaBytecodeVersion = Opcodes.V24;
}

// we do not use the version of the JVM by default, because this would limit the use of lucee
Expand All @@ -1247,53 +1248,48 @@ public static int getJavaVersionForBytecodeGeneration() {
}

public static String toStringVersion(int javaBytecodeVersion) {
switch (javaBytecodeVersion) {
case Opcodes.V1_2:
return "1.2";
case Opcodes.V1_3:
return "1.3";
case Opcodes.V1_4:
return "1.4";
case Opcodes.V1_5:
return "1.5";
case Opcodes.V1_6:
return "1.6";
case Opcodes.V1_7:
return "1.7";
case Opcodes.V1_8:
return "1.8";
case Opcodes.V9:
return "9";
case Opcodes.V10:
return "10";
case Opcodes.V11:
return "11";
case Opcodes.V12:
return "12";
case Opcodes.V13:
return "13";
case Opcodes.V14:
return "14";
case Opcodes.V15:
return "15";
case Opcodes.V16:
return "16";
case Opcodes.V17:
return "17";
case Opcodes.V18:
return "18";
case Opcodes.V19:
return "19";
case Opcodes.V20:
return "20";
case Opcodes.V21:
return "21";
case Opcodes.V22:
return "22";
case (0 << 16 | 67):
return "23";
}
return "unknown";
int majorVersion = javaBytecodeVersion & 0xFF; // Extracts the rightmost 8 bits (i.e., the major version)

if (majorVersion >= 46 && majorVersion <= 52) {
// Java 1.x versions (1.2 to 1.8)
return "1." + (majorVersion - 44);
}
else if (majorVersion >= 53) {
// Java 9 and above (including future versions)
return String.valueOf(majorVersion - 44);
}
else {
// Unsupported or unknown versions
return "unknown or unsupported version (" + majorVersion + ")";
}
}

public static String toStringVersionFromBytceodeVersion(int majorVersion, String defaultValue) {
if (majorVersion >= 46 && majorVersion <= 52) {
// Java 1.x versions (1.2 to 1.8)
return "1." + (majorVersion - 44);
}
else if (majorVersion >= 53) {
// Java 9 and above
return String.valueOf(majorVersion - 44);
}
else {
// Unsupported or unknown versions
return defaultValue;
}
}

public static String getJavaVersionFromException(IllegalArgumentException iae, String defaultValue) {
String msg = iae == null ? null : iae.getMessage();
// msg = "Unsupported class file major version 52";
if (StringUtil.isEmpty(msg)) return defaultValue;

int nbr = Caster.toIntValue(ListUtil.last(msg, ' '), -1);
if (nbr == -1) return defaultValue;
return toStringVersionFromBytceodeVersion(nbr, defaultValue);
}

public static int getMaxVersion() {
return Opcodes.V24;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lucee.commons.lang.types.RefInteger;
import lucee.commons.lang.types.RefIntegerImpl;
import lucee.runtime.converter.JavaConverter.ObjectInputStreamImpl;
import lucee.transformer.bytecode.util.ASMUtil;
import lucee.transformer.dynamic.meta.Clazz;
import lucee.transformer.dynamic.meta.Constructor;
import lucee.transformer.dynamic.meta.FunctionMember;
Expand Down Expand Up @@ -305,6 +306,19 @@ public void visit(int version, int access, String name, String signature, String
try {
_getFunctionMembers(cl.loadClass(Type.getObjectType(superName).getClassName()), members, log);
}
catch (IllegalArgumentException iae) {
String v = ASMUtil.getJavaVersionFromException(iae, null);
if (v != null) {
throw new RuntimeException("The class [" + superName + "] was compiled with Java version [" + v + "], "
+ "which is not supported by the current version of ASM. The highest supported version is ["
+ ASMUtil.toStringVersionFromBytceodeVersion(ASMUtil.getMaxVersion(), "unknown") + "]. ");

}
throw iae;
}
catch (RuntimeException re) {
throw re;
}
catch (Exception e) {
if (log != null) log.error("dynamic", e);
}
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.108-SNAPSHOT"/>
<property name="version" value="6.2.0.109-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
12 changes: 6 additions & 6 deletions loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.108-SNAPSHOT</version>
<version>6.2.0.109-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down Expand Up @@ -580,30 +580,30 @@
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>9.6</version>
<version>9.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-analysis</artifactId>
<version>9.6</version>
<version>9.7.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>9.6</version>
<version>9.7.1</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 81c0a2d

Please sign in to comment.