JDK-6459800 : Some Swing classes violate encapsulation by returning internal Insets
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_10
  • CPU: x86
  • Submitted: 2006-08-14
  • Updated: 2015-09-29
  • Resolved: 2015-01-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u60Fixed 9 b52Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.6.0-rc-b94)


A DESCRIPTION OF THE PROBLEM :
These classes:
class javax.swing.JMenu
class javax.swing.JMenuItem
class javax.swing.JCheckBoxMenuItem
class javax.swing.JInternalFrame
class javax.swing.JRadioButtonMenuItem
class javax.swing.JPopupMenu
class javax.swing.JScrollPane

Return an Insets object that when altered, alters the Insets that is used by the class.  In other words their returning internals.  They should either document this or be made consistent with the other Swing classes that according to the test case return copies of the Insets object.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test with assertions enabled.  See which classes fail.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
that copies are returned
ACTUAL -
internals are returned

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;

public class TestInsetsEncapsulation implements Runnable{

    public void run(){

        runTest(new JLabel("hi"));
        runTest(new JMenu());
        runTest(new JTree());
        runTest(new JTable());
        runTest(new JMenuItem());
        runTest(new JCheckBoxMenuItem());
        runTest(new JToggleButton());
        runTest(new JSpinner());
        runTest(new JSlider());
        runTest(Box.createVerticalBox());
        runTest(Box.createHorizontalBox());
        runTest(new JTextField());
        runTest(new JTextArea());
        runTest(new JTextPane());
        runTest(new JPasswordField());
        runTest(new JFormattedTextField());
        runTest(new JEditorPane());
        runTest(new JButton());
        runTest(new JColorChooser());
        runTest(new JFileChooser());
        runTest(new JCheckBox());
        runTest(new JInternalFrame());
        runTest(new JDesktopPane());
        runTest(new javax.swing.table.JTableHeader());
        runTest(new JLayeredPane());
        runTest(new JRootPane());
        runTest(new JMenuBar());
        runTest(new JOptionPane());
        runTest(new JRadioButton());
        runTest(new JRadioButtonMenuItem());
        runTest(new JPopupMenu());
        runTest(new JScrollBar());
        runTest(new JScrollPane());
        runTest(new JViewport());
        runTest(new JSplitPane());
        runTest(new JTabbedPane());
        runTest(new JToolBar());
        runTest(new JSeparator());
        runTest(new JProgressBar());
        System.out.println("These classes failed");
        for(Component failure: failures)
	    System.out.println(failure.getClass());
    }
    java.util.List<Component> failures = new java.util.ArrayList<Component>();
    public void runTest(JComponent c){

        try{
            test(c);
        }catch(Throwable e){
            e.printStackTrace();
	    System.out.println(c.getClass() + " Crashed test");
            failures.add(c);
        }
    }

    public void test(JComponent c){
        Insets p = c.getInsets();
        p.top += 200;
        assert(!p.equals(c.getInsets())): "Insets altered by altering Insets!";
    }

    public static void main(String ... args){
	SwingUtilities.invokeLater(new TestInsetsEncapsulation());
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
copy Insets when returned.

Comments
EVALUATION The problematic classes are javax.swing.JMenu javax.swing.JMenuItem javax.swing.JCheckBoxMenuItem javax.swing.JInternalFrame javax.swing.JRadioButtonMenuItem javax.swing.JPopupMenu javax.swing.JScrollPane need to return defensive copy of insets
15-08-2006