Skip to content

Commit

Permalink
LDEV-5106 - improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Oct 15, 2024
1 parent 01e6a77 commit a83683c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 49 deletions.
89 changes: 42 additions & 47 deletions core/src/main/java/lucee/transformer/bytecode/util/ASMUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1248,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.V22;
}
}
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.1.1.100-RC"/>
<property name="version" value="6.1.1.101-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion 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.1.1.100-RC</version>
<version>6.1.1.101-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit a83683c

Please sign in to comment.