Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6459800

Some Swing classes violate encapsulation by returning internal Insets

XMLWordPrintable

    • b52
    • x86
    • solaris_10

        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.

              serb Sergey Bylokhov
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: