Skip to content

Parse byte/short/char/boolean/float array attributes#36917

Open
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/classfile-primitive-array-attributes
Open

Parse byte/short/char/boolean/float array attributes#36917
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/classfile-primitive-array-attributes

Conversation

@junhyeong9812

Copy link
Copy Markdown

Overview

On JDK 24+, annotation metadata is read through the java.lang.classfile based
ClassFileAnnotationDelegate (selected by MetadataReaderFactoryDelegate).

Problem

parseArrayValue() converted only int, long and double array attributes into primitive
arrays. byte, short, char, boolean and float arrays fell through to the default branch
and were boxed into Byte[], Short[], Character[], Boolean[] and Float[]. When such an
attribute is later accessed, TypeMappedAnnotation adapts the value against the declared
attribute type (e.g. byte[]) and throws:

java.lang.IllegalStateException: Attribute 'byteValue' ... should be compatible with byte[]
but a java.lang.Byte[] value was returned

Empty arrays returned Object[0] and were handled separately, which masked the issue. The
ASM-based reader returns genuine primitive arrays (see MergedAnnotationMetadataVisitorTests),
confirming the intended behavior; the ClassFile reader should match.

Fix

Add explicit cases for OfByte/OfShort/OfChar/OfBoolean/OfFloat that build the
corresponding primitive array. (Stream offers no mapToByte/mapToShort/mapToChar/
mapToBoolean/mapToFloat, so a small loop is used, alongside the existing OfInt/OfLong/
OfDouble cases.)

A regression test is added under src/test/java24 (the java24Test source set); it fails on
the current code with the IllegalStateException above and passes with the fix.

The java.lang.classfile based ClassFileAnnotationDelegate only converted
int, long and double array attributes into primitive arrays; byte,
short, char, boolean and float arrays fell through to the default branch
and were boxed (Byte[], Short[], Character[], Boolean[], Float[]). On
JDK 24+ this made TypeMappedAnnotation reject the value with an
IllegalStateException when such an attribute was accessed, since the
declared attribute type is the primitive array. Empty arrays returned an
Object[0] and were unaffected, masking the issue.

Convert these element types to primitive arrays as well, matching the
ASM-based reader.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants