8003495: [findbugs] javax.swing.* - Storing a reference to an externally mutable object into the internal representation
authoralexsch
Tue, 24 Jan 2017 11:37:32 +0300
changeset 43322 046c9ccca33d
parent 43321 2b8d397c03a4
child 43323 230afa43a3b5
child 43513 7cb602dc9001
8003495: [findbugs] javax.swing.* - Storing a reference to an externally mutable object into the internal representation Reviewed-by: prr, serb
jdk/src/java.desktop/share/classes/javax/swing/JColorChooser.java
jdk/src/java.desktop/share/classes/javax/swing/JOptionPane.java
jdk/src/java.desktop/share/classes/sun/swing/FilePane.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/JColorChooser.java	Mon Jan 23 11:59:48 2017 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JColorChooser.java	Tue Jan 24 11:37:32 2017 +0300
@@ -487,7 +487,7 @@
             = "An array of different chooser types.")
     public void setChooserPanels( AbstractColorChooserPanel[] panels) {
         AbstractColorChooserPanel[] oldValue = chooserPanels;
-        chooserPanels = panels;
+        chooserPanels = Arrays.copyOf(panels, panels.length);
         firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels);
     }
 
@@ -497,7 +497,7 @@
      * @return an array of <code>AbstractColorChooserPanel</code> objects
      */
     public AbstractColorChooserPanel[] getChooserPanels() {
-        return chooserPanels;
+        return Arrays.copyOf(chooserPanels, chooserPanels.length);
     }
 
     /**
--- a/jdk/src/java.desktop/share/classes/javax/swing/JOptionPane.java	Mon Jan 23 11:59:48 2017 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JOptionPane.java	Tue Jan 24 11:37:32 2017 +0300
@@ -48,6 +48,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Vector;
 import javax.swing.plaf.OptionPaneUI;
 import javax.swing.event.InternalFrameEvent;
@@ -1812,7 +1813,7 @@
                        Icon icon, Object[] options, Object initialValue) {
 
         this.message = message;
-        this.options = options;
+        this.options = options == null ? null : Arrays.copyOf(options, options.length);
         this.initialValue = initialValue;
         this.icon = icon;
         setMessageType(messageType);
@@ -1972,7 +1973,9 @@
     public void setOptions(Object[] newOptions) {
         Object[]           oldOptions = options;
 
-        options = newOptions;
+        options = newOptions == null
+                ? null
+                : Arrays.copyOf(newOptions, newOptions.length);
         firePropertyChange(OPTIONS_PROPERTY, oldOptions, options);
     }
 
@@ -1983,14 +1986,7 @@
      * @see #setOptions
      */
     public Object[] getOptions() {
-        if(options != null) {
-            int             optionCount = options.length;
-            Object[]        retOptions = new Object[optionCount];
-
-            System.arraycopy(options, 0, retOptions, 0, optionCount);
-            return retOptions;
-        }
-        return options;
+        return options == null ? null : Arrays.copyOf(options, options.length);
     }
 
     /**
@@ -2139,7 +2135,9 @@
     public void setSelectionValues(Object[] newValues) {
         Object[]           oldValues = selectionValues;
 
-        selectionValues = newValues;
+        selectionValues = newValues == null
+                ? null
+                : Arrays.copyOf(newValues, newValues.length);
         firePropertyChange(SELECTION_VALUES_PROPERTY, oldValues, newValues);
         if(selectionValues != null)
             setWantsInput(true);
@@ -2152,7 +2150,9 @@
      * @see #setSelectionValues
      */
     public Object[] getSelectionValues() {
-        return selectionValues;
+        return selectionValues == null
+                ? null
+                : Arrays.copyOf(selectionValues, selectionValues.length);
     }
 
     /**
--- a/jdk/src/java.desktop/share/classes/sun/swing/FilePane.java	Mon Jan 23 11:59:48 2017 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/swing/FilePane.java	Tue Jan 24 11:37:32 2017 +0300
@@ -548,7 +548,7 @@
             actions = actionList.toArray(new Action[actionList.size()]);
         }
 
-        return actions;
+        return Arrays.copyOf(actions, actions.length);
     }
 
     protected void createActionMap() {