On Sat, 20 Nov 2021 19:13:43 GMT, Mandy Chung <[email protected]> wrote:
> 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.
src/java.base/share/classes/jdk/internal/reflect/MethodHandleFieldAccessorImpl.java
line 72:
> 70: */
> 71: protected IllegalArgumentException
> newGetIllegalArgumentException(Object o) {
> 72: return new IllegalArgumentException(getMessage(true, o != null ?
> o.getClass().getName() : ""));
Hello Mandy,
With this change the `getMessage` may get passed an empty string, so it would
end up printing something like `Can not get ... field <fieldname> on`. Do you
think the `getMessage` implementation should be tweaked not to print the "on"
if the `attemptedType` is empty?
-------------
PR: https://git.openjdk.java.net/jdk/pull/6490