On Sat, 22 Nov 2025 07:37:01 GMT, Steve Armstrong <[email protected]> wrote:

> The three AtomicXxxFieldUpdater classes (AtomicIntegerFieldUpdater, 
> AtomicLongFieldUpdater, and AtomicReferenceFieldUpdater) contain duplicate 
> field validation and access checking logic in their constructors and helper 
> methods.
> 
> This change extracts the common validation and utility methods into a new 
> package-private class FieldUpdaterUtil to eliminate code duplication and 
> improve maintainability.
> 
> Changes:
> - Added new FieldUpdaterUtil class with static utility methods:
>   * validateField() - validates field type, volatile, and static checks
>   * computeAccessClass() - determines correct class for access checks
>   * isSamePackage() - checks if two classes are in same package
>   * isAncestor() - checks classloader delegation chain
> 
> - Updated AtomicIntegerFieldUpdater to use FieldUpdaterUtil
>   * Simplified constructor to use validateField() and computeAccessClass()
>   * Removed duplicate isAncestor() and isSamePackage() methods
> 
> - Updated AtomicLongFieldUpdater to use FieldUpdaterUtil
>   * Simplified constructor to use validateField() and computeAccessClass()
>   * Removed duplicate isAncestor() and isSamePackage() methods
> 
> - Updated AtomicReferenceFieldUpdater to use FieldUpdaterUtil
>   * Simplified constructor to use validateField() and computeAccessClass()
>   * Removed duplicate isAncestor() and isSamePackage() methods
> 
> Existing tests in test/jdk/java/util/concurrent/tck and 
> test/jdk/java/util/concurrent/atomic verify that the refactoring preserves 
> the original behavior.

This can also get rid of the last remaining uses of the legacy 
`sun.reflect.misc.ReflectUtil`:

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

(`jdk.internal.reflect.Reflection` is already imported for 
`Reflection​.getCallerClass()`)

src/java.base/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java
 line 395:

> 393:                 modifiers = field.getModifiers();
> 394:                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
> 395:                     caller, tclass, null, modifiers);

Suggestion:

                Reflection.ensureMemberAccess(
                    caller, tclass, null, modifiers);

src/java.base/share/classes/java/util/concurrent/atomic/AtomicLongFieldUpdater.java
 line 390:

> 388:                 modifiers = field.getModifiers();
> 389:                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
> 390:                     caller, tclass, null, modifiers);

Suggestion:

                Reflection.ensureMemberAccess(
                    caller, tclass, null, modifiers);

src/java.base/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
 line 352:

> 350:                 modifiers = field.getModifiers();
> 351:                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
> 352:                     caller, tclass, null, modifiers);

Suggestion:

                Reflection.ensureMemberAccess(
                    caller, tclass, null, modifiers);

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

PR Review: https://git.openjdk.org/jdk/pull/28464#pullrequestreview-3498156901
PR Review Comment: https://git.openjdk.org/jdk/pull/28464#discussion_r2554468849
PR Review Comment: https://git.openjdk.org/jdk/pull/28464#discussion_r2554469008
PR Review Comment: https://git.openjdk.org/jdk/pull/28464#discussion_r2554469107

Reply via email to