Hi,
A gc was added after "PropertyEditorManager.registerEditor(targetClass,
editorClass);" based on Alan's patch.
Would you please review it and give me some advice? Thanks.
-------------------------------------------------------------
public class Test6397609 {
public static void main(String[] args) throws Exception {
Class<?> targetClass = Object.class;
Class<?> editorClass = new MemoryClassLoader().compile("Editor",
"public class Editor extends
java.beans.PropertyEditorSupport {}");
PropertyEditorManager.registerEditor(targetClass, editorClass);
// 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");
}
// 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");
}
}
}
-------------------------------------------------------------
For the full patch, please see the attachment.
I benefit a lot from the technical discussion with Alan and Sergey.
Thanks again.
Best regards,
Jie
On 2019年01月07日 20:02, Alan Bateman wrote:
On 07/01/2019 01:26, Fu Jie wrote:
Hi,
Thanks Alan for your guidance. I like this patch and it seems very nice.
Do you think we need to trigger a GC after
"PropertyEditorManager.registerEditor(targetClass, editorClass);"?
Okay, probably add a sleep too so that there is some time for
reference processing (the original test didn't do this).
Also in my patch I see I put the sleep before the GC, it should be the
other way around.
-Alan.
diff -r 22baf8054a40 test/jdk/java/beans/PropertyEditor/Test6397609.java
--- a/test/jdk/java/beans/PropertyEditor/Test6397609.java Sat Jan 05 10:48:54 2019 +0800
+++ b/test/jdk/java/beans/PropertyEditor/Test6397609.java Mon Jan 07 22:23:05 2019 +0800
@@ -32,31 +32,38 @@
*/
import java.beans.PropertyEditorManager;
+import java.lang.ref.WeakReference;
public class Test6397609 {
public static void main(String[] args) throws Exception {
- MemoryClassLoader loader = new MemoryClassLoader();
- PropertyEditorManager.registerEditor(
- Object.class,
- loader.compile("Editor",
- "public class Editor extends java.beans.PropertyEditorSupport {}"));
+ Class<?> targetClass = Object.class;
+ Class<?> editorClass = new MemoryClassLoader().compile("Editor",
+ "public class Editor extends java.beans.PropertyEditorSupport {}");
+ PropertyEditorManager.registerEditor(targetClass, editorClass);
- 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");
}
- 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");
}
}
-
- 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;
- }
}