35 |
35 |
36 package java.util.concurrent.atomic; |
36 package java.util.concurrent.atomic; |
37 import java.util.function.LongUnaryOperator; |
37 import java.util.function.LongUnaryOperator; |
38 import java.util.function.LongBinaryOperator; |
38 import java.util.function.LongBinaryOperator; |
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> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
84 public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) { |
|
85 Class<?> caller = Reflection.getCallerClass(); |
81 if (AtomicLong.VM_SUPPORTS_LONG_CAS) |
86 if (AtomicLong.VM_SUPPORTS_LONG_CAS) |
82 return new CASUpdater<U>(tclass, fieldName); |
87 return new CASUpdater<U>(tclass, fieldName, caller); |
83 else |
88 else |
84 return new LockedUpdater<U>(tclass, fieldName); |
89 return new LockedUpdater<U>(tclass, fieldName, caller); |
85 } |
90 } |
86 |
91 |
87 /** |
92 /** |
88 * Protected do-nothing constructor for use by subclasses. |
93 * Protected do-nothing constructor for use by subclasses. |
89 */ |
94 */ |
363 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
368 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
364 private final long offset; |
369 private final long offset; |
365 private final Class<T> tclass; |
370 private final Class<T> tclass; |
366 private final Class<?> cclass; |
371 private final Class<?> cclass; |
367 |
372 |
368 CASUpdater(final Class<T> tclass, final String fieldName) { |
373 CASUpdater(final Class<T> tclass, final String fieldName, final Class<?> caller) { |
369 final Field field; |
374 final Field field; |
370 final Class<?> caller; |
|
371 final int modifiers; |
375 final int modifiers; |
372 try { |
376 try { |
373 field = AccessController.doPrivileged( |
377 field = AccessController.doPrivileged( |
374 new PrivilegedExceptionAction<Field>() { |
378 new PrivilegedExceptionAction<Field>() { |
375 public Field run() throws NoSuchFieldException { |
379 public Field run() throws NoSuchFieldException { |
376 return tclass.getDeclaredField(fieldName); |
380 return tclass.getDeclaredField(fieldName); |
377 } |
381 } |
378 }); |
382 }); |
379 caller = sun.reflect.Reflection.getCallerClass(3); |
|
380 modifiers = field.getModifiers(); |
383 modifiers = field.getModifiers(); |
381 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
384 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
382 caller, tclass, null, modifiers); |
385 caller, tclass, null, modifiers); |
383 ClassLoader cl = tclass.getClassLoader(); |
386 ClassLoader cl = tclass.getClassLoader(); |
384 ClassLoader ccl = caller.getClassLoader(); |
387 ClassLoader ccl = caller.getClassLoader(); |
488 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
491 private static final Unsafe unsafe = Unsafe.getUnsafe(); |
489 private final long offset; |
492 private final long offset; |
490 private final Class<T> tclass; |
493 private final Class<T> tclass; |
491 private final Class<?> cclass; |
494 private final Class<?> cclass; |
492 |
495 |
493 LockedUpdater(final Class<T> tclass, final String fieldName) { |
496 LockedUpdater(final Class<T> tclass, final String fieldName, final Class<?> caller) { |
494 Field field = null; |
497 Field field = null; |
495 Class<?> caller = null; |
|
496 int modifiers = 0; |
498 int modifiers = 0; |
497 try { |
499 try { |
498 field = AccessController.doPrivileged( |
500 field = AccessController.doPrivileged( |
499 new PrivilegedExceptionAction<Field>() { |
501 new PrivilegedExceptionAction<Field>() { |
500 public Field run() throws NoSuchFieldException { |
502 public Field run() throws NoSuchFieldException { |
501 return tclass.getDeclaredField(fieldName); |
503 return tclass.getDeclaredField(fieldName); |
502 } |
504 } |
503 }); |
505 }); |
504 caller = sun.reflect.Reflection.getCallerClass(3); |
|
505 modifiers = field.getModifiers(); |
506 modifiers = field.getModifiers(); |
506 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
507 sun.reflect.misc.ReflectUtil.ensureMemberAccess( |
507 caller, tclass, null, modifiers); |
508 caller, tclass, null, modifiers); |
508 ClassLoader cl = tclass.getClassLoader(); |
509 ClassLoader cl = tclass.getClassLoader(); |
509 ClassLoader ccl = caller.getClassLoader(); |
510 ClassLoader ccl = caller.getClassLoader(); |