JDK-8342699 : java.lang.classfile.CodeModel.elementList() throws unspecified java.lang.IllegalArgumentException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2024-10-21
  • Updated: 2025-01-27
  • Resolved: 2025-01-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 24
24Resolved
Related Reports
Duplicate :  
Relates :  
Description
When using the ClassFile, for certain contents of a class file, a call to CodeModel.elementList() 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();
        if (methodCode.isEmpty()) {
            continue;
        }
        for (final CodeElement codeElement : methodCode.get().elementList()) {
            ...
    ...

This results in:

java.lang.IllegalArgumentException: Bytecode offset out of range; bci=26, codeLength=9
    at java.base/jdk.internal.classfile.impl.CodeImpl.getLabel(CodeImpl.java:101)
    at java.base/jdk.internal.classfile.impl.AnnotationReader.getLabel(AnnotationReader.java:174)
    at java.base/jdk.internal.classfile.impl.AnnotationReader.readLocalVarEntries(AnnotationReader.java:253)
    at java.base/jdk.internal.classfile.impl.AnnotationReader.readTypeAnnotation(AnnotationReader.java:201)
    at java.base/jdk.internal.classfile.impl.AnnotationReader.readTypeAnnotations(AnnotationReader.java:93)
    at java.base/jdk.internal.classfile.impl.BoundAttribute$BoundRuntimeVisibleTypeAnnotationsAttribute.annotations(BoundAttribute.java:833)
    at java.base/java.util.Optional.ifPresent(Optional.java:178)
    at java.base/jdk.internal.classfile.impl.CodeImpl.inflateTypeAnnotations(CodeImpl.java:332)
    at java.base/jdk.internal.classfile.impl.CodeImpl.inflateMetadata(CodeImpl.java:127)
    at java.base/jdk.internal.classfile.impl.CodeImpl.forEach(CodeImpl.java:166)
    at java.base/java.lang.classfile.CompoundElement.elementList(CompoundElement.java:89)
    ...

This was reproduced against JDK mainline (Java 24). The same is also reproduced in Java 23, although, the method elementList() doesn't exist in Java 23 and instead the call to CodeModel.elements() throws this same unspecified exception.
Comments
In 8342465, it's specified that all accessor methods in the Class-File API and builder methods may throw IAE when there's illegal format/unrepresentable data.
27-01-2025

IllegalArgumentException (and its sub-classes) is always expected as a reaction on an invalid class file. We should probably add a paragraph to the package info. It is not technically possible cover the whole (lazy initialized) Class-File API surface with javadoc describing all possible IAE cases.
06-11-2024

IllegalArgumentException (and its sub-classes) is always expected as a reaction on an invalid class file. We should probably add a paragraph to the package info. It is not technically possible cover the whole (lazy initialized) Class-File API surface with javadoc describing all possible IAE cases.
06-11-2024