Skip to content

Commit

Permalink
Fix PlatformDependent.newAtomic*FieldUpdater type safety
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slandelle authored and normanmaurer committed Jan 8, 2016
1 parent e578134 commit 1bee71f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public static int hashCodeAscii(CharSequence bytes) {
* use {@link AtomicReferenceFieldUpdater#newUpdater(Class, Class, String)} as fallback.
*/
public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
Class<U> tclass, String fieldName) {
Class<? super U> tclass, String fieldName) {
if (hasUnsafe()) {
try {
return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName);
Expand All @@ -572,7 +572,7 @@ public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUp
* use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback.
*/
public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
Class<?> tclass, String fieldName) {
Class<? super T> tclass, String fieldName) {
if (hasUnsafe()) {
try {
return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName);
Expand All @@ -589,7 +589,7 @@ public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
* use {@link AtomicLongFieldUpdater#newUpdater(Class, String)} as fallback.
*/
public static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
Class<?> tclass, String fieldName) {
Class<? super T> tclass, String fieldName) {
if (hasUnsafe()) {
try {
return PlatformDependent0.newAtomicLongFieldUpdater(tclass, fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,17 +484,17 @@ private static int hashCodeAsciiSanitizeFromChar(short value) {
}

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);
}

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);
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ final class UnsafeAtomicIntegerFieldUpdater<T> extends AtomicIntegerFieldUpdater
private final long offset;
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);
if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ final class UnsafeAtomicLongFieldUpdater<T> extends AtomicLongFieldUpdater<T> {
private final long offset;
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);
if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ final class UnsafeAtomicReferenceFieldUpdater<U, M> extends AtomicReferenceField
private final long offset;
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);
if (!Modifier.isVolatile(field.getModifiers())) {
throw new IllegalArgumentException("Must be volatile");
Expand Down

0 comments on commit 1bee71f

Please sign in to comment.