On Wed, 27 Oct 2021 20:16:54 GMT, Mandy Chung <mch...@openjdk.org> wrote:

>> This reimplements core reflection with method handles.
>> 
>> For `Constructor::newInstance` and `Method::invoke`, the new implementation 
>> uses `MethodHandle`.  For `Field` accessor, the new implementation uses 
>> `VarHandle`.    For the first few invocations of one of these reflective 
>> methods on a specific reflective object we invoke the corresponding method 
>> handle directly. After that we spin a dynamic bytecode stub defined in a 
>> hidden class which loads the target `MethodHandle` or `VarHandle` from its 
>> class data as a dynamically computed constant. Loading the method handle 
>> from a constant allows JIT to inline the method-handle invocation in order 
>> to achieve good performance.
>> 
>> The VM's native reflection methods are needed during early startup, before 
>> the method-handle mechanism is initialized. That happens soon after 
>> System::initPhase1 and before System::initPhase2, after which we switch to 
>> using method handles exclusively.
>> 
>> The core reflection and method handle implementation are updated to handle 
>> chained caller-sensitive method calls [1] properly. A caller-sensitive 
>> method can define with a caller-sensitive adapter method that will take an 
>> additional caller class parameter and the adapter method will be annotated 
>> with `@CallerSensitiveAdapter` for better auditing.   See the detailed 
>> description from [2].
>> 
>> Ran tier1-tier8 tests.   
>> 
>> [1] https://bugs.openjdk.java.net/browse/JDK-8013527
>> [2] 
>> https://bugs.openjdk.java.net/browse/JDK-8271820?focusedCommentId=14439430&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14439430
>
> Mandy Chung has updated the pull request with a new target base due to a 
> merge or a rebase. The pull request now contains 43 commits:
> 
>  - fix copyright header and typo
>  - improve documentation of AccessorUtils
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> reimplement-method-invoke
>  - Fall back to the VM native reflection support if method handle cannot be 
> created
>  - fix bug id in test
>  - Merge
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> reimplement-method-invoke
>  - Merge branch 'master' of https://github.com/openjdk/jdk into 
> reimplement-method-invoke
>  - Separate paramFlgas into paramCount and flags fields
>  - Minor cleanup.  Improve javadoc in CallerSensitiveAdapter
>  - ... and 33 more: 
> https://git.openjdk.java.net/jdk/compare/9a3e9542...46cb306b

I further looked at the specification and implementation of `Field::set` and 
adding `java.lang.reflect` to the trusted final field package list does not 
relate to this issue.   I was confused with the `isTrustedFinalField` check 
which is supposed to check if the given `Field` object has read-only access.

A `Field` object has write access if and only if
   - setAccessible(true) has succeeded for this Field object;
   - the field is non-static; and
   - the field's declaring class is not a hidden class; and
   - the field's declaring class is not a record class.
   
The hack dropping `final` from a static final `Field` make a `Field` object 
which has only read-only access to get write access.  With JEP 416, this hack 
no longer works because the read-only access is set according to the original 
field modifiers but not the `Field` object.

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

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

Reply via email to