35 |
35 |
36 package java.util.concurrent.atomic; |
36 package java.util.concurrent.atomic; |
37 import java.util.function.IntUnaryOperator; |
37 import java.util.function.IntUnaryOperator; |
38 import java.util.function.IntBinaryOperator; |
38 import java.util.function.IntBinaryOperator; |
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; |
75 * @throws RuntimeException with a nested reflection-based |
78 * @throws RuntimeException with a nested reflection-based |
76 * exception if the class does not hold field or is the wrong type, |
79 * exception if the class does not hold field or is the wrong type, |
77 * or the field is inaccessible to the caller according to Java language |
80 * or the field is inaccessible to the caller according to Java language |
78 * access control |
81 * access control |
79 */ |
82 */ |
|
83 @CallerSensitive |
80 public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
84 public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
81 return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName); |
85 return new AtomicIntegerFieldUpdaterImpl<U>(tclass, fieldName, Reflection.getCallerClass()); |
82 } |
86 } |
83 |
87 |
84 /** |
88 /** |
85 * Protected do-nothing constructor for use by subclasses. |
89 * Protected do-nothing constructor for use by subclasses. |
86 */ |
90 */ |
363 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
367 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
364 private final long offset; |
368 private final long offset; |
365 private final Class<T> tclass; |
369 private final Class<T> tclass; |
366 private final Class<?> cclass; |
370 private final Class<?> cclass; |
367 |
371 |
368 AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName) { |
372 AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, |
|
373 final String fieldName, |
|
374 final Class<?> caller) |
|
375 { |
369 final Field field; |
376 final Field field; |
370 final Class<?> caller; |
|
371 final int modifiers; |
377 final int modifiers; |
372 try { |
378 try { |
373 field = AccessController.doPrivileged( |
379 field = AccessController.doPrivileged( |
374 new PrivilegedExceptionAction<Field>() { |
380 new PrivilegedExceptionAction<Field>() { |
375 public Field run() throws NoSuchFieldException { |
381 public Field run() throws NoSuchFieldException { |
376 return tclass.getDeclaredField(fieldName); |
382 return tclass.getDeclaredField(fieldName); |
377 } |
383 } |
378 }); |
384 }); |
379 caller = sun.reflect.Reflection.getCallerClass(3); |
|
380 modifiers = field.getModifiers(); |
385 modifiers = field.getModifiers(); |
381 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
386 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
382 caller, tclass, null, modifiers); |
387 caller, tclass, null, modifiers); |
383 ClassLoader cl = tclass.getClassLoader(); |
388 ClassLoader cl = tclass.getClassLoader(); |
384 ClassLoader ccl = caller.getClassLoader(); |
389 ClassLoader ccl = caller.getClassLoader(); |