8049830: Remove reflection from ScreenMenuBar
authorpchelko
Thu, 10 Jul 2014 15:08:50 +0400
changeset 25764 6333e4aa8a70
parent 25763 51d1f910f68d
child 25765 88051e6ecdc7
8049830: Remove reflection from ScreenMenuBar Reviewed-by: anthony, serb
jdk/src/macosx/classes/com/apple/laf/ScreenMenuBar.java
jdk/src/share/classes/java/awt/MenuComponent.java
jdk/src/share/classes/sun/awt/AWTAccessor.java
--- a/jdk/src/macosx/classes/com/apple/laf/ScreenMenuBar.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/jdk/src/macosx/classes/com/apple/laf/ScreenMenuBar.java	Thu Jul 10 15:08:50 2014 +0400
@@ -25,6 +25,9 @@
 
 package com.apple.laf;
 
+import sun.awt.AWTAccessor;
+import sun.lwawt.macosx.CMenuBar;
+
 import java.awt.*;
 import java.awt.event.*;
 import java.lang.reflect.*;
@@ -243,55 +246,25 @@
             fSubmenus.remove(menu);
     }
 
-    private static Field[] stolenFields = null;
-
-    static {
-        stolenFields = AccessController.doPrivileged(new PrivilegedAction<Field[]>() {
-            public Field[] run() {
-                try {
-                    final Field[] localFields = new Field[2];
-                    localFields[0] = MenuBar.class.getDeclaredField("menus");
-                    localFields[1] = MenuComponent.class.getDeclaredField("parent");
-                    AccessibleObject.setAccessible(localFields, true);
-                    return localFields;
-                } catch (final NoSuchFieldException nsf) {
-                    // If this happens, Sun changed the definition of MenuBar and MenuComponent!
-                    nsf.printStackTrace(System.err);
-                    return null;
-                }
-            }
-        });
-    };
-
     public Menu add(final Menu m, final int index) {
         synchronized (getTreeLock()) {
             if (m.getParent() != null) {
                 m.getParent().remove(m);
             }
 
-            // Use nasty reflection to get at the menus array and parent fields.
-            try {
-                if (stolenFields == null) return m;
-
-                @SuppressWarnings("unchecked")
-                final Vector<Menu> menus = (Vector<Menu>)stolenFields[0].get(this);
-                    menus.insertElementAt(m, index);
-
-                    stolenFields[1].set(m, this);
+            final Vector<Menu> menus = AWTAccessor.getMenuBarAccessor().getMenus(this);
+            menus.insertElementAt(m, index);
+            AWTAccessor.getMenuComponentAccessor().setParent(m, this);
 
-                    final sun.lwawt.macosx.CMenuBar peer = (sun.lwawt.macosx.CMenuBar)getPeer();
-                if (peer == null) return m;
+            final CMenuBar peer = (CMenuBar)getPeer();
+            if (peer == null) return m;
 
-                        peer.setNextInsertionIndex(index);
-                        if (m.getPeer() == null) {
-                            m.addNotify();
-                        }
-
-                        peer.setNextInsertionIndex(-1);
-            } catch (final IllegalAccessException iae) {
-                iae.printStackTrace(System.err);
+            peer.setNextInsertionIndex(index);
+            if (m.getPeer() == null) {
+                m.addNotify();
             }
 
+            peer.setNextInsertionIndex(-1);
             return m;
         }
     }
--- a/jdk/src/share/classes/java/awt/MenuComponent.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/jdk/src/share/classes/java/awt/MenuComponent.java	Thu Jul 10 15:08:50 2014 +0400
@@ -132,16 +132,24 @@
     static {
         AWTAccessor.setMenuComponentAccessor(
             new AWTAccessor.MenuComponentAccessor() {
+                @Override
                 public AppContext getAppContext(MenuComponent menuComp) {
                     return menuComp.appContext;
                 }
+                @Override
                 public void setAppContext(MenuComponent menuComp,
                                           AppContext appContext) {
                     menuComp.appContext = appContext;
                 }
+                @Override
                 public MenuContainer getParent(MenuComponent menuComp) {
                     return menuComp.parent;
                 }
+                @Override
+                public void setParent(MenuComponent menuComp, MenuContainer menuContainer) {
+                    menuComp.parent = menuContainer;
+                }
+                @Override
                 public Font getFont_NoClientCode(MenuComponent menuComp) {
                     return menuComp.getFont_NoClientCode();
                 }
--- a/jdk/src/share/classes/sun/awt/AWTAccessor.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/jdk/src/share/classes/sun/awt/AWTAccessor.java	Thu Jul 10 15:08:50 2014 +0400
@@ -481,11 +481,16 @@
         void setAppContext(MenuComponent menuComp, AppContext appContext);
 
         /**
-         * Returns the menu container of the menu component
+         * Returns the menu container of the menu component.
          */
         MenuContainer getParent(MenuComponent menuComp);
 
         /**
+         * Sets the menu container of the menu component.
+         */
+        void  setParent(MenuComponent menuComp, MenuContainer menuContainer);
+
+        /**
          * Gets the font used for this menu component.
          */
         Font getFont_NoClientCode(MenuComponent menuComp);