6657138: Mutable statics in Windows PL&F (findbugs)
authoralexp
Fri, 06 Nov 2009 19:48:09 +0300
changeset 4225 57ec8171fcd5
parent 4224 3cfc595ac892
child 4226 e7b2e4f305ca
6657138: Mutable statics in Windows PL&F (findbugs) Reviewed-by: peterz, hawtin
jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifButtonUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifCheckBoxUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifLabelUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifRadioButtonUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifToggleButtonUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsButtonUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsCheckBoxUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsLabelUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsRadioButtonUI.java
jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsToggleButtonUI.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicButtonUI.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicCheckBoxUI.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicLabelUI.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalButtonUI.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalCheckBoxUI.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalLabelUI.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalRadioButtonUI.java
jdk/src/share/classes/javax/swing/plaf/metal/MetalToggleButtonUI.java
jdk/test/javax/swing/Security/6657138/ComponentTest.java
jdk/test/javax/swing/Security/6657138/bug6657138.java
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.motif;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.border.*;
 import javax.swing.plaf.basic.*;
@@ -46,16 +48,23 @@
  */
 public class MotifButtonUI extends BasicButtonUI {
 
-    private final static MotifButtonUI motifButtonUI = new MotifButtonUI();
-
     protected Color selectColor;
 
     private boolean defaults_initialized = false;
 
+    private static final Object MOTIF_BUTTON_UI_KEY = new Object();
+
     // ********************************
     //          Create PLAF
     // ********************************
-    public static ComponentUI createUI(JComponent c){
+    public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        MotifButtonUI motifButtonUI =
+                (MotifButtonUI) appContext.get(MOTIF_BUTTON_UI_KEY);
+        if (motifButtonUI == null) {
+            motifButtonUI = new MotifButtonUI();
+            appContext.put(MOTIF_BUTTON_UI_KEY, motifButtonUI);
+        }
         return motifButtonUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifCheckBoxUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifCheckBoxUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.motif;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 
 import javax.swing.plaf.*;
@@ -45,7 +47,7 @@
  */
 public class MotifCheckBoxUI extends MotifRadioButtonUI {
 
-    private static final MotifCheckBoxUI motifCheckBoxUI = new MotifCheckBoxUI();
+    private static final Object MOTIF_CHECK_BOX_UI_KEY = new Object();
 
     private final static String propertyPrefix = "CheckBox" + ".";
 
@@ -55,7 +57,14 @@
     // ********************************
     //         Create PLAF
     // ********************************
-    public static ComponentUI createUI(JComponent c){
+    public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        MotifCheckBoxUI motifCheckBoxUI =
+                (MotifCheckBoxUI) appContext.get(MOTIF_CHECK_BOX_UI_KEY);
+        if (motifCheckBoxUI == null) {
+            motifCheckBoxUI = new MotifCheckBoxUI();
+            appContext.put(MOTIF_CHECK_BOX_UI_KEY, motifCheckBoxUI);
+        }
         return motifCheckBoxUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifLabelUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifLabelUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.motif;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.plaf.basic.BasicLabelUI;
 import javax.swing.plaf.ComponentUI;
@@ -44,9 +46,16 @@
  */
 public class MotifLabelUI extends BasicLabelUI
 {
-    static MotifLabelUI sharedInstance = new MotifLabelUI();
+    private static final Object MOTIF_LABEL_UI_KEY = new Object();
 
     public static ComponentUI createUI(JComponent c) {
-        return sharedInstance;
+        AppContext appContext = AppContext.getAppContext();
+        MotifLabelUI motifLabelUI =
+                (MotifLabelUI) appContext.get(MOTIF_LABEL_UI_KEY);
+        if (motifLabelUI == null) {
+            motifLabelUI = new MotifLabelUI();
+            appContext.put(MOTIF_LABEL_UI_KEY, motifLabelUI);
+        }
+        return motifLabelUI;
     }
 }
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifRadioButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifRadioButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.motif;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.border.*;
 import javax.swing.plaf.basic.BasicRadioButtonUI;
@@ -47,7 +49,7 @@
  */
 public class MotifRadioButtonUI extends BasicRadioButtonUI {
 
-    private static final MotifRadioButtonUI motifRadioButtonUI = new MotifRadioButtonUI();
+    private static final Object MOTIF_RADIO_BUTTON_UI_KEY = new Object();
 
     protected Color focusColor;
 
@@ -57,6 +59,13 @@
     //         Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        MotifRadioButtonUI motifRadioButtonUI =
+                (MotifRadioButtonUI) appContext.get(MOTIF_RADIO_BUTTON_UI_KEY);
+        if (motifRadioButtonUI == null) {
+            motifRadioButtonUI = new MotifRadioButtonUI();
+            appContext.put(MOTIF_RADIO_BUTTON_UI_KEY, motifRadioButtonUI);
+        }
         return motifRadioButtonUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifToggleButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/motif/MotifToggleButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.motif;
 
+import sun.awt.AppContext;
+
 import java.awt.*;
 import java.awt.event.*;
 
@@ -48,7 +50,7 @@
  */
 public class MotifToggleButtonUI extends BasicToggleButtonUI
 {
-    private final static MotifToggleButtonUI motifToggleButtonUI = new MotifToggleButtonUI();
+    private static final Object MOTIF_TOGGLE_BUTTON_UI_KEY = new Object();
 
     protected Color selectColor;
 
@@ -58,6 +60,13 @@
     //         Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        MotifToggleButtonUI motifToggleButtonUI =
+                (MotifToggleButtonUI) appContext.get(MOTIF_TOGGLE_BUTTON_UI_KEY);
+        if (motifToggleButtonUI == null) {
+            motifToggleButtonUI = new MotifToggleButtonUI();
+            appContext.put(MOTIF_TOGGLE_BUTTON_UI_KEY, motifToggleButtonUI);
+        }
         return motifToggleButtonUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -35,6 +35,7 @@
 import static com.sun.java.swing.plaf.windows.TMSchema.*;
 import static com.sun.java.swing.plaf.windows.TMSchema.Part.*;
 import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
+import sun.awt.AppContext;
 
 
 /**
@@ -52,8 +53,6 @@
  */
 public class WindowsButtonUI extends BasicButtonUI
 {
-    private final static WindowsButtonUI windowsButtonUI = new WindowsButtonUI();
-
     protected int dashedRectGapX;
     protected int dashedRectGapY;
     protected int dashedRectGapWidth;
@@ -63,11 +62,19 @@
 
     private boolean defaults_initialized = false;
 
+    private static final Object WINDOWS_BUTTON_UI_KEY = new Object();
 
     // ********************************
     //          Create PLAF
     // ********************************
-    public static ComponentUI createUI(JComponent c){
+    public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        WindowsButtonUI windowsButtonUI =
+                (WindowsButtonUI) appContext.get(WINDOWS_BUTTON_UI_KEY);
+        if (windowsButtonUI == null) {
+            windowsButtonUI = new WindowsButtonUI();
+            appContext.put(WINDOWS_BUTTON_UI_KEY, windowsButtonUI);
+        }
         return windowsButtonUI;
     }
 
@@ -151,7 +158,7 @@
      * allocating them in each paint call substantially reduced the time
      * it took paint to run.  Obviously, this method can't be re-entered.
      */
-    private static Rectangle viewRect = new Rectangle();
+    private Rectangle viewRect = new Rectangle();
 
     public void paint(Graphics g, JComponent c) {
         if (XPStyle.getXP() != null) {
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsCheckBoxUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsCheckBoxUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.windows;
 
+import sun.awt.AppContext;
+
 import javax.swing.plaf.basic.*;
 import javax.swing.*;
 import javax.swing.plaf.*;
@@ -49,7 +51,7 @@
     // of BasicCheckBoxUI because we want to pick up all the
     // painting changes made in MetalRadioButtonUI.
 
-    private static final WindowsCheckBoxUI windowsCheckBoxUI = new WindowsCheckBoxUI();
+    private static final Object WINDOWS_CHECK_BOX_UI_KEY = new Object();
 
     private final static String propertyPrefix = "CheckBox" + ".";
 
@@ -59,6 +61,13 @@
     //          Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        WindowsCheckBoxUI windowsCheckBoxUI =
+                (WindowsCheckBoxUI) appContext.get(WINDOWS_CHECK_BOX_UI_KEY);
+        if (windowsCheckBoxUI == null) {
+            windowsCheckBoxUI = new WindowsCheckBoxUI();
+            appContext.put(WINDOWS_CHECK_BOX_UI_KEY, windowsCheckBoxUI);
+        }
         return windowsCheckBoxUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsLabelUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsLabelUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package com.sun.java.swing.plaf.windows;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import java.awt.Color;
 import java.awt.Graphics;
 
@@ -51,12 +53,19 @@
  */
 public class WindowsLabelUI extends BasicLabelUI {
 
-    private final static WindowsLabelUI windowsLabelUI = new WindowsLabelUI();
+    private static final Object WINDOWS_LABEL_UI_KEY = new Object();
 
     // ********************************
     //          Create PLAF
     // ********************************
-    public static ComponentUI createUI(JComponent c){
+    public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        WindowsLabelUI windowsLabelUI =
+                (WindowsLabelUI) appContext.get(WINDOWS_LABEL_UI_KEY);
+        if (windowsLabelUI == null) {
+            windowsLabelUI = new WindowsLabelUI();
+            appContext.put(WINDOWS_LABEL_UI_KEY, windowsLabelUI);
+        }
         return windowsLabelUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsRadioButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsRadioButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.windows;
 
+import sun.awt.AppContext;
+
 import javax.swing.plaf.basic.*;
 import javax.swing.*;
 import javax.swing.plaf.*;
@@ -44,7 +46,7 @@
  */
 public class WindowsRadioButtonUI extends BasicRadioButtonUI
 {
-    private static final WindowsRadioButtonUI windowsRadioButtonUI = new WindowsRadioButtonUI();
+    private static final Object WINDOWS_RADIO_BUTTON_UI_KEY = new Object();
 
     protected int dashedRectGapX;
     protected int dashedRectGapY;
@@ -59,6 +61,13 @@
     //          Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        WindowsRadioButtonUI windowsRadioButtonUI =
+                (WindowsRadioButtonUI) appContext.get(WINDOWS_RADIO_BUTTON_UI_KEY);
+        if (windowsRadioButtonUI == null) {
+            windowsRadioButtonUI = new WindowsRadioButtonUI();
+            appContext.put(WINDOWS_RADIO_BUTTON_UI_KEY, windowsRadioButtonUI);
+        }
         return windowsRadioButtonUI;
     }
 
--- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsToggleButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsToggleButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package com.sun.java.swing.plaf.windows;
 
+import sun.awt.AppContext;
+
 import javax.swing.plaf.basic.*;
 import javax.swing.border.*;
 import javax.swing.plaf.*;
@@ -49,18 +51,25 @@
  */
 public class WindowsToggleButtonUI extends BasicToggleButtonUI
 {
-    protected static int dashedRectGapX;
-    protected static int dashedRectGapY;
-    protected static int dashedRectGapWidth;
-    protected static int dashedRectGapHeight;
+    protected int dashedRectGapX;
+    protected int dashedRectGapY;
+    protected int dashedRectGapWidth;
+    protected int dashedRectGapHeight;
 
     protected Color focusColor;
 
-    private final static WindowsToggleButtonUI windowsToggleButtonUI = new WindowsToggleButtonUI();
+    private static final Object WINDOWS_TOGGLE_BUTTON_UI_KEY = new Object();
 
     private boolean defaults_initialized = false;
 
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        WindowsToggleButtonUI windowsToggleButtonUI =
+                (WindowsToggleButtonUI) appContext.get(WINDOWS_TOGGLE_BUTTON_UI_KEY);
+        if (windowsToggleButtonUI == null) {
+            windowsToggleButtonUI = new WindowsToggleButtonUI();
+            appContext.put(WINDOWS_TOGGLE_BUTTON_UI_KEY, windowsToggleButtonUI);
+        }
         return windowsToggleButtonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.plaf.basic;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import java.awt.*;
 import java.awt.event.*;
 import java.io.Serializable;
@@ -44,9 +46,6 @@
  * @author Jeff Dinkins
  */
 public class BasicButtonUI extends ButtonUI{
-    // Shared UI object
-    private final static BasicButtonUI buttonUI = new BasicButtonUI();
-
     // Visual constants
     // NOTE: This is not used or set any where. Were we allowed to remove
     // fields, this would be removed.
@@ -61,10 +60,19 @@
 
     private final static String propertyPrefix = "Button" + ".";
 
+    private static final Object BASIC_BUTTON_UI_KEY = new Object();
+
     // ********************************
     //          Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        BasicButtonUI buttonUI =
+                (BasicButtonUI) appContext.get(BASIC_BUTTON_UI_KEY);
+        if (buttonUI == null) {
+            buttonUI = new BasicButtonUI();
+            appContext.put(BASIC_BUTTON_UI_KEY, buttonUI);
+        }
         return buttonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicCheckBoxUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicCheckBoxUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package javax.swing.plaf.basic;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 
 import java.awt.*;
@@ -49,7 +51,7 @@
  */
 public class BasicCheckBoxUI extends BasicRadioButtonUI {
 
-    private final static BasicCheckBoxUI checkboxUI = new BasicCheckBoxUI();
+    private static final Object BASIC_CHECK_BOX_UI_KEY = new Object();
 
     private final static String propertyPrefix = "CheckBox" + ".";
 
@@ -57,6 +59,13 @@
     //            Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        BasicCheckBoxUI checkboxUI =
+                (BasicCheckBoxUI) appContext.get(BASIC_CHECK_BOX_UI_KEY);
+        if (checkboxUI == null) {
+            checkboxUI = new BasicCheckBoxUI();
+            appContext.put(BASIC_CHECK_BOX_UI_KEY, checkboxUI);
+        }
         return checkboxUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicLabelUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicLabelUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -28,6 +28,8 @@
 import sun.swing.SwingUtilities2;
 import sun.swing.DefaultLookup;
 import sun.swing.UIAction;
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.plaf.*;
 import javax.swing.text.View;
@@ -63,7 +65,7 @@
     * name in defaults table under the key "LabelUI".
     */
     protected static BasicLabelUI labelUI = new BasicLabelUI();
-    private final static BasicLabelUI SAFE_BASIC_LABEL_UI = new BasicLabelUI();
+    private static final Object BASIC_LABEL_UI_KEY = new Object();
 
     private Rectangle paintIconR = new Rectangle();
     private Rectangle paintTextR = new Rectangle();
@@ -394,10 +396,16 @@
 
     public static ComponentUI createUI(JComponent c) {
         if (System.getSecurityManager() != null) {
-            return SAFE_BASIC_LABEL_UI;
-        } else {
-            return labelUI;
+            AppContext appContext = AppContext.getAppContext();
+            BasicLabelUI safeBasicLabelUI =
+                    (BasicLabelUI) appContext.get(BASIC_LABEL_UI_KEY);
+            if (safeBasicLabelUI == null) {
+                safeBasicLabelUI = new BasicLabelUI();
+                appContext.put(BASIC_LABEL_UI_KEY, safeBasicLabelUI);
+            }
+            return safeBasicLabelUI;
         }
+        return labelUI;
     }
 
     public void propertyChange(PropertyChangeEvent e) {
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -32,6 +32,7 @@
 import javax.swing.plaf.*;
 import javax.swing.text.View;
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
 
 
 /**
@@ -41,7 +42,7 @@
  */
 public class BasicRadioButtonUI extends BasicToggleButtonUI
 {
-    private final static BasicRadioButtonUI radioButtonUI = new BasicRadioButtonUI();
+    private static final Object BASIC_RADIO_BUTTON_UI_KEY = new Object();
 
     protected Icon icon;
 
@@ -53,6 +54,13 @@
     //        Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        BasicRadioButtonUI radioButtonUI =
+                (BasicRadioButtonUI) appContext.get(BASIC_RADIO_BUTTON_UI_KEY);
+        if (radioButtonUI == null) {
+            radioButtonUI = new BasicRadioButtonUI();
+            appContext.put(BASIC_RADIO_BUTTON_UI_KEY, radioButtonUI);
+        }
         return radioButtonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicToggleButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package javax.swing.plaf.basic;
 
+import sun.awt.AppContext;
+
 import java.awt.*;
 import java.awt.event.*;
 
@@ -43,7 +45,7 @@
  */
 public class BasicToggleButtonUI extends BasicButtonUI {
 
-    private final static BasicToggleButtonUI toggleButtonUI = new BasicToggleButtonUI();
+    private static final Object BASIC_TOGGLE_BUTTON_UI_KEY = new Object();
 
     private final static String propertyPrefix = "ToggleButton" + ".";
 
@@ -51,6 +53,13 @@
     //          Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        BasicToggleButtonUI toggleButtonUI =
+                (BasicToggleButtonUI) appContext.get(BASIC_TOGGLE_BUTTON_UI_KEY);
+        if (toggleButtonUI == null) {
+            toggleButtonUI = new BasicToggleButtonUI();
+            appContext.put(BASIC_TOGGLE_BUTTON_UI_KEY, toggleButtonUI);
+        }
         return toggleButtonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.plaf.metal;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.border.*;
 import javax.swing.plaf.basic.*;
@@ -49,19 +51,25 @@
  * @author Tom Santos
  */
 public class MetalButtonUI extends BasicButtonUI {
-
-    private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
-
     // NOTE: These are not really needed, but at this point we can't pull
     // them. Their values are updated purely for historical reasons.
     protected Color focusColor;
     protected Color selectColor;
     protected Color disabledTextColor;
 
+    private static final Object METAL_BUTTON_UI_KEY = new Object();
+
     // ********************************
     //          Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        MetalButtonUI metalButtonUI =
+                (MetalButtonUI) appContext.get(METAL_BUTTON_UI_KEY);
+        if (metalButtonUI == null) {
+            metalButtonUI = new MetalButtonUI();
+            appContext.put(METAL_BUTTON_UI_KEY, metalButtonUI);
+        }
         return metalButtonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalCheckBoxUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalCheckBoxUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -25,6 +25,8 @@
 
 package javax.swing.plaf.metal;
 
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.plaf.basic.BasicCheckBoxUI;
 
@@ -55,7 +57,7 @@
     // of BasicCheckBoxUI because we want to pick up all the
     // painting changes made in MetalRadioButtonUI.
 
-    private final static MetalCheckBoxUI checkboxUI = new MetalCheckBoxUI();
+    private static final Object METAL_CHECK_BOX_UI_KEY = new Object();
 
     private final static String propertyPrefix = "CheckBox" + ".";
 
@@ -65,6 +67,13 @@
     //         Create PlAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        MetalCheckBoxUI checkboxUI =
+                (MetalCheckBoxUI) appContext.get(METAL_CHECK_BOX_UI_KEY);
+        if (checkboxUI == null) {
+            checkboxUI = new MetalCheckBoxUI();
+            appContext.put(METAL_CHECK_BOX_UI_KEY, checkboxUI);
+        }
         return checkboxUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalLabelUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalLabelUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.plaf.metal;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import javax.swing.*;
 import javax.swing.plaf.*;
 import javax.swing.plaf.basic.*;
@@ -51,15 +53,21 @@
     * name in defaults table under the key "LabelUI".
     */
     protected static MetalLabelUI metalLabelUI = new MetalLabelUI();
-    private final static MetalLabelUI SAFE_METAL_LABEL_UI = new MetalLabelUI();
 
+    private static final Object METAL_LABEL_UI_KEY = new Object();
 
     public static ComponentUI createUI(JComponent c) {
         if (System.getSecurityManager() != null) {
-            return SAFE_METAL_LABEL_UI;
-        } else {
-            return metalLabelUI;
+            AppContext appContext = AppContext.getAppContext();
+            MetalLabelUI safeMetalLabelUI =
+                    (MetalLabelUI) appContext.get(METAL_LABEL_UI_KEY);
+            if (safeMetalLabelUI == null) {
+                safeMetalLabelUI = new MetalLabelUI();
+                appContext.put(METAL_LABEL_UI_KEY, safeMetalLabelUI);
+            }
+            return safeMetalLabelUI;
         }
+        return metalLabelUI;
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalRadioButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalRadioButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.plaf.metal;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
@@ -53,7 +55,7 @@
  */
 public class MetalRadioButtonUI extends BasicRadioButtonUI {
 
-    private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI();
+    private static final Object METAL_RADIO_BUTTON_UI_KEY = new Object();
 
     protected Color focusColor;
     protected Color selectColor;
@@ -65,6 +67,13 @@
     //        Create PlAF
     // ********************************
     public static ComponentUI createUI(JComponent c) {
+        AppContext appContext = AppContext.getAppContext();
+        MetalRadioButtonUI metalRadioButtonUI =
+                (MetalRadioButtonUI) appContext.get(METAL_RADIO_BUTTON_UI_KEY);
+        if (metalRadioButtonUI == null) {
+            metalRadioButtonUI = new MetalRadioButtonUI();
+            appContext.put(METAL_RADIO_BUTTON_UI_KEY, metalRadioButtonUI);
+        }
         return metalRadioButtonUI;
     }
 
--- a/jdk/src/share/classes/javax/swing/plaf/metal/MetalToggleButtonUI.java	Wed Nov 04 10:13:52 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/plaf/metal/MetalToggleButtonUI.java	Fri Nov 06 19:48:09 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.plaf.metal;
 
 import sun.swing.SwingUtilities2;
+import sun.awt.AppContext;
+
 import java.awt.*;
 import java.awt.event.*;
 import java.lang.ref.*;
@@ -55,7 +57,7 @@
  */
 public class MetalToggleButtonUI extends BasicToggleButtonUI {
 
-    private static final MetalToggleButtonUI metalToggleButtonUI = new MetalToggleButtonUI();
+    private static final Object METAL_TOGGLE_BUTTON_UI_KEY = new Object();
 
     protected Color focusColor;
     protected Color selectColor;
@@ -67,6 +69,13 @@
     //        Create PLAF
     // ********************************
     public static ComponentUI createUI(JComponent b) {
+        AppContext appContext = AppContext.getAppContext();
+        MetalToggleButtonUI metalToggleButtonUI =
+                (MetalToggleButtonUI) appContext.get(METAL_TOGGLE_BUTTON_UI_KEY);
+        if (metalToggleButtonUI == null) {
+            metalToggleButtonUI = new MetalToggleButtonUI();
+            appContext.put(METAL_TOGGLE_BUTTON_UI_KEY, metalToggleButtonUI);
+        }
         return metalToggleButtonUI;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/Security/6657138/ComponentTest.java	Fri Nov 06 19:48:09 2009 +0300
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6657138
+ * @summary Verifies that buttons and labels work well after the fix for 6657138
+ * @author Alexander Potochkin
+ */
+
+import sun.awt.SunToolkit;
+
+import javax.swing.*;
+import java.awt.*;
+
+public class ComponentTest extends JFrame {
+    private static JFrame frame;
+
+    public ComponentTest() {
+        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        setLayout(new FlowLayout());
+        add(new JButton("JButton"));
+        add(new JToggleButton("JToggleButton"));
+        add(new JCheckBox("JCheckBox"));
+        add(new JRadioButton("JRadioButton"));
+        add(new JLabel("JLabel"));
+        pack();
+        setLocationRelativeTo(null);
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                frame = new ComponentTest();
+                frame.setVisible(true);
+            }
+        });
+        toolkit.realSync();
+        UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
+        for (final UIManager.LookAndFeelInfo laf : lafs) {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    try {
+                        UIManager.setLookAndFeel(laf.getClassName());
+                    } catch (Exception e) {
+                        new RuntimeException(e);
+                    }
+                    SwingUtilities.updateComponentTreeUI(frame);
+                }
+            });
+            toolkit.realSync();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/Security/6657138/bug6657138.java	Fri Nov 06 19:48:09 2009 +0300
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6657138
+ * @summary Verifies that buttons and labels don't share their ui's across appContexts
+ * @author Alexander Potochkin
+ */
+
+import sun.awt.SunToolkit;
+
+import javax.swing.*;
+import javax.swing.plaf.ButtonUI;
+import javax.swing.plaf.ComponentUI;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class bug6657138 implements Runnable {
+
+    private static Map<JComponent, Map<String, ComponentUI>> componentMap =
+            Collections.synchronizedMap(
+            new HashMap<JComponent, Map<String, ComponentUI>>());
+
+    public void run() {
+        SunToolkit.createNewAppContext();
+        try {
+            testUIMap();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static void testUIMap() throws Exception {
+        UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
+        Set<JComponent> components = componentMap.keySet();
+        for (JComponent c : components) {
+            Map<String, ComponentUI> uiMap = componentMap.get(c);
+
+            for (UIManager.LookAndFeelInfo laf : lafs) {
+                if ("Nimbus".equals(laf.getName())) {
+                    // for some unclear reasons
+                    // Nimbus ui delegate for a button is null
+                    // when this method is called from the new AppContext
+                    continue;
+                }
+                String className = laf.getClassName();
+                UIManager.setLookAndFeel(className);
+                ComponentUI ui = UIManager.getUI(c);
+                if (ui == null) {
+                    throw new RuntimeException("UI is null for " + c);
+                }
+                if (ui == uiMap.get(laf.getName())) {
+                    throw new RuntimeException(
+                            "Two AppContexts share the same UI delegate! \n" +
+                                    c + "\n" + ui);
+                }
+                uiMap.put(laf.getName(), ui);
+            }
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        componentMap.put(new JButton("JButton"),
+                new HashMap<String, ComponentUI>());
+        componentMap.put(new JToggleButton("JToggleButton"),
+                new HashMap<String, ComponentUI>());
+        componentMap.put(new JRadioButton("JRadioButton"),
+                new HashMap<String, ComponentUI>());
+        componentMap.put(new JCheckBox("JCheckBox"),
+                new HashMap<String, ComponentUI>());
+        componentMap.put(new JCheckBox("JLabel"),
+                new HashMap<String, ComponentUI>());
+        testUIMap();
+        ThreadGroup group = new ThreadGroup("6657138");
+        Thread thread = new Thread(group, new bug6657138());
+        thread.start();
+        thread.join();
+    }
+}
+