changeset 455:826806a193c8

6795356: Leak caused by javax.swing.UIDefaults.ProxyLazyValue.acc Reviewed-by: hawtin
author alexp
date Tue, 26 Oct 2010 15:21:25 +0100
parents 9216ec4e4c14
children 2ea7a84adb54
files src/share/classes/javax/swing/JTable.java src/share/classes/javax/swing/plaf/metal/OceanTheme.java src/share/classes/sun/swing/SwingLazyValue.java test/javax/swing/UIDefaults/6795356/SwingLazyValueTest.java test/javax/swing/UIDefaults/6795356/TableTest.java test/javax/swing/UIDefaults/6795356/bug6795356.java
diffstat 6 files changed, 215 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/JTable.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/javax/swing/JTable.java	Tue Oct 26 15:21:25 2010 +0100
@@ -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
@@ -5309,7 +5310,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/src/share/classes/javax/swing/plaf/metal/OceanTheme.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/javax/swing/plaf/metal/OceanTheme.java	Tue Oct 26 15:21:25 2010 +0100
@@ -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/src/share/classes/sun/swing/SwingLazyValue.java	Tue Sep 14 10:45:38 2010 -0400
+++ b/src/share/classes/sun/swing/SwingLazyValue.java	Tue Oct 26 15:21:25 2010 +0100
@@ -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;
 
 /**
@@ -66,13 +69,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
@@ -82,6 +87,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/test/javax/swing/UIDefaults/6795356/SwingLazyValueTest.java	Tue Oct 26 15:21:25 2010 +0100
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.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/test/javax/swing/UIDefaults/6795356/TableTest.java	Tue Oct 26 15:21:25 2010 +0100
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.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/test/javax/swing/UIDefaults/6795356/bug6795356.java	Tue Oct 26 15:21:25 2010 +0100
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2009, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.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");
+    }
+}