JDK-6613060 : Persisting JFrame with XMLEncoder gives error in Java SE 6
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: linux
  • CPU: x86
  • Submitted: 2007-10-04
  • Updated: 2011-02-16
  • Resolved: 2010-05-24
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 6 JDK 7
6u10Fixed 7Resolved
Description
FULL PRODUCT VERSION :
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b05)
Java HotSpot(TM) Server VM (build 1.6.0_02-b05, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux thinkpad-x60 2.6.20-16-generic #2 SMP Sun Sep 23 19:50:39 UTC 2007 i686 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Saving a JFrame with XMLEncoder gives the error report shown below.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program that follows. Click Save. Type test.xml for the file name. Watch the error report.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expected not to see an error report. (There was none under SE 5.0)
ACTUAL -
I saw the aforementioned error report.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.InstantiationException: javax.swing.BufferStrategyPaintManager$BufferInfo
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement JFrame.addWindowListener(BufferStrategyPaintManager$BufferInfo);
Continuing ...

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import javax.swing.*;

/**
   This program demonstrates the use of an XML encoder and decoder to save and restore a frame.
*/
public class PersistentFrameTest
{
   public static void main(String[] args)
   {
      chooser = new JFileChooser();
      chooser.setCurrentDirectory(new File("."));
      PersistentFrameTest test = new PersistentFrameTest();
      test.init();
   }

   public void init()
   {
      frame = new JFrame();
      frame.setLayout(new FlowLayout());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("PersistentFrameTest");
      frame.setSize(400, 200);

      JButton loadButton = new JButton("Load");
      frame.add(loadButton);
      loadButton.addActionListener(EventHandler.create(ActionListener.class, this, "load"));

      JButton saveButton = new JButton("Save");
      frame.add(saveButton);
      saveButton.addActionListener(EventHandler.create(ActionListener.class, this, "save"));
     
      frame.setVisible(true);
   }

   public void load()
   {
      // show file chooser dialog
      int r = chooser.showOpenDialog(null);

      // if file selected, open
      if(r == JFileChooser.APPROVE_OPTION)
      {
         try
         {
            File file = chooser.getSelectedFile();
            XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
            decoder.readObject();
            decoder.close();
         }
         catch (IOException e)
         {
            JOptionPane.showMessageDialog(null, e);
         }
      }
   }

   public void save()
   {
      if(chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
      {
         try
         {
            File file = chooser.getSelectedFile();
            XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
            encoder.writeObject(frame);
            encoder.close();
         }
         catch (IOException e)
         {
            JOptionPane.showMessageDialog(null, e);
         }
      }
   }

   private static JFileChooser chooser;
   private JFrame frame;
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Close both eyes when the program runs.

Release Regression From : 5.0
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION It is not reproducible since b76 at least.
24-05-2010

EVALUATION XMLEncoder does not support private classes i.e. javax.swing.BufferStrategyPaintManager$BufferInfo because of security. Seems we should create additional persistence delegate for this class in the MetaData.java file.
19-11-2007