8039750: KSS: Replace MetalLazyValue with lambda
authormalenkov
Thu, 10 Apr 2014 22:26:43 +0400
changeset 24176 ee19a0634f04
parent 24175 3a8694713eab
child 24177 cbbea768ae65
8039750: KSS: Replace MetalLazyValue with lambda Reviewed-by: alexsch, serb
jdk/src/share/classes/javax/swing/JTable.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalLookAndFeel.java
jdk/test/javax/swing/JTable/8031971/bug8031971.java
jdk/test/javax/swing/plaf/metal/MetalLookAndFeel/Test8039750.java
--- a/jdk/src/share/classes/javax/swing/JTable.java	Thu Apr 10 13:22:23 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/JTable.java	Thu Apr 10 22:26:43 2014 +0400
@@ -34,7 +34,6 @@
 
 import java.beans.*;
 
-import java.io.Serializable;
 import java.io.ObjectOutputStream;
 import java.io.ObjectInputStream;
 import java.io.IOException;
@@ -5307,14 +5306,6 @@
         return retValue;
     }
 
-    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);
-    }
-
     /**
      * Creates default cell renderers for objects, numbers, doubles, dates,
      * booleans, and icons.
@@ -5325,24 +5316,32 @@
         defaultRenderersByColumnClass = new UIDefaults(8, 0.75f);
 
         // Objects
-        setLazyRenderer(Object.class, LazyClass.UIResource);
+        defaultRenderersByColumnClass.put(Object.class, (UIDefaults.LazyValue)
+                t -> new DefaultTableCellRenderer.UIResource());
 
         // Numbers
-        setLazyRenderer(Number.class, LazyClass.NumberRenderer);
+        defaultRenderersByColumnClass.put(Number.class, (UIDefaults.LazyValue)
+                t -> new NumberRenderer());
 
         // Doubles and Floats
-        setLazyRenderer(Float.class, LazyClass.DoubleRenderer);
-        setLazyRenderer(Double.class, LazyClass.DoubleRenderer);
+        defaultRenderersByColumnClass.put(Float.class, (UIDefaults.LazyValue)
+                t -> new DoubleRenderer());
+        defaultRenderersByColumnClass.put(Double.class, (UIDefaults.LazyValue)
+                t -> new DoubleRenderer());
 
         // Dates
-        setLazyRenderer(Date.class, LazyClass.DateRenderer);
+        defaultRenderersByColumnClass.put(Date.class, (UIDefaults.LazyValue)
+                t -> new DateRenderer());
 
         // Icons and ImageIcons
-        setLazyRenderer(Icon.class, LazyClass.IconRenderer);
-        setLazyRenderer(ImageIcon.class, LazyClass.IconRenderer);
+        defaultRenderersByColumnClass.put(Icon.class, (UIDefaults.LazyValue)
+                t -> new IconRenderer());
+        defaultRenderersByColumnClass.put(ImageIcon.class, (UIDefaults.LazyValue)
+                t -> new IconRenderer());
 
         // Booleans
-        setLazyRenderer(Boolean.class, LazyClass.BooleanRenderer);
+        defaultRenderersByColumnClass.put(Boolean.class, (UIDefaults.LazyValue)
+                t -> new BooleanRenderer());
     }
 
     /**
@@ -5420,10 +5419,6 @@
         }
     }
 
-    private void setLazyEditor(Class c, LazyClass lazyClass) {
-        setLazyValue(defaultEditorsByColumnClass, c, lazyClass);
-    }
-
     /**
      * Creates default cell editors for objects, numbers, and boolean values.
      * @see DefaultCellEditor
@@ -5432,13 +5427,16 @@
         defaultEditorsByColumnClass = new UIDefaults(3, 0.75f);
 
         // Objects
-        setLazyEditor(Object.class, LazyClass.GenericEditor);
+        defaultEditorsByColumnClass.put(Object.class, (UIDefaults.LazyValue)
+                t -> new GenericEditor());
 
         // Numbers
-        setLazyEditor(Number.class, LazyClass.NumberEditor);
+        defaultEditorsByColumnClass.put(Number.class, (UIDefaults.LazyValue)
+                t -> new NumberEditor());
 
         // Booleans
-        setLazyEditor(Boolean.class, LazyClass.BooleanEditor);
+        defaultEditorsByColumnClass.put(Boolean.class, (UIDefaults.LazyValue)
+                t -> new BooleanEditor());
     }
 
     /**
@@ -6544,54 +6542,6 @@
         }
     }
 
-    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/javax/swing/plaf/metal/MetalLookAndFeel.java	Thu Apr 10 13:22:23 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalLookAndFeel.java	Thu Apr 10 22:26:43 2014 +0400
@@ -34,13 +34,10 @@
 import javax.swing.text.DefaultEditorKit;
 
 import java.awt.Color;
-import java.awt.event.KeyEvent;
-import java.lang.reflect.*;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 
 import java.security.AccessController;
-import java.security.PrivilegedAction;
 
 import sun.awt.*;
 import sun.security.action.GetPropertyAction;
@@ -460,11 +457,9 @@
         LazyValue textFieldBorder =
             t -> MetalBorders.getTextFieldBorder();
 
-        Object dialogBorder = new MetalLazyValue(
-                          "javax.swing.plaf.metal.MetalBorders$DialogBorder");
+        LazyValue dialogBorder = t -> new MetalBorders.DialogBorder();
 
-        Object questionDialogBorder = new MetalLazyValue(
-                  "javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
+        LazyValue questionDialogBorder = t -> new MetalBorders.QuestionDialogBorder();
 
         Object fieldInputMap = new UIDefaults.LazyInputMap(new Object[] {
                            "ctrl C", DefaultEditorKit.copyAction,
@@ -1470,12 +1465,8 @@
             "ToolBar.floatingBackground", menuBackground,
             "ToolBar.dockingForeground", primaryControlDarkShadow,
             "ToolBar.floatingForeground", primaryControl,
-            "ToolBar.rolloverBorder", new MetalLazyValue(
-                         "javax.swing.plaf.metal.MetalBorders",
-                         "getToolBarRolloverBorder"),
-            "ToolBar.nonrolloverBorder", new MetalLazyValue(
-                         "javax.swing.plaf.metal.MetalBorders",
-                         "getToolBarNonrolloverBorder"),
+            "ToolBar.rolloverBorder", (LazyValue) t -> MetalBorders.getToolBarRolloverBorder(),
+            "ToolBar.nonrolloverBorder", (LazyValue) t -> MetalBorders.getToolBarNonrolloverBorder(),
             "ToolBar.ancestorInputMap",
                new UIDefaults.LazyInputMap(new Object[] {
                         "UP", "navigateUp",
@@ -1489,17 +1480,14 @@
                  }),
 
             // RootPane
-            "RootPane.frameBorder", new MetalLazyValue(
-                      "javax.swing.plaf.metal.MetalBorders$FrameBorder"),
+            "RootPane.frameBorder", (LazyValue) t -> new MetalBorders.FrameBorder(),
             "RootPane.plainDialogBorder", dialogBorder,
             "RootPane.informationDialogBorder", dialogBorder,
-            "RootPane.errorDialogBorder", new MetalLazyValue(
-                      "javax.swing.plaf.metal.MetalBorders$ErrorDialogBorder"),
+            "RootPane.errorDialogBorder", (LazyValue) t -> new MetalBorders.ErrorDialogBorder(),
             "RootPane.colorChooserDialogBorder", questionDialogBorder,
             "RootPane.fileChooserDialogBorder", questionDialogBorder,
             "RootPane.questionDialogBorder", questionDialogBorder,
-            "RootPane.warningDialogBorder", new MetalLazyValue(
-                    "javax.swing.plaf.metal.MetalBorders$WarningDialogBorder"),
+            "RootPane.warningDialogBorder", (LazyValue) t -> new MetalBorders.WarningDialogBorder(),
             // These bindings are only enabled when there is a default
             // button set on the rootpane.
             "RootPane.defaultButtonWindowKeyBindings", new Object[] {
@@ -2151,61 +2139,6 @@
 
 
     /**
-     * MetalLazyValue is a slimmed down version of <code>ProxyLaxyValue</code>.
-     * The code is duplicate so that it can get at the package private
-     * classes in metal.
-     */
-    private static class MetalLazyValue implements UIDefaults.LazyValue {
-        /**
-         * Name of the class to create.
-         */
-        private String className;
-        private String methodName;
-
-        MetalLazyValue(String name) {
-            this.className = name;
-        }
-
-        MetalLazyValue(String name, String methodName) {
-            this(name);
-            this.methodName = methodName;
-        }
-
-        public Object createValue(UIDefaults table) {
-            try {
-                final Class c = Class.forName(className);
-
-                if (methodName == null) {
-                    return c.newInstance();
-                }
-                Method method = AccessController.doPrivileged(
-                    new PrivilegedAction<Method>() {
-                    public Method run() {
-                        Method[] methods = c.getDeclaredMethods();
-                        for (int counter = methods.length - 1; counter >= 0;
-                             counter--) {
-                            if (methods[counter].getName().equals(methodName)){
-                                methods[counter].setAccessible(true);
-                                return methods[counter];
-                            }
-                        }
-                        return null;
-                    }
-                });
-                if (method != null) {
-                    return method.invoke(null, (Object[])null);
-                }
-            } catch (ClassNotFoundException cnfe) {
-            } catch (InstantiationException ie) {
-            } catch (IllegalAccessException iae) {
-            } catch (InvocationTargetException ite) {
-            }
-            return null;
-        }
-    }
-
-
-    /**
      * FontActiveValue redirects to the appropriate metal theme method.
      */
     private static class FontActiveValue implements UIDefaults.ActiveValue {
--- a/jdk/test/javax/swing/JTable/8031971/bug8031971.java	Thu Apr 10 13:22:23 2014 +0400
+++ b/jdk/test/javax/swing/JTable/8031971/bug8031971.java	Thu Apr 10 22:26:43 2014 +0400
@@ -29,7 +29,7 @@
 
 /**
  * @test
- * @bug 8031971
+ * @bug 8031971 8039750
  * @author Alexander Scherbatiy
  * @summary Use only public methods in the SwingLazyValue
  * @run main bug8031971
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/metal/MetalLookAndFeel/Test8039750.java	Thu Apr 10 22:26:43 2014 +0400
@@ -0,0 +1,76 @@
+/*
+ * 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 javax.swing.UIDefaults;
+import javax.swing.border.CompoundBorder;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+
+/*
+ * @test
+ * @bug 8039750
+ * @summary Tests MetalLazyValue removing
+ * @author Sergey Malenkov
+ */
+public class Test8039750 {
+    public static void main(String[] args) {
+        UIDefaults table= new MetalLookAndFeel().getDefaults();
+        test(table.get("ToolBar.rolloverBorder"),
+                "javax.swing.plaf.metal.MetalBorders$ButtonBorder",
+                "javax.swing.plaf.metal.MetalBorders$RolloverMarginBorder");
+        test(table.get("ToolBar.nonrolloverBorder"),
+                "javax.swing.plaf.metal.MetalBorders$ButtonBorder",
+                "javax.swing.plaf.metal.MetalBorders$RolloverMarginBorder");
+        test(table.get("RootPane.frameBorder"),
+                "javax.swing.plaf.metal.MetalBorders$FrameBorder");
+        test(table.get("RootPane.plainDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$DialogBorder");
+        test(table.get("RootPane.informationDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$DialogBorder");
+        test(table.get("RootPane.errorDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$ErrorDialogBorder");
+        test(table.get("RootPane.colorChooserDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
+        test(table.get("RootPane.fileChooserDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
+        test(table.get("RootPane.questionDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$QuestionDialogBorder");
+        test(table.get("RootPane.warningDialogBorder"),
+                "javax.swing.plaf.metal.MetalBorders$WarningDialogBorder");
+    }
+
+    private static void test(Object value, String name) {
+        if (!value.getClass().getName().equals(name)) {
+            throw new Error(name);
+        }
+    }
+
+    private static void test(Object value, String one, String two) {
+        if (value instanceof CompoundBorder) {
+            CompoundBorder border = (CompoundBorder) value;
+            test(border.getOutsideBorder(), one);
+            test(border.getInsideBorder(), two);
+        } else {
+            throw new Error("CompoundBorder");
+        }
+    }
+}