8029960: Remove reflection from Swing classes
authoralexsch
Mon, 17 Feb 2014 13:41:50 +0400
changeset 23297 2946ef51ee24
parent 23296 0acf698eee6a
child 23298 1c729daf92a0
8029960: Remove reflection from Swing classes Reviewed-by: serb, pchelko
jdk/src/share/classes/java/awt/Component.java
jdk/src/share/classes/java/awt/event/InputEvent.java
jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java
jdk/src/share/classes/sun/awt/AWTAccessor.java
jdk/src/share/classes/sun/swing/SwingUtilities2.java
--- a/jdk/src/share/classes/java/awt/Component.java	Fri Feb 14 20:24:43 2014 +0400
+++ b/jdk/src/share/classes/java/awt/Component.java	Mon Feb 17 13:41:50 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 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
@@ -976,6 +976,17 @@
             public void revalidateSynchronously(Component comp) {
                 comp.revalidateSynchronously();
             }
+
+            @Override
+            public void createBufferStrategy(Component comp, int numBuffers,
+                    BufferCapabilities caps) throws AWTException {
+                comp.createBufferStrategy(numBuffers, caps);
+            }
+
+            @Override
+            public BufferStrategy getBufferStrategy(Component comp) {
+                return comp.getBufferStrategy();
+            }
         });
     }
 
--- a/jdk/src/share/classes/java/awt/event/InputEvent.java	Fri Feb 14 20:24:43 2014 +0400
+++ b/jdk/src/share/classes/java/awt/event/InputEvent.java	Mon Feb 17 13:41:50 2014 +0400
@@ -298,6 +298,10 @@
                 public int[] getButtonDownMasks() {
                     return InputEvent.getButtonDownMasks();
                 }
+
+                public boolean canAccessSystemClipboard(InputEvent event) {
+                    return event.canAccessSystemClipboard;
+                }
             });
     }
 
--- a/jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java	Fri Feb 14 20:24:43 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java	Mon Feb 17 13:41:50 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 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
@@ -32,6 +32,9 @@
 import java.util.*;
 
 import com.sun.java.swing.SwingUtilities3;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import sun.awt.AWTAccessor;
 
 import sun.awt.SubRegionShowable;
 import sun.java2d.SunGraphics2D;
@@ -69,12 +72,6 @@
     // if we get a blit strategy.
     //
 
-    //
-    // Methods used to create BufferStrategy for Applets.
-    //
-    private static Method COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
-    private static Method COMPONENT_GET_BUFFER_STRATEGY_METHOD;
-
     private static final PlatformLogger LOGGER = PlatformLogger.getLogger(
                            "javax.swing.BufferStrategyPaintManager");
 
@@ -143,44 +140,6 @@
      */
     private boolean disposeBufferOnEnd;
 
-    private static Method getGetBufferStrategyMethod() {
-        if (COMPONENT_GET_BUFFER_STRATEGY_METHOD == null) {
-            getMethods();
-        }
-        return COMPONENT_GET_BUFFER_STRATEGY_METHOD;
-    }
-
-    private static Method getCreateBufferStrategyMethod() {
-        if (COMPONENT_CREATE_BUFFER_STRATEGY_METHOD == null) {
-            getMethods();
-        }
-        return COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
-    }
-
-    private static void getMethods() {
-        java.security.AccessController.doPrivileged(
-                            new java.security.PrivilegedAction<Object>() {
-            public Object run() {
-                try {
-                    COMPONENT_CREATE_BUFFER_STRATEGY_METHOD = Component.class.
-                              getDeclaredMethod("createBufferStrategy",
-                                                new Class[] { int.class,
-                                                BufferCapabilities.class });
-                    COMPONENT_CREATE_BUFFER_STRATEGY_METHOD.
-                                            setAccessible(true);
-                    COMPONENT_GET_BUFFER_STRATEGY_METHOD = Component.class.
-                              getDeclaredMethod("getBufferStrategy");
-                    COMPONENT_GET_BUFFER_STRATEGY_METHOD.setAccessible(true);
-                } catch (SecurityException e) {
-                    assert false;
-                } catch (NoSuchMethodException nsme) {
-                    assert false;
-                }
-                return null;
-            }
-        });
-    }
-
     BufferStrategyPaintManager() {
         bufferInfos = new ArrayList<BufferInfo>(1);
     }
@@ -766,16 +725,7 @@
                     componentBS = ((Window)root).getBufferStrategy();
                 }
                 else {
-                    try {
-                        componentBS = (BufferStrategy)
-                                 getGetBufferStrategyMethod().invoke(root);
-                    } catch (InvocationTargetException ite) {
-                        assert false;
-                    } catch (IllegalArgumentException iae) {
-                        assert false;
-                    } catch (IllegalAccessException iae2) {
-                        assert false;
-                    }
+                    componentBS = AWTAccessor.getComponentAccessor().getBufferStrategy(root);
                 }
                 if (componentBS != ourBS) {
                     // Component has a different BS, dispose ours.
@@ -839,19 +789,16 @@
             BufferStrategy bs = null;
             if (SunToolkit.isInstanceOf(root, "java.applet.Applet")) {
                 try {
-                    getCreateBufferStrategyMethod().invoke(root, 2, caps);
-                    bs = (BufferStrategy)getGetBufferStrategyMethod().
-                                            invoke(root);
-                } catch (InvocationTargetException ite) {
+                    AWTAccessor.ComponentAccessor componentAccessor
+                            = AWTAccessor.getComponentAccessor();
+                    componentAccessor.createBufferStrategy(root, 2, caps);
+                    bs = componentAccessor.getBufferStrategy(root);
+                } catch (AWTException e) {
                     // Type is not supported
                     if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
                         LOGGER.finer("createBufferStratety failed",
-                                     ite);
+                                     e);
                     }
-                } catch (IllegalArgumentException iae) {
-                    assert false;
-                } catch (IllegalAccessException iae2) {
-                    assert false;
                 }
             }
             else {
--- a/jdk/src/share/classes/sun/awt/AWTAccessor.java	Fri Feb 14 20:24:43 2014 +0400
+++ b/jdk/src/share/classes/sun/awt/AWTAccessor.java	Mon Feb 17 13:41:50 2014 +0400
@@ -34,6 +34,7 @@
 import java.awt.event.InvocationEvent;
 import java.awt.event.KeyEvent;
 import java.awt.geom.Point2D;
+import java.awt.image.BufferStrategy;
 import java.awt.peer.ComponentPeer;
 
 import java.lang.reflect.InvocationTargetException;
@@ -243,6 +244,16 @@
          */
         void revalidateSynchronously(Component comp);
 
+        /**
+         * Creates a new strategy for multi-buffering on this component.
+         */
+        void createBufferStrategy(Component comp, int numBuffers,
+                BufferCapabilities caps) throws AWTException;
+
+        /**
+         * returns the buffer strategy used by this component.
+         */
+        BufferStrategy getBufferStrategy(Component comp);
     }
 
     /*
@@ -375,6 +386,11 @@
          * Accessor for InputEvent.getButtonDownMasks()
          */
         int[] getButtonDownMasks();
+
+        /*
+         * Accessor for InputEvent.canAccessSystemClipboard field
+         */
+        boolean canAccessSystemClipboard(InputEvent event);
     }
 
     /*
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Fri Feb 14 20:24:43 2014 +0400
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Mon Feb 17 13:41:50 2014 +0400
@@ -189,7 +189,6 @@
         new StringUIClientPropertyKey ("maxTextOffset");
 
     // security stuff
-    private static Field inputEvent_CanAccessSystemClipboard_Field = null;
     private static final String UntrustedClipboardAccess =
         "UNTRUSTED_CLIPBOARD_ACCESS_KEY";
 
@@ -1263,41 +1262,6 @@
     }
 
     /**
-     * returns canAccessSystemClipboard field from InputEvent
-     *
-     * @param ie InputEvent to get the field from
-     */
-    private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) {
-        if (inputEvent_CanAccessSystemClipboard_Field == null) {
-            inputEvent_CanAccessSystemClipboard_Field =
-                AccessController.doPrivileged(
-                    new java.security.PrivilegedAction<Field>() {
-                        public Field run() {
-                            try {
-                                Field field = InputEvent.class.
-                                    getDeclaredField("canAccessSystemClipboard");
-                                field.setAccessible(true);
-                                return field;
-                            } catch (SecurityException e) {
-                            } catch (NoSuchFieldException e) {
-                            }
-                            return null;
-                        }
-                    });
-        }
-        if (inputEvent_CanAccessSystemClipboard_Field == null) {
-            return false;
-        }
-        boolean ret = false;
-        try {
-            ret = inputEvent_CanAccessSystemClipboard_Field.
-                getBoolean(ie);
-        } catch(IllegalAccessException e) {
-        }
-        return ret;
-    }
-
-    /**
      * Returns true if the given event is corrent gesture for
      * accessing clipboard
      *
@@ -1350,7 +1314,8 @@
              */
             if (e instanceof InputEvent
                 && (! checkGesture || isAccessClipboardGesture((InputEvent)e))) {
-                return inputEvent_canAccessSystemClipboard((InputEvent)e);
+                return AWTAccessor.getInputEventAccessor().
+                        canAccessSystemClipboard((InputEvent) e);
             } else {
                 return false;
             }