RFR: 4517644: Core reflection setter type conversion spec bugs [v3]

Chen Liang liach at openjdk.org
Thu Nov 6 20:43:37 UTC 2025


> ### 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. Class specification of field mentions widening/narrowing conversion rules that is not sensible for get/set Object accessors
> 4. Field.set misses the identity or widening in IAE clause (but mentioned in main body)
> 5. Field primitive setters incorrectly claim they are equivalent to set(instance, wrapper) which is wrong due to lack of boxing conversion
> 6. Field primitive setters refer to nonsense "unwrapping conversion"
> 
> ### Solutions
> 1. Make sure the unbox -> identity/widen process and the IAE conditions are present in both Field and Array.set
> 2. Add that boxing is absent for all primitive setters
> 3. Update Field class spec to mention the narrowing/widening conversion limits are for primitive accessors only
> 4. Fix the Field primitive setters' "unwrapping conversion" to be "identity or primitive widening conversion" as in Array primitive setters
> 5. Qualify the Field primitive setters assertion with "if this field is of a primitive type" to make it correct

Chen Liang has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains seven additional commits since the last revision:

 - Link
 - Update
 - Redundant
 - Stuff for the class spec
 - Merge branch 'master' of https://github.com/openjdk/jdk into doc/reflect-set
 - the narrowing conversion restriction is primitive-only
 - 4517644: (reflect spec) Array.set documentation hole for array/value ref type mismatch

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/28029/files
  - new: https://git.openjdk.org/jdk/pull/28029/files/5559e613..7fdcc3e5

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=28029&range=02
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=28029&range=01-02

  Stats: 72364 lines in 1035 files changed: 39096 ins; 27728 del; 5540 mod
  Patch: https://git.openjdk.org/jdk/pull/28029.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/28029/head:pull/28029

PR: https://git.openjdk.org/jdk/pull/28029


More information about the core-libs-dev mailing list