Skip to content

Commit 356c4d9

Browse files
author
Viktor Klang
committed
8355369: Remove setAccessible usage for setting final fields in java.util.concurrent
Reviewed-by: pminborg, dl, rgiulietti, alanb
1 parent cf96b10 commit 356c4d9

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListSet.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535

3636
package java.util.concurrent;
3737

38-
import java.lang.reflect.Field;
38+
import jdk.internal.misc.Unsafe;
39+
40+
import java.lang.invoke.VarHandle;
3941
import java.util.AbstractSet;
4042
import java.util.Collection;
4143
import java.util.Collections;
@@ -176,6 +178,8 @@ public ConcurrentSkipListSet<E> clone() {
176178
ConcurrentSkipListSet<E> clone =
177179
(ConcurrentSkipListSet<E>) super.clone();
178180
clone.setMap(new ConcurrentSkipListMap<E,Object>(m));
181+
// Needed to ensure safe publication of setMap()
182+
VarHandle.releaseFence();
179183
return clone;
180184
} catch (CloneNotSupportedException e) {
181185
throw new InternalError();
@@ -527,12 +531,11 @@ public Spliterator<E> spliterator() {
527531

528532
/** Initializes map field; for use in clone. */
529533
private void setMap(ConcurrentNavigableMap<E,Object> map) {
530-
try {
531-
Field mapField = ConcurrentSkipListSet.class.getDeclaredField("m");
532-
mapField.setAccessible(true);
533-
mapField.set(this, map);
534-
} catch (IllegalAccessException | NoSuchFieldException e) {
535-
throw new Error(e);
536-
}
534+
final Unsafe U = Unsafe.getUnsafe();
535+
U.putReference(
536+
this,
537+
U.objectFieldOffset(ConcurrentSkipListSet.class, "m"),
538+
map
539+
);
537540
}
538541
}

src/java.base/share/classes/java/util/concurrent/CopyOnWriteArrayList.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
package java.util.concurrent;
3636

3737
import java.lang.invoke.VarHandle;
38-
import java.lang.reflect.Field;
3938
import java.util.ArrayList;
4039
import java.util.Arrays;
4140
import java.util.Collection;
@@ -57,6 +56,7 @@
5756
import java.util.stream.Stream;
5857
import java.util.stream.StreamSupport;
5958
import jdk.internal.access.SharedSecrets;
59+
import jdk.internal.misc.Unsafe;
6060
import jdk.internal.util.ArraysSupport;
6161

6262
/**
@@ -2095,12 +2095,11 @@ public List<E> reversed() {
20952095

20962096
/** Initializes the lock; for use when deserializing or cloning. */
20972097
private void resetLock() {
2098-
try {
2099-
Field lockField = CopyOnWriteArrayList.class.getDeclaredField("lock");
2100-
lockField.setAccessible(true);
2101-
lockField.set(this, new Object());
2102-
} catch (IllegalAccessException | NoSuchFieldException e) {
2103-
throw new Error(e);
2104-
}
2098+
final Unsafe U = Unsafe.getUnsafe();
2099+
U.putReference(
2100+
this,
2101+
U.objectFieldOffset(CopyOnWriteArrayList.class, "lock"),
2102+
new Object()
2103+
);
21052104
}
21062105
}

src/java.base/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535

3636
package java.util.concurrent.atomic;
3737

38+
import jdk.internal.misc.Unsafe;
39+
3840
import java.lang.invoke.MethodHandles;
3941
import java.lang.invoke.VarHandle;
4042
import java.lang.reflect.Array;
41-
import java.lang.reflect.Field;
4243
import java.util.Arrays;
4344
import java.util.function.BinaryOperator;
4445
import java.util.function.UnaryOperator;
@@ -330,14 +331,13 @@ private void readObject(java.io.ObjectInputStream s)
330331
throw new java.io.InvalidObjectException("Not array type");
331332
if (a.getClass() != Object[].class)
332333
a = Arrays.copyOf((Object[])a, Array.getLength(a), Object[].class);
333-
try {
334334

335-
Field arrayField = AtomicReferenceArray.class.getDeclaredField("array");
336-
arrayField.setAccessible(true);
337-
arrayField.set(this, a);
338-
} catch (NoSuchFieldException | IllegalAccessException e) {
339-
throw new Error(e);
340-
}
335+
final Unsafe U = Unsafe.getUnsafe();
336+
U.putReference(
337+
this,
338+
U.objectFieldOffset(AtomicReferenceArray.class, "array"),
339+
a
340+
);
341341
}
342342

343343
// jdk9
@@ -523,5 +523,4 @@ public final boolean weakCompareAndSetAcquire(int i, E expectedValue, E newValue
523523
public final boolean weakCompareAndSetRelease(int i, E expectedValue, E newValue) {
524524
return AA.weakCompareAndSetRelease(array, i, expectedValue, newValue);
525525
}
526-
527526
}

0 commit comments

Comments
 (0)