changeset 10390:b2e756f77a2e

8049830: Remove reflection from ScreenMenuBar Reviewed-by: anthony, serb
author pchelko
date Thu, 10 Jul 2014 15:08:50 +0400
parents 9fe87c9a16da
children a13a49fc1810
files src/macosx/classes/com/apple/laf/ScreenMenuBar.java src/share/classes/java/awt/MenuComponent.java src/share/classes/sun/awt/AWTAccessor.java
diffstat 3 files changed, 26 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/com/apple/laf/ScreenMenuBar.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/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;
+            final Vector<Menu> menus = AWTAccessor.getMenuBarAccessor().getMenus(this);
+            menus.insertElementAt(m, index);
+            AWTAccessor.getMenuComponentAccessor().setParent(m, this);
 
-                @SuppressWarnings("unchecked")
-                final Vector<Menu> menus = (Vector<Menu>)stolenFields[0].get(this);
-                    menus.insertElementAt(m, index);
+            final CMenuBar peer = (CMenuBar)getPeer();
+            if (peer == null) return m;
 
-                    stolenFields[1].set(m, this);
-
-                    final sun.lwawt.macosx.CMenuBar peer = (sun.lwawt.macosx.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/src/share/classes/java/awt/MenuComponent.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/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/src/share/classes/sun/awt/AWTAccessor.java	Thu Jul 10 12:21:29 2014 +0400
+++ b/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);