When using the ClassFile, for certain contents of a class file, a call to MethodModel.code() throws an unspecified java.lang.IllegalArgumentException.
The sample code looks as follows:
ClassFile cf = ClassFile.of(...);
ClassModel model = cf.parse(...);
for (final MethodModel method : model.methods()) {
final Optional<CodeModel> methodCode = method.code();
...
This results in:
java.lang.IllegalArgumentException: Reading beyond classfile bounds
at java.base/jdk.internal.classfile.impl.ClassReaderImpl.outOfBoundsError(ClassReaderImpl.java:179)
at java.base/jdk.internal.classfile.impl.ClassReaderImpl.readU2(ClassReaderImpl.java:198)
at java.base/jdk.internal.classfile.impl.BoundAttribute$BoundCodeAttribute.<init>(BoundAttribute.java:956)
at java.base/jdk.internal.classfile.impl.CodeImpl.<init>(CodeImpl.java:83)
at java.base/jdk.internal.classfile.impl.AbstractAttributeMapper$CodeMapper.readAttribute(AbstractAttributeMapper.java:164)
at java.base/jdk.internal.classfile.impl.AbstractAttributeMapper$CodeMapper.readAttribute(AbstractAttributeMapper.java:155)
at java.base/jdk.internal.classfile.impl.BoundAttribute.readAttributes(BoundAttribute.java:143)
at java.base/jdk.internal.classfile.impl.MethodImpl.attributes(MethodImpl.java:94)
at java.base/java.lang.classfile.AttributedElement.findAttribute(AttributedElement.java:64)
at java.base/jdk.internal.classfile.impl.MethodImpl.code(MethodImpl.java:116)
...
This happens in Java 23 as well as latest JDK mainline (Java 24).