jdk/src/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java
changeset 16906 44dfee24cb71
parent 16011 890a7ed97f6c
child 18576 7a5c231327af
equal deleted inserted replaced
16905:0419f45c7761 16906:44dfee24cb71
    35 
    35 
    36 package java.util.concurrent.atomic;
    36 package java.util.concurrent.atomic;
    37 import java.util.function.UnaryOperator;
    37 import java.util.function.UnaryOperator;
    38 import java.util.function.BinaryOperator;
    38 import java.util.function.BinaryOperator;
    39 import sun.misc.Unsafe;
    39 import sun.misc.Unsafe;
       
    40 import sun.reflect.CallerSensitive;
       
    41 import sun.reflect.Reflection;
       
    42 
    40 import java.lang.reflect.Field;
    43 import java.lang.reflect.Field;
    41 import java.lang.reflect.Modifier;
    44 import java.lang.reflect.Modifier;
    42 import java.security.AccessController;
    45 import java.security.AccessController;
    43 import java.security.PrivilegedExceptionAction;
    46 import java.security.PrivilegedExceptionAction;
    44 import java.security.PrivilegedActionException;
    47 import java.security.PrivilegedActionException;
    94      * @throws RuntimeException with a nested reflection-based
    97      * @throws RuntimeException with a nested reflection-based
    95      * exception if the class does not hold field or is the wrong type,
    98      * exception if the class does not hold field or is the wrong type,
    96      * or the field is inaccessible to the caller according to Java language
    99      * or the field is inaccessible to the caller according to Java language
    97      * access control
   100      * access control
    98      */
   101      */
       
   102     @CallerSensitive
    99     public static <U, W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass, Class<W> vclass, String fieldName) {
   103     public static <U, W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass, Class<W> vclass, String fieldName) {
   100         return new AtomicReferenceFieldUpdaterImpl<U,W>(tclass,
   104         return new AtomicReferenceFieldUpdaterImpl<U,W>(tclass,
   101                                                         vclass,
   105                                                         vclass,
   102                                                         fieldName);
   106                                                         fieldName,
       
   107                                                         Reflection.getCallerClass());
   103     }
   108     }
   104 
   109 
   105     /**
   110     /**
   106      * Protected do-nothing constructor for use by subclasses.
   111      * Protected do-nothing constructor for use by subclasses.
   107      */
   112      */
   295          * screenings fail.
   300          * screenings fail.
   296          */
   301          */
   297 
   302 
   298         AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
   303         AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
   299                                         Class<V> vclass,
   304                                         Class<V> vclass,
   300                                         final String fieldName) {
   305                                         final String fieldName,
       
   306                                         final Class<?> caller)
       
   307         {
   301             final Field field;
   308             final Field field;
   302             final Class<?> fieldClass;
   309             final Class<?> fieldClass;
   303             final Class<?> caller;
       
   304             final int modifiers;
   310             final int modifiers;
   305             try {
   311             try {
   306                 field = AccessController.doPrivileged(
   312                 field = AccessController.doPrivileged(
   307                     new PrivilegedExceptionAction<Field>() {
   313                     new PrivilegedExceptionAction<Field>() {
   308                         public Field run() throws NoSuchFieldException {
   314                         public Field run() throws NoSuchFieldException {
   309                             return tclass.getDeclaredField(fieldName);
   315                             return tclass.getDeclaredField(fieldName);
   310                         }
   316                         }
   311                     });
   317                     });
   312                 caller = sun.reflect.Reflection.getCallerClass(3);
       
   313                 modifiers = field.getModifiers();
   318                 modifiers = field.getModifiers();
   314                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
   319                 sun.reflect.misc.ReflectUtil.ensureMemberAccess(
   315                     caller, tclass, null, modifiers);
   320                     caller, tclass, null, modifiers);
   316                 ClassLoader cl = tclass.getClassLoader();
   321                 ClassLoader cl = tclass.getClassLoader();
   317                 ClassLoader ccl = caller.getClassLoader();
   322                 ClassLoader ccl = caller.getClassLoader();