6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
authoralexp
Thu, 22 Oct 2009 18:42:18 +0400
changeset 4272 f4742294ef29
parent 4271 35fc7add4925
child 4273 19adfe928d6c
child 4276 ff9daeaa1e21
6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc Reviewed-by: hawtin
jdk/src/share/classes/javax/swing/JTable.java
jdk/src/share/classes/javax/swing/plaf/metal/OceanTheme.java
jdk/src/share/classes/sun/swing/SwingLazyValue.java
jdk/test/javax/swing/UIDefaults/6795356/SwingLazyValueTest.java
jdk/test/javax/swing/UIDefaults/6795356/TableTest.java
jdk/test/javax/swing/UIDefaults/6795356/bug6795356.java
--- a/jdk/src/share/classes/javax/swing/JTable.java	Thu Oct 22 16:39:29 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/JTable.java	Thu Oct 22 18:42:18 2009 +0400
@@ -57,6 +57,7 @@
 import sun.swing.SwingUtilities2.Section;
 import static sun.swing.SwingUtilities2.Section.*;
 import sun.swing.PrintingStatus;
+import sun.swing.SwingLazyValue;
 
 /**
  * The <code>JTable</code> is used to display and edit regular two-dimensional tables
@@ -5316,7 +5317,7 @@
     }
 
     private void setLazyValue(Hashtable h, Class c, String s) {
-        h.put(c, new UIDefaults.ProxyLazyValue(s));
+        h.put(c, new SwingLazyValue(s));
     }
 
     private void setLazyRenderer(Class c, String s) {
--- a/jdk/src/share/classes/javax/swing/plaf/metal/OceanTheme.java	Thu Oct 22 16:39:29 2009 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/OceanTheme.java	Thu Oct 22 18:42:18 2009 +0400
@@ -32,6 +32,7 @@
 import javax.swing.plaf.*;
 import sun.swing.SwingUtilities2;
 import sun.swing.PrintColorUIResource;
+import sun.swing.SwingLazyValue;
 
 /**
  * The default theme for the {@code MetalLookAndFeel}.
@@ -128,7 +129,7 @@
      * @throws NullPointerException if {@code table} is {@code null}
      */
     public void addCustomEntriesToTable(UIDefaults table) {
-        Object focusBorder = new UIDefaults.ProxyLazyValue(
+        Object focusBorder = new SwingLazyValue(
                       "javax.swing.plaf.BorderUIResource$LineBorderUIResource",
                       new Object[] {getPrimary1()});
         // .30 0 DDE8F3 white secondary2
--- a/jdk/src/share/classes/sun/swing/SwingLazyValue.java	Thu Oct 22 16:39:29 2009 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingLazyValue.java	Thu Oct 22 18:42:18 2009 +0400
@@ -26,6 +26,9 @@
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.lang.reflect.AccessibleObject;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import javax.swing.UIDefaults;
 
 /**
@@ -65,13 +68,15 @@
             if (methodName != null) {
                 Class[] types = getClassArray(args);
                 Method m = c.getMethod(methodName, types);
+                makeAccessible(m);
                 return m.invoke(c, args);
             } else {
                 Class[] types = getClassArray(args);
                 Constructor constructor = c.getConstructor(types);
+                makeAccessible(constructor);
                 return constructor.newInstance(args);
             }
-        } catch(Exception e) {
+        } catch (Exception e) {
             // Ideally we would throw an exception, unfortunately
             // often times there are errors as an initial look and
             // feel is loaded before one can be switched. Perhaps a
@@ -81,6 +86,15 @@
         return null;
     }
 
+    private void makeAccessible(final AccessibleObject object) {
+        AccessController.doPrivileged(new PrivilegedAction<Void>() {
+            public Void run() {
+                object.setAccessible(true);
+                return null;
+            }
+        });
+    }
+
     private Class[] getClassArray(Object[] args) {
         Class[] types = null;
         if (args!=null) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/UIDefaults/6795356/SwingLazyValueTest.java	Thu Oct 22 18:42:18 2009 +0400
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6795356
+ * @summary Checks that SwingLazyValue class correclty works
+ * @author Alexander Potochkin
+ * @run main SwingLazyValueTest
+ */
+
+import sun.swing.SwingLazyValue;
+
+import javax.swing.*;
+
+public class SwingLazyValueTest {
+
+    public static void main(String[] args) throws Exception {
+        if(new SwingLazyValue("javax.swing.JTable$DoubleRenderer").
+                createValue(null) == null) {
+            throw new RuntimeException("SwingLazyValue doesn't work");
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/UIDefaults/6795356/TableTest.java	Thu Oct 22 18:42:18 2009 +0400
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6795356
+ * @summary Checks that SwingLazyValue class correclty works
+ * @author Alexander Potochkin
+ * @run main/othervm TableTest
+ */
+
+import sun.applet.AppletSecurity;
+
+import javax.swing.*;
+import javax.swing.table.TableCellEditor;
+import java.awt.*;
+
+public class TableTest {
+
+    public static void main(String[] args) throws Exception {
+
+        KeyboardFocusManager.getCurrentKeyboardFocusManager();
+        System.setSecurityManager(new AppletSecurity());
+
+        JTable table = new JTable();
+        TableCellEditor de = table.getDefaultEditor(Double.class);
+        if (de == null) {
+            throw new RuntimeException("Table default editor is null");
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/UIDefaults/6795356/bug6795356.java	Thu Oct 22 18:42:18 2009 +0400
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6795356
+ * @summary Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc
+ * @author Alexander Potochkin
+ * @run main bug6795356
+ */
+
+import java.lang.ref.WeakReference;
+import java.security.ProtectionDomain;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.AccessControlContext;
+import java.util.LinkedList;
+import java.util.List;
+import javax.swing.*;
+
+public class bug6795356 {
+    volatile static WeakReference<ProtectionDomain> weakRef;
+
+    public static void main(String[] args) throws Exception {
+
+        ProtectionDomain domain = new ProtectionDomain(null, null);
+
+        AccessController.doPrivileged(new PrivilegedAction<Object>() {
+            public Object run() {
+
+                // this initialize ProxyLazyValues
+                UIManager.getLookAndFeel();
+
+                return null;
+            }
+        }, new AccessControlContext(new ProtectionDomain[]{domain}));
+
+        weakRef = new WeakReference<ProtectionDomain>(domain);
+        domain = null;
+
+        // Generate OutOfMemory and check the weak ref
+        generateOOME();
+
+        if (weakRef.get() != null) {
+            throw new RuntimeException("Memory leak found!");
+        }
+        System.out.println("Test passed");
+    }
+
+    static void generateOOME() {
+        List<Object> bigLeak = new LinkedList<Object>();
+        boolean oome = false;
+        System.out.print("Filling the heap");
+        try {
+            for(int i = 0; true ; i++) {
+                // Now, use up all RAM
+                bigLeak.add(new byte[1024 * 1024]);
+                System.out.print(".");
+
+                // Give the GC a change at that weakref
+                if (i % 10 == 0) {
+                    System.gc();
+                    try {
+                        Thread.sleep(100);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        } catch (OutOfMemoryError e) {
+            bigLeak = null;
+            oome = true;
+        }
+        System.out.println("");
+        if (!oome) {
+            throw new RuntimeException("Problem with test case - never got OOME");
+        }
+        System.out.println("Got OOME");
+    }
+}