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

Regression: java.awt.Font is no longer serializable

XMLWordPrintable

    • 2d
    • sparc
    • solaris_2.6

      To reproduce the bug, first apply this patch to
      src/share/classes/java/awt/Frame.java:

      *** Frame.java Mon Jun 8 15:52:48 1998
      --- Frame2.java Mon Jun 8 15:54:37 1998
      ***************
      *** 131,137 ****
             * instead of 'this' so that garbage collection can still take
             * place correctly.
             */
      ! private SoftReference softThis;
        
            private static final String base = "frame";
            private static int nameCounter = 0;
      --- 131,137 ----
             * instead of 'this' so that garbage collection can still take
             * place correctly.
             */
      ! transient private SoftReference softThis;
        
            private static final String base = "frame";
            private static int nameCounter = 0;
      ***************
      *** 500,506 ****
                  ownedWindows = null;
              }
        
      ! addToFrameList();
            }
        }
        
      --- 500,507 ----
                  ownedWindows = null;
              }
        
      ! softThis = new SoftReference(this);
      ! addToFrameList();
            }
        }




      Now, run the following test program:

      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.applet.*;

      public class ComponentSerializationTest extends Applet {

          public ComponentSerializationTest() {
              new MainFrame();
          }

          public static void main(String[] argc){
              Frame f = new MainFrame();
              f.addWindowListener( new WindowAdapter() {
                                          public void windowClosing(WindowEvent ev) {
                                              System.exit(0);
                                          }
                                      }
                                  );
          }
      }

      class MainFrame extends Frame {

          public MainFrame(){
              super("PrintComponentTest");

              setSize(600, 400);
              setLayout(new FlowLayout());

              ///////////////////////
              // as printing bugs are fixed, add your component here
              // to get it tested for printing

              // peered components
              add(new Button("Button") );
              add(new TestCanvas());
              add(new Checkbox("Checkbox", true) );
              Choice choice = new Choice();
              choice.add("Choice 1");
              choice.add("Choice Two");
              add(choice);
              add(new Label("Label") );
              List list = new List();
              list.add("List 1");
              list.add("List Two");
              add(list);
              add(new Scrollbar(Scrollbar.VERTICAL) );
              add(new Scrollbar(Scrollbar.HORIZONTAL) );
              ScrollPane scrollpane = new ScrollPane();
              scrollpane.add(new Button("Button in a scrollpane"));
              add(scrollpane);
              add(new TextArea("TextArea", 3, 30));
              add(new TextField("TextField"));

              // nested components
              Panel panel1 = new Panel();
              panel1.setLayout(new FlowLayout());
              panel1.setBackground(Color.red);
              this.add(panel1);

              panel1.add(new Button("level 2"));

              Panel panel2 = new Panel();
              panel2.setLayout(new FlowLayout());
              panel2.setBackground(Color.green);
              panel1.add(panel2);

              panel2.add(new Button("level 3"));


              // lightweight components
              add(new LWButton("LWbutton") );

              // overlapping components
              add(new ZOrderPanel());

              ///////////////////////

              Menu menu = new Menu("Serialize");
              Menu menu2 = new Menu("File");
              Menu menu3 = new Menu("Edit");
              Menu menu4 = new Menu("ReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyRe
      allyReallyReallyReallyReallyReallyReallyReallyLong");
              menu2.setFont(new Font("SansSerif", Font.BOLD, 20));
              menu2.setEnabled(false);
              menu3.setFont(new Font("Monospaced", Font.ITALIC, 18));
              menu3.setEnabled(false);
              menu4.setEnabled(false);
              MenuItem itemSerialize = new MenuItem("Serialize!");
              CheckboxMenuItem itemCheck = new CheckboxMenuItem("Check me");
              menu.add(itemSerialize);
              menu.add(itemCheck);
              MenuBar menuBar = new MenuBar();
              menuBar.add( menu );
              menuBar.add( menu2 );
              menuBar.add( menu3 );
              menuBar.add( menu4 );
              setMenuBar(menuBar);

              itemSerialize.addActionListener( new ActionSerialize() );
              setVisible(true);
          }

          class ActionSerialize implements ActionListener {

              public void actionPerformed(ActionEvent ev) {
                  Frame f2 = null;

                  try {
                      FileOutputStream fos = new FileOutputStream("tmp");
                      ObjectOutputStream oos = new ObjectOutputStream(fos);
                      oos.writeObject(Frame.this);
                      oos.flush();
                      FileInputStream fis = new FileInputStream("tmp");
                      ObjectInputStream ois = new ObjectInputStream(fis);
                      f2 = (Frame)ois.readObject();
                  }
                  catch (Exception e) {
                      e.printStackTrace(System.out);
                      System.exit(1);
                  }

                  if (f2 != null) {
                      f2.setVisible(true);
                  }
                  else {
                      System.out.println(new NullPointerException("f2 is null"));
                      System.exit(1);
                  }
              }
          }

          class LWButton extends Component {
              String label;
              int width = 100;
              int height = 30;

              public LWButton(String label) {
                  super();
                  this.label = label;
              }

              public void paint(Graphics g) {
                  Dimension d = getSize();
                  g.setColor(Color.orange);
                  g.fillRect(0, 0, d.width, d.height);
                  g.setColor(Color.black);
                  int x = 5;
                  int y = (d.height - 5);
                  g.drawString(label, x, y);
              }

              public Dimension getPreferredSize()
              {
                  return new Dimension(width,height);
              }
          }

          class TestCanvas extends Canvas {
              int width = 100;
              int height = 100;

              public void paint(Graphics g) {
                  g.setColor(Color.blue);
                  g.fillRoundRect(10, 10, 50, 50, 15, 30);
                  g.setColor(Color.red);
                  g.fillOval(70, 70, 25, 25);
              }
              public Dimension getPreferredSize() {
                  return new Dimension(width, height);
              }
          }

          class ZOrderPanel extends Panel
          {
              ZOrderPanel()
              {
                  setLayout(null);

                  Component first, second, third, fourth;

                  show();
                  // add first component
                  first = makeBox("Second", Color.blue, -1);
                  // insert on top
                  second = makeBox("First", Color.yellow, 0);
                  // put at the back
                  fourth = makeBox("Fourth", Color.red, 2);
                  // insert in last position
                  third = makeBox("Third", Color.green, 3);
                  // swap third and fourth to correct positions
                  remove(third);
                  add(third, 2);
                  // re-validate so third and fourth peers change position
                  validate();
                  // now make things really interesting with a lightweight
                  // component at the top of the z-order, that should print
                  // _below_ the native guys to match the screen...
                  add(new LWButton("LWButton"),0);
              }

              public Dimension preferredSize()
              {
                  return new Dimension(260, 80);
              }

              public void layout()
              {
                  int i, n;
                  Insets ins = insets();
                  n = countComponents();
                  for (i = n-1; i >= 0; i--) {
                          Component p = getComponent(i);
                          p.reshape(ins.left + 40 * i, ins.top + 5 * i, 60, 60);
                  }
              }

              public Component makeBox(String s, Color c, int index)
              {
                  Label l = new Label(s);
                  l.setBackground(c);
                  l.setAlignment(Label.RIGHT);
                  add(l, index);
                  validate();
                  return l;
              }
          }
      }

      From the Serialize menu, select Serialize!. The following Exception appears:

      java.io.NotSerializableException: java.awt.font.TextAttribute
              at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:835)
              at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:334)
              at java.util.Hashtable.writeObject(Hashtable.java:727)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectOutputStream.invokeObjectWriter(ObjectOutputStream.java:1576)
              at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:897)
              at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:334)
              at java.io.ObjectOutputStream.outputClassFields(ObjectOutputStream.java:1558)
              at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:445)
              at java.awt.Font.writeObject(Font.java:620)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectOutputStream.invokeObjectWriter(ObjectOutputStream.java:1576)
              at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:897)
              at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:334)
              at java.io.ObjectOutputStream.outputClassFields(ObjectOutputStream.java:1558)
              at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:445)
              at java.awt.Component.writeObject(Component.java:3533)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectOutputStream.invokeObjectWriter(ObjectOutputStream.java:1576)
              at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:897)
              at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:334)
              at MainFrame$ActionSerialize.actionPerformed(ComponentSerializationTest.java:119)
              at java.awt.MenuItem.processActionEvent(MenuItem.java:440)
              at java.awt.MenuItem.processEvent(MenuItem.java:404)
              at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:189)
              at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:181)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:225)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)

      The test program works correctly in beta3, and is thus a beta4 regression.

            pcharltosunw Paul Charlton (Inactive)
            dmendenhsunw David Mendenhall (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: