RFR: JDK-8277451: java.lang.reflect.Field::set on static field with invalid argument type should throw IAE

Mandy Chung mchung at openjdk.java.net
Sat Nov 20 19:20:35 UTC 2021


java.lang.reflect.Field::set on static field with invalid argument type should throw IAE.  But this regression is introduced by JEP 416 throwing NPE instead.

`ensureObj` is called as the first check of the `Field::set` method to ensure the receiver object is checked first before the argument.   For a Field instance with write-access, the method handle invocation will check the receiver.  Therefore for `Field::setXXX` methods to set a primitive value, `ensureObj` is only called if it's a read-only Field instance to ensure IllegalArgumentException is thrown first before IllegalAccessException to keep the existing behavior to avoid duplicated receiver check.

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

Commit messages:
 - JDK-8277451: java.lang.reflect.Field::set on static field with invalid argument type should throw IAE

Changes: https://git.openjdk.java.net/jdk/pull/6490/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=6490&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8277451
  Stats: 666 lines in 12 files changed: 623 ins; 17 del; 26 mod
  Patch: https://git.openjdk.java.net/jdk/pull/6490.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/6490/head:pull/6490

PR: https://git.openjdk.java.net/jdk/pull/6490


More information about the core-libs-dev mailing list