Skip to content

Commit 6bf719d

Browse files
author
SendaoYan
committed
8305186: Reference.waitForReferenceProcessing should be more accessible to tests
Backport-of: d8f012ea2a0514020434d5db6047e36941e9349b
1 parent 255a49c commit 6bf719d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/lib/jdk/test/whitebox/WhiteBox.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
package jdk.test.whitebox;
2525

2626
import java.lang.management.MemoryUsage;
27+
import java.lang.ref.Reference;
2728
import java.lang.reflect.Executable;
29+
import java.lang.reflect.InaccessibleObjectException;
30+
import java.lang.reflect.InvocationTargetException;
31+
import java.lang.reflect.Method;
2832
import java.util.Arrays;
2933
import java.util.List;
3034
import java.util.function.BiFunction;
@@ -526,6 +530,53 @@ public void clearInlineCaches(boolean preserve_static_stubs) {
526530
// Force Full GC
527531
public native void fullGC();
528532

533+
// Infrastructure for waitForReferenceProcessing()
534+
private static volatile Method waitForReferenceProcessingMethod = null;
535+
536+
private static Method getWaitForReferenceProcessingMethod() {
537+
Method wfrp = waitForReferenceProcessingMethod;
538+
if (wfrp == null) {
539+
try {
540+
wfrp = Reference.class.getDeclaredMethod("waitForReferenceProcessing");
541+
wfrp.setAccessible(true);
542+
assert wfrp.getReturnType() == Boolean.class;
543+
Class<?>[] ev = wfrp.getExceptionTypes();
544+
assert ev.length == 1;
545+
assert ev[0] == InterruptedException.class;
546+
waitForReferenceProcessingMethod = wfrp;
547+
} catch (InaccessibleObjectException e) {
548+
throw new RuntimeException("Need to add @modules java.base/java.lang.ref:open to test?", e);
549+
} catch (NoSuchMethodException e) {
550+
throw new RuntimeException(e);
551+
}
552+
}
553+
return wfrp;
554+
}
555+
556+
/**
557+
* Wait for reference processing, via Reference.waitForReferenceProcessing().
558+
* Callers of this method will need the
559+
* @modules java.base/java.lang.ref:open
560+
* jtreg tag.
561+
*
562+
* This method should usually be called after a call to WhiteBox.fullGC().
563+
*/
564+
public boolean waitForReferenceProcessing() throws InterruptedException {
565+
try {
566+
Method wfrp = getWaitForReferenceProcessingMethod();
567+
return (Boolean) wfrp.invoke(null);
568+
} catch (IllegalAccessException e) {
569+
throw new RuntimeException("Shouldn't happen, we call setAccessible()", e);
570+
} catch (InvocationTargetException e) {
571+
Throwable cause = e.getCause();
572+
if (cause instanceof InterruptedException) {
573+
throw (InterruptedException) cause;
574+
} else {
575+
throw new RuntimeException(e);
576+
}
577+
}
578+
}
579+
529580
// Returns true if the current GC supports concurrent collection control.
530581
public native boolean supportsConcurrentGCBreakpoints();
531582

0 commit comments

Comments
 (0)