Fix PlatformDependent.newAtomic*FieldUpdater type safety

Motivation:

* newAtomicIntegerFieldUpdater and newAtomicLongFieldUpdater take a
class<?> so they're too lax
* newAtomicReferenceFieldUpdater takes a Class<U> so it's too strict
and can only be passe a rawtype parameter when dealing w/ generic
classes

Modifications:

Take a Class<? super T> parameter instead.

Result:

Better type safety and generics support.
This commit is contained in:
Stephane Landelle 2016-01-08 00:06:09 +01:00 committed by Norman Maurer
parent 8c8fa11004
commit b95dbb391f
5 changed files with 12 additions and 9 deletions

View File

@ -416,7 +416,7 @@ public final class PlatformDependent {
* use {@link AtomicReferenceFieldUpdater#newUpdater(Class, Class, String)} as fallback. * use {@link AtomicReferenceFieldUpdater#newUpdater(Class, Class, String)} as fallback.
*/ */
public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater( public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
Class<U> tclass, String fieldName) { Class<? super U> tclass, String fieldName) {
if (hasUnsafe()) { if (hasUnsafe()) {
try { try {
return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName); return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName);
@ -433,7 +433,7 @@ public final class PlatformDependent {
* use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback. * use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback.
*/ */
public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater( public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
Class<?> tclass, String fieldName) { Class<? super T> tclass, String fieldName) {
if (hasUnsafe()) { if (hasUnsafe()) {
try { try {
return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName); return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName);
@ -450,7 +450,7 @@ public final class PlatformDependent {
* use {@link AtomicLongFieldUpdater#newUpdater(Class, String)} as fallback. * use {@link AtomicLongFieldUpdater#newUpdater(Class, String)} as fallback.
*/ */
public static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater( public static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
Class<?> tclass, String fieldName) { Class<? super T> tclass, String fieldName) {
if (hasUnsafe()) { if (hasUnsafe()) {
try { try {
return PlatformDependent0.newAtomicLongFieldUpdater(tclass, fieldName); return PlatformDependent0.newAtomicLongFieldUpdater(tclass, fieldName);

View File

@ -269,17 +269,17 @@ final class PlatformDependent0 {
} }
static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater( static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
Class<U> tclass, String fieldName) throws Exception { Class<? super U> tclass, String fieldName) throws Exception {
return new UnsafeAtomicReferenceFieldUpdater<U, W>(UNSAFE, tclass, fieldName); return new UnsafeAtomicReferenceFieldUpdater<U, W>(UNSAFE, tclass, fieldName);
} }
static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater( static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
Class<?> tclass, String fieldName) throws Exception { Class<? super T> tclass, String fieldName) throws Exception {
return new UnsafeAtomicIntegerFieldUpdater<T>(UNSAFE, tclass, fieldName); return new UnsafeAtomicIntegerFieldUpdater<T>(UNSAFE, tclass, fieldName);
} }
static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater( static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
Class<?> tclass, String fieldName) throws Exception { Class<? super T> tclass, String fieldName) throws Exception {
return new UnsafeAtomicLongFieldUpdater<T>(UNSAFE, tclass, fieldName); return new UnsafeAtomicLongFieldUpdater<T>(UNSAFE, tclass, fieldName);
} }

View File

@ -25,7 +25,8 @@ final class UnsafeAtomicIntegerFieldUpdater<T> extends AtomicIntegerFieldUpdater
private final long offset; private final long offset;
private final Unsafe unsafe; private final Unsafe unsafe;
UnsafeAtomicIntegerFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException { UnsafeAtomicIntegerFieldUpdater(Unsafe unsafe, Class<? super T> tClass, String fieldName)
throws NoSuchFieldException {
Field field = tClass.getDeclaredField(fieldName); Field field = tClass.getDeclaredField(fieldName);
if (!Modifier.isVolatile(field.getModifiers())) { if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile"); throw new IllegalArgumentException("Must be volatile");

View File

@ -25,7 +25,8 @@ final class UnsafeAtomicLongFieldUpdater<T> extends AtomicLongFieldUpdater<T> {
private final long offset; private final long offset;
private final Unsafe unsafe; private final Unsafe unsafe;
UnsafeAtomicLongFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException { UnsafeAtomicLongFieldUpdater(Unsafe unsafe, Class<? super T> tClass, String fieldName)
throws NoSuchFieldException {
Field field = tClass.getDeclaredField(fieldName); Field field = tClass.getDeclaredField(fieldName);
if (!Modifier.isVolatile(field.getModifiers())) { if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile"); throw new IllegalArgumentException("Must be volatile");

View File

@ -25,7 +25,8 @@ final class UnsafeAtomicReferenceFieldUpdater<U, M> extends AtomicReferenceField
private final long offset; private final long offset;
private final Unsafe unsafe; private final Unsafe unsafe;
UnsafeAtomicReferenceFieldUpdater(Unsafe unsafe, Class<U> tClass, String fieldName) throws NoSuchFieldException { UnsafeAtomicReferenceFieldUpdater(Unsafe unsafe, Class<? super U> tClass, String fieldName)
throws NoSuchFieldException {
Field field = tClass.getDeclaredField(fieldName); Field field = tClass.getDeclaredField(fieldName);
if (!Modifier.isVolatile(field.getModifiers())) { if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile"); throw new IllegalArgumentException("Must be volatile");