8216046: test/jdk/java/beans/PropertyEditor/Test6397609.java failing

Reviewed-by: alanb, serb
This commit is contained in:
Jie Fu 2019-02-03 10:00:14 +01:00 committed by Alan Bateman
parent eaa2b28750
commit f464c38ed6

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -32,31 +32,38 @@
*/ */
import java.beans.PropertyEditorManager; import java.beans.PropertyEditorManager;
import java.lang.ref.WeakReference;
public class Test6397609 { public class Test6397609 {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
MemoryClassLoader loader = new MemoryClassLoader(); Class<?> targetClass = Object.class;
PropertyEditorManager.registerEditor( Class<?> editorClass = new MemoryClassLoader().compile("Editor",
Object.class, "public class Editor extends java.beans.PropertyEditorSupport {}");
loader.compile("Editor", PropertyEditorManager.registerEditor(targetClass, editorClass);
"public class Editor extends java.beans.PropertyEditorSupport {}"));
if (!isEditorExist(Object.class)) { // trigger a gc
Object object = new Object();
var r = new WeakReference<Object>(object);
object = null;
while (r.get() != null) {
System.gc();
Thread.sleep(100);
}
if (PropertyEditorManager.findEditor(targetClass) == null) {
throw new Error("the editor is lost"); throw new Error("the editor is lost");
} }
loader = null; // clean the reference
if (isEditorExist(Object.class)) { // allow, and wait for, Editor class to be unloaded
var ref = new WeakReference<Class<?>>(editorClass);
editorClass = null;
while (ref.get() != null) {
System.gc();
Thread.sleep(100);
}
if (PropertyEditorManager.findEditor(targetClass) != null) {
throw new Error("unexpected editor is found"); throw new Error("unexpected editor is found");
} }
} }
private static boolean isEditorExist(Class type) {
for (int i = 0; i < 10; i++) {
System.gc(); // clean all weak references
if (null == PropertyEditorManager.findEditor(type)) {
return false;
}
}
return true;
}
} }