package demo; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Opcodes; import java.lang.classfile.ClassFile; /** What happens when the class file is newer than the reader. Patches the major-version bytes of Target.class. */ public class VersionLimits { static byte[] withMajor(byte[] original, int major) { byte[] b = original.clone(); b[6] = (byte) (major >> 8); b[7] = (byte) major; return b; } static String tryClassFile(byte[] b) { try { var cm = ClassFile.of().parse(b); return "parsed, " + cm.methods().size() + " methods"; } catch (Throwable t) { return t.getClass().getName() + ": " + t.getMessage(); } } static String tryAsm(byte[] b) { try { int[] n = {0}; new ClassReader(b).accept(new ClassVisitor(Opcodes.ASM9) { @Override public org.objectweb.asm.MethodVisitor visitMethod(int a, String nm, String d, String s, String[] e) { n[0]++; return null; } }, 0); return "parsed, " + n[0] + " methods"; } catch (Throwable t) { return t.getClass().getName() + ": " + t.getMessage(); } } public static void main(String[] args) { byte[] t = Bytes.of(Target.class); System.out.println("running on Java " + Runtime.version().feature() + "; ClassFile.latestMajorVersion() = " + ClassFile.latestMajorVersion() + "; ASM Opcodes.V25=" + Opcodes.V25 + ", V28=" + Opcodes.V28); for (int major : new int[]{69, 70, 72, 73, 99}) { byte[] b = withMajor(t, major); System.out.println("major " + major + ":"); System.out.println(" java.lang.classfile : " + tryClassFile(b)); System.out.println(" ASM 9.11.1 : " + tryAsm(b)); } Check.that(tryClassFile(withMajor(t, 69)).startsWith("parsed"), "major 69 parses with java.lang.classfile"); Check.that(tryClassFile(withMajor(t, ClassFile.latestMajorVersion() + 1)).startsWith("java.lang.IllegalArgumentException"), "one version above the JDK's own is rejected by java.lang.classfile"); Check.that(tryAsm(withMajor(t, Opcodes.V28)).startsWith("parsed"), "ASM 9.11.1 still reads major 72 (Java 28)"); Check.that(tryAsm(withMajor(t, Opcodes.V28 + 1)).startsWith("java.lang.IllegalArgumentException"), "ASM 9.11.1 rejects major 73"); Check.finish(); } }