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

ExE Boss duke at openjdk.org
Fri Aug 1 18:55:58 UTC 2025


On Fri, 1 Aug 2025 16:16:01 GMT, Chen Liang <liach at openjdk.org> wrote:

>> Unsafe throws IAE for misusing static vs instance fields, and it's revealed that AtomicXxxFieldUpdaters are using this mechanism to reject static fields. This is not a good practice, but we can at least document this so we don't accidentally introduce problems.
>
> 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`.

src/java.base/share/classes/jdk/internal/misc/Unsafe.java line 1108:

> 1106:         if (result < 0) {
> 1107:             String type = switch ((int) result) {
> 1108:                 case -2 -> "a static field";

Suggestion:

                case -2 -> "is a static field";

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

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


More information about the core-libs-dev mailing list