8031971: Use only public methods in the SwingLazyValue
authoralexsch
Fri, 14 Feb 2014 18:01:22 +0400
changeset 23295 6c61adf6111e
parent 23294 f53c3359f3bb
child 23296 0acf698eee6a
8031971: Use only public methods in the SwingLazyValue Reviewed-by: serb, pchelko
jdk/src/share/classes/javax/swing/JTable.java
jdk/src/share/classes/sun/swing/SwingLazyValue.java
jdk/test/javax/swing/JTable/8031971/bug8031971.java
--- a/jdk/src/share/classes/javax/swing/JTable.java	Fri Feb 14 02:19:19 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/JTable.java	Fri Feb 14 18:01:22 2014 +0400
@@ -5308,12 +5308,12 @@
         return retValue;
     }
 
-    private void setLazyValue(Hashtable h, Class c, String s) {
-        h.put(c, new SwingLazyValue(s));
-    }
-
-    private void setLazyRenderer(Class c, String s) {
-        setLazyValue(defaultRenderersByColumnClass, c, s);
+    private void setLazyValue(Hashtable h, Class c, LazyClass lazyClass) {
+        h.put(c, new TableLazyValue(lazyClass));
+    }
+
+    private void setLazyRenderer(Class c, LazyClass lazyClass) {
+        setLazyValue(defaultRenderersByColumnClass, c, lazyClass);
     }
 
     /**
@@ -5326,24 +5326,24 @@
         defaultRenderersByColumnClass = new UIDefaults(8, 0.75f);
 
         // Objects
-        setLazyRenderer(Object.class, "javax.swing.table.DefaultTableCellRenderer$UIResource");
+        setLazyRenderer(Object.class, LazyClass.UIResource);
 
         // Numbers
-        setLazyRenderer(Number.class, "javax.swing.JTable$NumberRenderer");
+        setLazyRenderer(Number.class, LazyClass.NumberRenderer);
 
         // Doubles and Floats
-        setLazyRenderer(Float.class, "javax.swing.JTable$DoubleRenderer");
-        setLazyRenderer(Double.class, "javax.swing.JTable$DoubleRenderer");
+        setLazyRenderer(Float.class, LazyClass.DoubleRenderer);
+        setLazyRenderer(Double.class, LazyClass.DoubleRenderer);
 
         // Dates
-        setLazyRenderer(Date.class, "javax.swing.JTable$DateRenderer");
+        setLazyRenderer(Date.class, LazyClass.DateRenderer);
 
         // Icons and ImageIcons
-        setLazyRenderer(Icon.class, "javax.swing.JTable$IconRenderer");
-        setLazyRenderer(ImageIcon.class, "javax.swing.JTable$IconRenderer");
+        setLazyRenderer(Icon.class, LazyClass.IconRenderer);
+        setLazyRenderer(ImageIcon.class, LazyClass.IconRenderer);
 
         // Booleans
-        setLazyRenderer(Boolean.class, "javax.swing.JTable$BooleanRenderer");
+        setLazyRenderer(Boolean.class, LazyClass.BooleanRenderer);
     }
 
     /**
@@ -5421,8 +5421,8 @@
         }
     }
 
-    private void setLazyEditor(Class c, String s) {
-        setLazyValue(defaultEditorsByColumnClass, c, s);
+    private void setLazyEditor(Class c, LazyClass lazyClass) {
+        setLazyValue(defaultEditorsByColumnClass, c, lazyClass);
     }
 
     /**
@@ -5433,13 +5433,13 @@
         defaultEditorsByColumnClass = new UIDefaults(3, 0.75f);
 
         // Objects
-        setLazyEditor(Object.class, "javax.swing.JTable$GenericEditor");
+        setLazyEditor(Object.class, LazyClass.GenericEditor);
 
         // Numbers
-        setLazyEditor(Number.class, "javax.swing.JTable$NumberEditor");
+        setLazyEditor(Number.class, LazyClass.NumberEditor);
 
         // Booleans
-        setLazyEditor(Boolean.class, "javax.swing.JTable$BooleanEditor");
+        setLazyEditor(Boolean.class, LazyClass.BooleanEditor);
     }
 
     /**
@@ -6545,6 +6545,53 @@
         }
     }
 
+    private enum LazyClass {
+
+        UIResource,
+        NumberRenderer,
+        DoubleRenderer,
+        DateRenderer,
+        IconRenderer,
+        BooleanRenderer,
+        GenericEditor,
+        NumberEditor,
+        BooleanEditor,
+    }
+
+    private static class TableLazyValue implements UIDefaults.LazyValue {
+
+        private LazyClass type;
+
+        public TableLazyValue(LazyClass type) {
+            this.type = type;
+        }
+
+        @Override
+        public Object createValue(UIDefaults table) {
+            switch (type) {
+                case UIResource:
+                    return new DefaultTableCellRenderer.UIResource();
+                case NumberRenderer:
+                    return new NumberRenderer();
+                case DoubleRenderer:
+                    return new DoubleRenderer();
+                case DateRenderer:
+                    return new DateRenderer();
+                case IconRenderer:
+                    return new IconRenderer();
+                case BooleanRenderer:
+                    return new BooleanRenderer();
+                case GenericEditor:
+                    return new GenericEditor();
+                case NumberEditor:
+                    return new NumberEditor();
+                case BooleanEditor:
+                    return new BooleanEditor();
+                default:
+                    return null;
+            }
+        }
+    }
 
 /////////////////
 // Accessibility support
--- a/jdk/src/share/classes/sun/swing/SwingLazyValue.java	Fri Feb 14 02:19:19 2014 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingLazyValue.java	Fri Feb 14 18:01:22 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2014, 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
@@ -26,9 +26,6 @@
 
 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;
 import sun.reflect.misc.ReflectUtil;
 
@@ -69,12 +66,10 @@
             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) {
@@ -87,15 +82,6 @@
         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/JTable/8031971/bug8031971.java	Fri Feb 14 18:01:22 2014 +0400
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2014, 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.
+ */
+import java.util.Date;
+import java.util.Hashtable;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+
+/**
+ * @test
+ * @bug 8031971
+ * @author Alexander Scherbatiy
+ * @summary Use only public methods in the SwingLazyValue
+ * @run main bug8031971
+ */
+public class bug8031971 {
+
+    static Object[][] RENDERERS = {
+        {Object.class, "javax.swing.table.DefaultTableCellRenderer$UIResource"},
+        {Number.class, "javax.swing.JTable$NumberRenderer"},
+        {Float.class, "javax.swing.JTable$DoubleRenderer"},
+        {Double.class, "javax.swing.JTable$DoubleRenderer"},
+        {Date.class, "javax.swing.JTable$DateRenderer"},
+        {Icon.class, "javax.swing.JTable$IconRenderer"},
+        {ImageIcon.class, "javax.swing.JTable$IconRenderer"},
+        {Boolean.class, "javax.swing.JTable$BooleanRenderer"}
+    };
+
+    static Object[][] EDITORS = {
+        {Object.class, "javax.swing.JTable$GenericEditor"},
+        {Number.class, "javax.swing.JTable$NumberEditor"},
+        {Boolean.class, "javax.swing.JTable$BooleanEditor"}
+    };
+
+    public static void main(String[] args) throws Exception {
+
+        SwingUtilities.invokeAndWait(() -> {
+
+            TestTable table = new TestTable();
+            test(table.getDefaultRenderersByColumnClass(), RENDERERS);
+            test(table.getDefaultEditorsByColumnClass(), EDITORS);
+        });
+    }
+
+    static void test(Hashtable table, Object[][] values) {
+        for (int i = 0; i < values.length; i++) {
+            test(table.get(values[i][0]), (String) values[i][1]);
+        }
+    }
+
+    static void test(Object obj, String className) {
+        if (!obj.getClass().getCanonicalName().equals(className.replace('$', '.'))) {
+            throw new RuntimeException("Wrong value!");
+        }
+    }
+
+    static class TestTable extends JTable {
+
+        Hashtable getDefaultRenderersByColumnClass() {
+            return defaultRenderersByColumnClass;
+        }
+
+        Hashtable getDefaultEditorsByColumnClass() {
+            return defaultEditorsByColumnClass;
+        }
+    }
+}