Skip to content

Commit 972bc3b

Browse files
author
Mandy Chung
committed
8256167: Convert JDK use of Reference::get to Reference::refersTo
Reviewed-by: sspitsyn, shade, dfuchs, alanb, kbarrett
1 parent 78be334 commit 972bc3b

12 files changed

Lines changed: 49 additions & 53 deletions

File tree

src/java.base/share/classes/java/io/ObjectStreamClass.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,7 +2411,7 @@ public boolean equals(Object obj) {
24112411
Class<?> referent;
24122412
return (nullClass ? other.nullClass
24132413
: ((referent = get()) != null) &&
2414-
(referent == other.get())) &&
2414+
(other.refersTo(referent))) &&
24152415
Arrays.equals(sigs, other.sigs);
24162416
} else {
24172417
return false;
@@ -2532,9 +2532,9 @@ public boolean equals(Object obj) {
25322532
}
25332533

25342534
if (obj instanceof WeakClassKey) {
2535-
Object referent = get();
2535+
Class<?> referent = get();
25362536
return (referent != null) &&
2537-
(referent == ((WeakClassKey) obj).get());
2537+
(((WeakClassKey) obj).refersTo(referent));
25382538
} else {
25392539
return false;
25402540
}

src/java.base/share/classes/java/lang/Thread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,9 +2044,9 @@ public boolean equals(Object obj) {
20442044
return true;
20452045

20462046
if (obj instanceof WeakClassKey) {
2047-
Object referent = get();
2047+
Class<?> referent = get();
20482048
return (referent != null) &&
2049-
(referent == ((WeakClassKey) obj).get());
2049+
(((WeakClassKey) obj).refersTo(referent));
20502050
} else {
20512051
return false;
20522052
}

src/java.base/share/classes/java/lang/ThreadLocal.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
2626
package java.lang;
2727
import jdk.internal.misc.TerminatingThreadLocal;
2828

29-
import java.lang.ref.*;
29+
import java.lang.ref.WeakReference;
3030
import java.util.Objects;
3131
import java.util.concurrent.atomic.AtomicInteger;
3232
import java.util.function.Supplier;
@@ -433,7 +433,7 @@ private ThreadLocalMap(ThreadLocalMap parentMap) {
433433
private Entry getEntry(ThreadLocal<?> key) {
434434
int i = key.threadLocalHashCode & (table.length - 1);
435435
Entry e = table[i];
436-
if (e != null && e.get() == key)
436+
if (e != null && e.refersTo(key))
437437
return e;
438438
else
439439
return getEntryAfterMiss(key, i, e);
@@ -453,10 +453,9 @@ private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
453453
int len = tab.length;
454454

455455
while (e != null) {
456-
ThreadLocal<?> k = e.get();
457-
if (k == key)
456+
if (e.refersTo(key))
458457
return e;
459-
if (k == null)
458+
if (e.refersTo(null))
460459
expungeStaleEntry(i);
461460
else
462461
i = nextIndex(i, len);
@@ -485,14 +484,12 @@ private void set(ThreadLocal<?> key, Object value) {
485484
for (Entry e = tab[i];
486485
e != null;
487486
e = tab[i = nextIndex(i, len)]) {
488-
ThreadLocal<?> k = e.get();
489-
490-
if (k == key) {
487+
if (e.refersTo(key)) {
491488
e.value = value;
492489
return;
493490
}
494491

495-
if (k == null) {
492+
if (e.refersTo(null)) {
496493
replaceStaleEntry(key, value, i);
497494
return;
498495
}
@@ -514,7 +511,7 @@ private void remove(ThreadLocal<?> key) {
514511
for (Entry e = tab[i];
515512
e != null;
516513
e = tab[i = nextIndex(i, len)]) {
517-
if (e.get() == key) {
514+
if (e.refersTo(key)) {
518515
e.clear();
519516
expungeStaleEntry(i);
520517
return;
@@ -551,22 +548,20 @@ private void replaceStaleEntry(ThreadLocal<?> key, Object value,
551548
for (int i = prevIndex(staleSlot, len);
552549
(e = tab[i]) != null;
553550
i = prevIndex(i, len))
554-
if (e.get() == null)
551+
if (e.refersTo(null))
555552
slotToExpunge = i;
556553

557554
// Find either the key or trailing null slot of run, whichever
558555
// occurs first
559556
for (int i = nextIndex(staleSlot, len);
560557
(e = tab[i]) != null;
561558
i = nextIndex(i, len)) {
562-
ThreadLocal<?> k = e.get();
563-
564559
// If we find key, then we need to swap it
565560
// with the stale entry to maintain hash table order.
566561
// The newly stale slot, or any other stale slot
567562
// encountered above it, can then be sent to expungeStaleEntry
568563
// to remove or rehash all of the other entries in run.
569-
if (k == key) {
564+
if (e.refersTo(key)) {
570565
e.value = value;
571566

572567
tab[i] = tab[staleSlot];
@@ -582,7 +577,7 @@ private void replaceStaleEntry(ThreadLocal<?> key, Object value,
582577
// If we didn't find stale entry on backward scan, the
583578
// first stale entry seen while scanning for key is the
584579
// first still present in the run.
585-
if (k == null && slotToExpunge == staleSlot)
580+
if (e.refersTo(null) && slotToExpunge == staleSlot)
586581
slotToExpunge = i;
587582
}
588583

@@ -673,7 +668,7 @@ private boolean cleanSomeSlots(int i, int n) {
673668
do {
674669
i = nextIndex(i, len);
675670
Entry e = tab[i];
676-
if (e != null && e.get() == null) {
671+
if (e != null && e.refersTo(null)) {
677672
n = len;
678673
removed = true;
679674
i = expungeStaleEntry(i);
@@ -733,7 +728,7 @@ private void expungeStaleEntries() {
733728
int len = tab.length;
734729
for (int j = 0; j < len; j++) {
735730
Entry e = tab[j];
736-
if (e != null && e.get() == null)
731+
if (e != null && e.refersTo(null))
737732
expungeStaleEntry(j);
738733
}
739734
}

src/java.base/share/classes/java/lang/invoke/DirectMethodHandle.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -402,9 +402,8 @@ private static boolean checkInitialized(MemberName member) {
402402
if (ref == null) {
403403
return true; // the final state
404404
}
405-
Thread clinitThread = ref.get();
406405
// Somebody may still be running defc.<clinit>.
407-
if (clinitThread == Thread.currentThread()) {
406+
if (ref.refersTo(Thread.currentThread())) {
408407
// If anybody is running defc.<clinit>, it is this thread.
409408
if (UNSAFE.shouldBeInitialized(defc))
410409
// Yes, we are running it; keep the barrier for now.

src/java.base/share/classes/java/lang/ref/Reference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void runFinalization() {
337337
*
338338
* @return The object to which this reference refers, or
339339
* {@code null} if this reference object has been cleared
340-
* @see refersTo
340+
* @see #refersTo
341341
*/
342342
@IntrinsicCandidate
343343
public T get() {

src/java.base/share/classes/java/lang/reflect/AccessibleObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ private static class Cache {
642642
}
643643

644644
boolean isCacheFor(Class<?> caller, Class<?> refc) {
645-
return callerRef.get() == caller && targetRef.get() == refc;
645+
return callerRef.refersTo(caller) && targetRef.refersTo(refc);
646646
}
647647

648648
static Object protectedMemberCallerCache(Class<?> caller, Class<?> refc) {
@@ -674,7 +674,7 @@ private boolean isAccessChecked(Class<?> caller) {
674674
if (cache instanceof WeakReference) {
675675
@SuppressWarnings("unchecked")
676676
WeakReference<Class<?>> ref = (WeakReference<Class<?>>) cache;
677-
return ref.get() == caller;
677+
return ref.refersTo(caller);
678678
}
679679
return false;
680680
}

src/java.base/share/classes/java/util/ResourceBundle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1755,7 +1755,7 @@ private static ResourceBundle findBundle(Module callerModule,
17551755
// Otherwise, remove the cached one since we can't keep
17561756
// the same bundles having different parents.
17571757
BundleReference bundleRef = cacheList.get(cacheKey);
1758-
if (bundleRef != null && bundleRef.get() == bundle) {
1758+
if (bundleRef != null && bundleRef.refersTo(bundle)) {
17591759
cacheList.remove(cacheKey, bundleRef);
17601760
}
17611761
}

src/java.base/share/classes/java/util/WeakHashMap.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,6 @@
2727

2828
import java.lang.ref.WeakReference;
2929
import java.lang.ref.ReferenceQueue;
30-
import java.util.concurrent.ThreadLocalRandom;
3130
import java.util.function.BiConsumer;
3231
import java.util.function.BiFunction;
3332
import java.util.function.Consumer;
@@ -283,8 +282,14 @@ static Object unmaskNull(Object key) {
283282
* Checks for equality of non-null reference x and possibly-null y. By
284283
* default uses Object.equals.
285284
*/
286-
private static boolean eq(Object x, Object y) {
287-
return x == y || x.equals(y);
285+
private boolean matchesKey(Entry<K,V> e, Object key) {
286+
// check if the given entry refers to the given key without
287+
// keeping a strong reference to the entry's referent
288+
if (e.refersTo(key)) return true;
289+
290+
// then check for equality if the referent is not cleared
291+
Object k = e.get();
292+
return k != null && key.equals(k);
288293
}
289294

290295
/**
@@ -399,7 +404,7 @@ public V get(Object key) {
399404
int index = indexFor(h, tab.length);
400405
Entry<K,V> e = tab[index];
401406
while (e != null) {
402-
if (e.hash == h && eq(k, e.get()))
407+
if (e.hash == h && matchesKey(e, k))
403408
return e.value;
404409
e = e.next;
405410
}
@@ -428,7 +433,7 @@ Entry<K,V> getEntry(Object key) {
428433
Entry<K,V>[] tab = getTable();
429434
int index = indexFor(h, tab.length);
430435
Entry<K,V> e = tab[index];
431-
while (e != null && !(e.hash == h && eq(k, e.get())))
436+
while (e != null && !(e.hash == h && matchesKey(e, k)))
432437
e = e.next;
433438
return e;
434439
}
@@ -452,7 +457,7 @@ public V put(K key, V value) {
452457
int i = indexFor(h, tab.length);
453458

454459
for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
455-
if (h == e.hash && eq(k, e.get())) {
460+
if (h == e.hash && matchesKey(e, k)) {
456461
V oldValue = e.value;
457462
if (value != oldValue)
458463
e.value = value;
@@ -515,8 +520,7 @@ private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
515520
src[j] = null;
516521
while (e != null) {
517522
Entry<K,V> next = e.next;
518-
Object key = e.get();
519-
if (key == null) {
523+
if (e.refersTo(null)) {
520524
e.next = null; // Help GC
521525
e.value = null; // " "
522526
size--;
@@ -597,7 +601,7 @@ public V remove(Object key) {
597601

598602
while (e != null) {
599603
Entry<K,V> next = e.next;
600-
if (h == e.hash && eq(k, e.get())) {
604+
if (h == e.hash && matchesKey(e, k)) {
601605
modCount++;
602606
size--;
603607
if (prev == e)

src/java.base/share/classes/jdk/internal/logger/BootstrapLogger.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -181,12 +181,10 @@ static void awaitPendingTasks() {
181181
// This is used by tests.
182182
static boolean isAlive() {
183183
WeakReference<ExecutorService> ref = executorRef;
184-
ExecutorService executor = ref == null ? null : ref.get();
185-
if (executor != null) return true;
184+
if (ref != null && !ref.refersTo(null)) return true;
186185
synchronized (BootstrapExecutors.class) {
187186
ref = executorRef;
188-
executor = ref == null ? null : ref.get();
189-
return executor != null;
187+
return ref != null && !ref.refersTo(null);
190188
}
191189
}
192190

src/java.logging/share/classes/java/util/logging/LogManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNe
777777
}
778778
LoggerWeakRef ref = namedLoggers.get(name);
779779
if (ref != null) {
780-
if (ref.get() == null) {
780+
if (ref.refersTo(null)) {
781781
// It's possible that the Logger was GC'ed after a
782782
// drainLoggerRefQueueBounded() call above so allow
783783
// a new one to be registered.

0 commit comments

Comments
 (0)