Avoid name-clash with future java.lang.Record (#10470)

Motivation:
 Recent Intellij versions are starting to anticipate
 future versions of Java that include a
 `java.lang.Record` class, and the Intellij compiler
 gets confused by the `Record` class in our
 `ResorceLeakDetector`.

Modification:
 Rename our `Record` class to `TracerRecord`.
 This matches what the class is doing, while avoiding
 any future name clashes.

Result:
 Intellij can now compile the project again, even when
 configured to use a future (snapshot or early access)
 version of Java.
This commit is contained in:
Chris Vest 2020-08-11 20:51:39 +02:00 committed by GitHub
parent f58223982c
commit 3d7ec896ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -348,9 +348,9 @@ public class ResourceLeakDetector<T> {
extends WeakReference<Object> implements ResourceLeakTracker<T>, ResourceLeak { extends WeakReference<Object> implements ResourceLeakTracker<T>, ResourceLeak {
@SuppressWarnings("unchecked") // generics and updaters do not mix. @SuppressWarnings("unchecked") // generics and updaters do not mix.
private static final AtomicReferenceFieldUpdater<DefaultResourceLeak<?>, Record> headUpdater = private static final AtomicReferenceFieldUpdater<DefaultResourceLeak<?>, TraceRecord> headUpdater =
(AtomicReferenceFieldUpdater) (AtomicReferenceFieldUpdater)
AtomicReferenceFieldUpdater.newUpdater(DefaultResourceLeak.class, Record.class, "head"); AtomicReferenceFieldUpdater.newUpdater(DefaultResourceLeak.class, TraceRecord.class, "head");
@SuppressWarnings("unchecked") // generics and updaters do not mix. @SuppressWarnings("unchecked") // generics and updaters do not mix.
private static final AtomicIntegerFieldUpdater<DefaultResourceLeak<?>> droppedRecordsUpdater = private static final AtomicIntegerFieldUpdater<DefaultResourceLeak<?>> droppedRecordsUpdater =
@ -358,7 +358,7 @@ public class ResourceLeakDetector<T> {
AtomicIntegerFieldUpdater.newUpdater(DefaultResourceLeak.class, "droppedRecords"); AtomicIntegerFieldUpdater.newUpdater(DefaultResourceLeak.class, "droppedRecords");
@SuppressWarnings("unused") @SuppressWarnings("unused")
private volatile Record head; private volatile TraceRecord head;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private volatile int droppedRecords; private volatile int droppedRecords;
@ -379,7 +379,7 @@ public class ResourceLeakDetector<T> {
trackedHash = System.identityHashCode(referent); trackedHash = System.identityHashCode(referent);
allLeaks.add(this); allLeaks.add(this);
// Create a new Record so we always have the creation stacktrace included. // Create a new Record so we always have the creation stacktrace included.
headUpdater.set(this, new Record(Record.BOTTOM)); headUpdater.set(this, new TraceRecord(TraceRecord.BOTTOM));
this.allLeaks = allLeaks; this.allLeaks = allLeaks;
} }
@ -422,9 +422,9 @@ public class ResourceLeakDetector<T> {
private void record0(Object hint) { private void record0(Object hint) {
// Check TARGET_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords // Check TARGET_RECORDS > 0 here to avoid similar check before remove from and add to lastRecords
if (TARGET_RECORDS > 0) { if (TARGET_RECORDS > 0) {
Record oldHead; TraceRecord oldHead;
Record prevHead; TraceRecord prevHead;
Record newHead; TraceRecord newHead;
boolean dropped; boolean dropped;
do { do {
if ((prevHead = oldHead = headUpdater.get(this)) == null) { if ((prevHead = oldHead = headUpdater.get(this)) == null) {
@ -440,7 +440,7 @@ public class ResourceLeakDetector<T> {
} else { } else {
dropped = false; dropped = false;
} }
newHead = hint != null ? new Record(prevHead, hint) : new Record(prevHead); newHead = hint != null ? new TraceRecord(prevHead, hint) : new TraceRecord(prevHead);
} while (!headUpdater.compareAndSet(this, oldHead, newHead)); } while (!headUpdater.compareAndSet(this, oldHead, newHead));
if (dropped) { if (dropped) {
droppedRecordsUpdater.incrementAndGet(this); droppedRecordsUpdater.incrementAndGet(this);
@ -509,7 +509,7 @@ public class ResourceLeakDetector<T> {
@Override @Override
public String toString() { public String toString() {
Record oldHead = headUpdater.getAndSet(this, null); TraceRecord oldHead = headUpdater.getAndSet(this, null);
if (oldHead == null) { if (oldHead == null) {
// Already closed // Already closed
return EMPTY_STRING; return EMPTY_STRING;
@ -525,10 +525,10 @@ public class ResourceLeakDetector<T> {
int i = 1; int i = 1;
Set<String> seen = new HashSet<String>(present); Set<String> seen = new HashSet<String>(present);
for (; oldHead != Record.BOTTOM; oldHead = oldHead.next) { for (; oldHead != TraceRecord.BOTTOM; oldHead = oldHead.next) {
String s = oldHead.toString(); String s = oldHead.toString();
if (seen.add(s)) { if (seen.add(s)) {
if (oldHead.next == Record.BOTTOM) { if (oldHead.next == TraceRecord.BOTTOM) {
buf.append("Created at:").append(NEWLINE).append(s); buf.append("Created at:").append(NEWLINE).append(s);
} else { } else {
buf.append('#').append(i++).append(':').append(NEWLINE).append(s); buf.append('#').append(i++).append(':').append(NEWLINE).append(s);
@ -588,30 +588,30 @@ public class ResourceLeakDetector<T> {
} while (!excludedMethods.compareAndSet(oldMethods, newMethods)); } while (!excludedMethods.compareAndSet(oldMethods, newMethods));
} }
private static final class Record extends Throwable { private static final class TraceRecord extends Throwable {
private static final long serialVersionUID = 6065153674892850720L; private static final long serialVersionUID = 6065153674892850720L;
private static final Record BOTTOM = new Record(); private static final TraceRecord BOTTOM = new TraceRecord();
private final String hintString; private final String hintString;
private final Record next; private final TraceRecord next;
private final int pos; private final int pos;
Record(Record next, Object hint) { TraceRecord(TraceRecord next, Object hint) {
// This needs to be generated even if toString() is never called as it may change later on. // This needs to be generated even if toString() is never called as it may change later on.
hintString = hint instanceof ResourceLeakHint ? ((ResourceLeakHint) hint).toHintString() : hint.toString(); hintString = hint instanceof ResourceLeakHint ? ((ResourceLeakHint) hint).toHintString() : hint.toString();
this.next = next; this.next = next;
this.pos = next.pos + 1; this.pos = next.pos + 1;
} }
Record(Record next) { TraceRecord(TraceRecord next) {
hintString = null; hintString = null;
this.next = next; this.next = next;
this.pos = next.pos + 1; this.pos = next.pos + 1;
} }
// Used to terminate the stack // Used to terminate the stack
private Record() { private TraceRecord() {
hintString = null; hintString = null;
next = null; next = null;
pos = -1; pos = -1;