RFR: 4517644: Core reflection setter type conversion spec bugs

Chen Liang liach at openjdk.org
Wed Oct 29 14:28:33 UTC 2025


On Tue, 28 Oct 2025 22:23:59 GMT, Chen Liang <liach at openjdk.org> wrote:

> ### Current Status
> In core reflection, there are a few holes with the documentation on the type conversions performed on the different setters. They generally follow this process:
> 
> 1. The Object-valued setters checks the destination type. If that type is primitive, unbox the object.
> 2. Perform identity or (primitive/reference, depending on destination type) widening conversions.
> 
> For each step, they can fail with IllegalArgumentException, which has been the long standing behavior.
> 
> Note that this process is more restrictive than the JLS 5.2 assignment context, which allows boxing conversions, while the primitive reflective setters consistently reject them.
> 
> ### Problems
> There are some problems with current specs:
> 1. No mention that boxing is never done for primitive-typed setters
> 2. Array.set missing description for the final identity or widening conversion (reference or primitive) and the associated IAE condition
> 3. Field.set misses the identity or widening in IAE clause (but mentioned in main body)
> 4. Field primitive setters incorrectly claim they are equivalent to `set(instance, wrapper)` which is wrong due to lack of boxing conversion
> 5. Field primitive setters refer to nonsense "unwrapping conversion"
> 
> ### Solutions
> 1. Make sure the unbox -> identity/widen process and the IAE conditoins are present in both Field and Array.set
> 2. Add that boxing is absent for all primitive setters
> 3. Fix the Field primitive setters' "unwrapping conversion" to be "identity or primitive widening conversion" as in Array primitive setters
> 4. Qualify the Field primitive setters assertion with "if this field is of a primitive type" to make it correct

I think that paragraph most likely applies to the primitive accessors, because Object `set` are already performing necessary casts as narrowing conversions, and Object `get` never narrows its result. I should qualify this paragraph.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/28029#issuecomment-3461852729


More information about the core-libs-dev mailing list