8149879: Examine UIDefaults::addResourceBundle(String bundleName) with resource encapsulation
authorserb
Wed, 18 Jan 2017 18:14:50 +0300
changeset 43308 cff6230190e5
parent 43233 cb6dfe51617c
child 43309 5df4efb223bd
8149879: Examine UIDefaults::addResourceBundle(String bundleName) with resource encapsulation Reviewed-by: prr, mchung, alexsch
jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaLookAndFeel.java
jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKLookAndFeel.java
jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifLookAndFeel.java
jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java
jdk/src/java.desktop/share/classes/javax/swing/UIDefaults.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalLookAndFeel.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthLookAndFeel.java
jdk/src/java.desktop/share/classes/sun/swing/SwingAccessor.java
jdk/test/javax/swing/UIDefaults/8149879/InternalResourceBundle.java
--- a/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2017, 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
@@ -248,7 +248,8 @@
      */
     private void initResourceBundle(final UIDefaults table) {
         table.setDefaultLocale(Locale.getDefault());
-        table.addResourceBundle(PKG_PREFIX + "resources.aqua");
+        SwingAccessor.getUIDefaultsAccessor().addInternalBundle(table,
+                PKG_PREFIX + "resources.aqua");
         try {
             final ResourceBundle aquaProperties = ResourceBundle.getBundle(PKG_PREFIX + "resources.aqua");
             final Enumeration<String> propertyKeys = aquaProperties.getKeys();
--- a/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -47,6 +47,7 @@
 import sun.awt.OSInfo;
 import sun.security.action.GetPropertyAction;
 import sun.swing.DefaultLayoutStyle;
+import sun.swing.SwingAccessor;
 import sun.swing.SwingUtilities2;
 
 /**
@@ -288,7 +289,9 @@
     }
 
     private void initResourceBundle(UIDefaults table) {
-        table.addResourceBundle("com.sun.java.swing.plaf.gtk.resources.gtk");
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.java.swing.plaf.gtk.resources.gtk");
     }
 
     protected void initComponentDefaults(UIDefaults table) {
--- a/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/motif/MotifLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -43,6 +43,7 @@
 import javax.swing.plaf.basic.BasicComboBoxRenderer;
 import javax.swing.plaf.basic.BasicComboBoxEditor;
 
+import sun.swing.SwingAccessor;
 import sun.swing.SwingUtilities2;
 import sun.awt.OSInfo;
 
@@ -179,7 +180,9 @@
      * used for getting localized defaults.
      */
     private void initResourceBundle(UIDefaults table) {
-        table.addResourceBundle( "com.sun.java.swing.plaf.motif.resources.motif" );
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.java.swing.plaf.motif.resources.motif");
     }
 
 
--- a/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -68,6 +68,7 @@
 
 import sun.swing.DefaultLayoutStyle;
 import sun.swing.ImageIconUIResource;
+import sun.swing.SwingAccessor;
 import sun.swing.icon.SortArrowIcon;
 import sun.swing.SwingUtilities2;
 import sun.swing.StringUIClientPropertyKey;
@@ -287,7 +288,9 @@
      * used for getting localized defaults.
      */
     private void initResourceBundle(UIDefaults table) {
-        table.addResourceBundle( "com.sun.java.swing.plaf.windows.resources.windows" );
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.java.swing.plaf.windows.resources.windows");
     }
 
     // XXX - there are probably a lot of redundant values that could be removed.
--- a/jdk/src/java.desktop/share/classes/javax/swing/UIDefaults.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/UIDefaults.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -53,6 +53,7 @@
 
 import sun.reflect.misc.MethodUtil;
 import sun.reflect.misc.ReflectUtil;
+import sun.swing.SwingAccessor;
 import sun.swing.SwingUtilities2;
 
 /**
@@ -91,6 +92,10 @@
      */
     private Map<Locale, Map<String, Object>> resourceCache;
 
+    static {
+        SwingAccessor.setUIDefaultsAccessor(UIDefaults::addInternalBundle);
+    }
+
     /**
      * Creates an empty defaults table.
      */
@@ -881,29 +886,49 @@
 
     /**
      * Adds a resource bundle to the list of resource bundles that are
-     * searched for localized values.  Resource bundles are searched in the
-     * reverse order they were added.  In other words, the most recently added
-     * bundle is searched first.
+     * searched for localized values. Resource bundles are searched in
+     * the reverse order they were added, using the
+     * {@linkplain ClassLoader#getSystemClassLoader application class loader}.
+     * In other words, the most recently added bundle is searched first.
      *
      * @param bundleName  the base name of the resource bundle to be added
      * @see java.util.ResourceBundle
      * @see #removeResourceBundle
+     * @see ResourceBundle#getBundle(String, Locale, ClassLoader)
      * @since 1.4
      */
-    public synchronized void addResourceBundle( String bundleName ) {
-        if( bundleName == null ) {
+    public synchronized void addResourceBundle(final String bundleName) {
+        if (bundleName == null) {
+            return;
+        }
+        if (isDesktopResourceBundle(bundleName)) {
+            // Only the java.desktop itself can register resource bundles from
+            // java.desktop module
             return;
         }
-        if( resourceBundles == null ) {
+        addInternalBundle(bundleName);
+    }
+
+    /**
+     * This methods should be used to register internal resource bundles from
+     * the java.desktop module.
+     *
+     * @param bundleName  the base name of the resource bundle to be added
+     * @since 9
+     */
+    private synchronized void addInternalBundle(final String bundleName) {
+        if (bundleName == null) {
+            return;
+        }
+        if (resourceBundles == null) {
             resourceBundles = new Vector<String>(5);
         }
         if (!resourceBundles.contains(bundleName)) {
-            resourceBundles.add( bundleName );
+            resourceBundles.add(bundleName);
             resourceCache.clear();
         }
     }
 
-
     /**
      * Removes a resource bundle from the list of resource bundles that are
      * searched for localized defaults.
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, 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
@@ -48,7 +48,7 @@
 
 import sun.awt.AppContext;
 import sun.awt.SunToolkit;
-
+import sun.swing.SwingAccessor;
 import sun.swing.SwingUtilities2;
 import sun.swing.icon.SortArrowIcon;
 
@@ -439,7 +439,9 @@
      */
     private void initResourceBundle(UIDefaults table) {
         table.setDefaultLocale( Locale.getDefault() );
-        table.addResourceBundle( "com.sun.swing.internal.plaf.basic.resources.basic" );
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.swing.internal.plaf.basic.resources.basic");
     }
 
     /**
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, 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
@@ -43,6 +43,8 @@
 import sun.security.action.GetPropertyAction;
 import sun.swing.DefaultLayoutStyle;
 import static javax.swing.UIDefaults.LazyValue;
+
+import sun.swing.SwingAccessor;
 import sun.swing.SwingUtilities2;
 
 /**
@@ -418,7 +420,9 @@
      * used for getting localized defaults.
      */
     private void initResourceBundle(UIDefaults table) {
-        table.addResourceBundle( "com.sun.swing.internal.plaf.metal.resources.metal" );
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.swing.internal.plaf.metal.resources.metal");
     }
 
     /**
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthLookAndFeel.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthLookAndFeel.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -647,9 +647,12 @@
 
         Region.registerUIs(table);
         table.setDefaultLocale(Locale.getDefault());
-        table.addResourceBundle(
-              "com.sun.swing.internal.plaf.basic.resources.basic" );
-        table.addResourceBundle("com.sun.swing.internal.plaf.synth.resources.synth");
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.swing.internal.plaf.basic.resources.basic");
+        SwingAccessor.getUIDefaultsAccessor()
+                     .addInternalBundle(table,
+                             "com.sun.swing.internal.plaf.synth.resources.synth");
 
         // SynthTabbedPaneUI supports rollover on tabs, GTK does not
         table.put("TabbedPane.isTabRollover", Boolean.TRUE);
--- a/jdk/src/java.desktop/share/classes/sun/swing/SwingAccessor.java	Wed Jan 18 11:35:31 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/sun/swing/SwingAccessor.java	Wed Jan 18 18:14:50 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2017, 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
@@ -92,6 +92,16 @@
     }
 
     /**
+     * An accessor for the UIDefaults class.
+     */
+    public interface UIDefaultsAccessor {
+        /**
+         * Adds a resource bundle to the list of resource bundles.
+         */
+        void addInternalBundle(UIDefaults uiDefaults, String bundleName);
+    }
+
+    /**
      * An accessor for the RepaintManager class.
      */
     public interface RepaintManagerAccessor {
@@ -184,6 +194,28 @@
     }
 
     /**
+     * The UIDefaults class accessor object
+     */
+    private static UIDefaultsAccessor uiDefaultsAccessor;
+
+    /**
+     * Set an accessor object for the UIDefaults class.
+     */
+    public static void setUIDefaultsAccessor(UIDefaultsAccessor accessor) {
+        uiDefaultsAccessor = accessor;
+    }
+
+    /**
+     * Retrieve the accessor object for the JLightweightFrame class
+     */
+    public static UIDefaultsAccessor getUIDefaultsAccessor() {
+        if (uiDefaultsAccessor == null) {
+            unsafe.ensureClassInitialized(UIDefaults.class);
+        }
+        return uiDefaultsAccessor;
+    }
+
+    /**
      * The RepaintManager class accessor object.
      */
     private static RepaintManagerAccessor repaintManagerAccessor;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/UIDefaults/8149879/InternalResourceBundle.java	Wed Jan 18 18:14:50 2017 +0300
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2017, 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.awt.EventQueue;
+
+import javax.swing.UIDefaults;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+import javax.swing.plaf.nimbus.NimbusLookAndFeel;
+
+/**
+ * @test
+ * @bug 8149879
+ * @summary The users should not register internal resource bundles.
+ */
+public class InternalResourceBundle {
+
+    public static void main(final String[] args) throws Exception {
+        EventQueue.invokeAndWait(() -> {
+            // Indirectly register resource bundle from Nimbus, it will be used
+            // by default
+            try {
+                UIManager.setLookAndFeel(new NimbusLookAndFeel());
+            } catch (final UnsupportedLookAndFeelException e) {
+                throw new RuntimeException(e);
+            }
+            UIDefaults defaults = UIManager.getDefaults();
+            // Try to register resource bundle from Metal, which is
+            // not enabled by default. This request should be skipped.
+            defaults.addResourceBundle("com.sun.swing.internal.plaf.metal.resources.metal");
+
+            Object value = getValue(defaults);
+            if (value != null) {
+                throw new RuntimeException("value is not null = " + value);
+            }
+
+            // Indirectly register resource bundle from Metal
+            try {
+                UIManager.setLookAndFeel(new MetalLookAndFeel());
+            } catch (final UnsupportedLookAndFeelException e) {
+                throw new RuntimeException(e);
+            }
+            value = getValue(defaults);
+            if (value == null) {
+                throw new RuntimeException("value is null");
+            }
+        });
+    }
+
+    private static Object getValue(UIDefaults defaults) {
+        return defaults.get("MetalTitlePane.restore.titleAndMnemonic");
+    }
+}