RFR: 8361300: Document exceptions for Unsafe offset methods [v7]

Chen Liang liach at openjdk.org
Fri Aug 1 20:21:57 UTC 2025


On Fri, 1 Aug 2025 18:46:53 GMT, ExE Boss <duke at openjdk.org> wrote:

>> Chen Liang has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Test update advised by volkan
>
> src/java.base/share/classes/jdk/internal/misc/Unsafe.java line 1093:
> 
>> 1091:      * startup.  This should not be used to find fields in non-trusted code.
>> 1092:      * Use the {@link #objectFieldOffset(Field) Field}-accepting version for
>> 1093:      * arbitrary fields instead.
> 
> It’s also used to obtain offsets of fields in classes which have their fields filtered from reflection using `Reflection​::registerFieldsToFilter(…)`, such as fields in `java.lang.ClassLoader` or `java.lang.Module`.

I don't think this is ever the intention of [JDK-8182487](https://bugs.openjdk.org/browse/JDK-8182487) (928ca49c21d7e571e88dd27cf47033bf88d2dc71) - it is a side effect, and you can always query the VM for a Field object if you are determined, or java.lang.invoke.MemberName can be used to obtain an offset for filtered fields too.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25945#discussion_r2248804132


More information about the core-libs-dev mailing list