JDK-4967135 : Encoding Colors and Fonts in Applets with XMLEncoder throws Exceptions
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2003-12-11
  • Updated: 2005-05-03
  • Resolved: 2005-05-03
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
6Resolved
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 12/11/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OS VERSION :
Linux bern 2.4.18-4GB #1 Wed Mar 27 13:57:05 UTC 2002 i686 unknown


A DESCRIPTION OF THE PROBLEM :
Encoding Colors and Fonts in Applets with XMLEncoder throws Exceptions (both, either using appletviewer or java-plugin).
These exceptions are not thrown using a JFrame or in non-gui apps!!!

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to encode a Color or a Font with XMLEncoder inside an Applet.

EXPECTED VERSUS ACTUAL BEHAVIOR :
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="java.awt.Color">
  <int>255</int>
  <int>0</int>
  <int>0</int>
  <int>255</int>
 </object>
 <object class="java.awt.Font">
  <string>Arial</string>
  <int>0</int>
  <int>20</int>
 </object>
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Color0);
        at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:339)
        at java.beans.XMLEncoder.writeObject(XMLEncoder.java:253)
        at test.XMLEncoderTestPanel.encode(XMLEncoderTestPanel.java:60)
        at test.XMLEncoderTestApplet.init(XMLEncoderTestApplet.java:37)
        at sun.applet.AppletPanel.run(AppletPanel.java:347)
        at java.lang.Thread.run(Thread.java:536)
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Font0);
        at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:339)
        at java.beans.XMLEncoder.writeObject(XMLEncoder.java:253)
        at test.XMLEncoderTestPanel.encode(XMLEncoderTestPanel.java:61)
        at test.XMLEncoderTestApplet.init(XMLEncoderTestApplet.java:37)
        at sun.applet.AppletPanel.run(AppletPanel.java:347)
        at java.lang.Thread.run(Thread.java:536)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Class XMLEncoderTestApplet:


import java.awt.*;
import javax.swing.*;

public class XMLEncoderTestApplet extends JApplet {
  
  public XMLEncoderTestApplet() {
  }

  public void init() {
    super.init();
    XMLEncoderTestPanel panel = new XMLEncoderTestPanel();
    getContentPane().add(BorderLayout.CENTER, panel);
    panel.encode();
  }
  
}



Class XMLEncoderTestFrame:


import java.awt.*;
import javax.swing.*;

public class XMLEncoderTestFrame extends JFrame {
  
  public XMLEncoderTestFrame() {
    initComponents();
    XMLEncoderTestPanel panel = new XMLEncoderTestPanel();
    getContentPane().add(BorderLayout.CENTER, panel);
    panel.encode();
    pack();
  }
  
  private void initComponents() {
    
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        exitForm(evt);
      }
    });
    
    pack();
  }
  
  /** Exit the Application */
  private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
  }
  
  public static void main(String args[]) {
    new XMLEncoderTestFrame().show();
  }
  
}



Class XMLEncoderTestPanel:


import java.awt.Color;
import java.awt.Font;
import java.beans.*;
import java.io.*;
import java.util.*;
import javax.swing.*;


public class XMLEncoderTestPanel extends JPanel {
  
  public XMLEncoderTestPanel() {
    initComponents();
  }

  private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();

    setLayout(new java.awt.BorderLayout());

    jTextArea1.setEditable(false);
    jScrollPane1.setViewportView(jTextArea1);

    add(jScrollPane1, java.awt.BorderLayout.CENTER);

  }
  
  public void encode() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    XMLEncoder encoder = new XMLEncoder(bos);
    encoder.setExceptionListener(new ExceptionListener(){
      public void exceptionThrown(Exception ex){
        ex.printStackTrace();
      }
    });
    try{
      encoder.writeObject(Color.RED);
      encoder.writeObject(new Font("Arial", 0, 20));
      encoder.flush();
      jTextArea1.setText(baos.toString());
    } catch(Exception ex1){
      ex1.printStackTrace();
    } finally {
      encoder.close();
    }
  }
  
  
  // Variables declaration - do not modify
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTextArea jTextArea1;
  // End of variables declaration
  
}



XMLEncoderTestApplet.html:


<HTML>
<HEAD>
   <TITLE>Applet HTML Page</title>
</head>
<BODY>

<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></h3>

<P>
<APPLET code="XMLEncoderTestApplet.class" width=350 height=200></applet>
</p>

<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</i></font>
</body>
</html>



---------- END SOURCE ----------
(Incident Review ID: 183149) 
======================================================================

Comments
EVALUATION Since the bug occurs with both appletviewer and plugin, it isn't a bug in appletviewer. Based on the exception, my first guess is beans. Incidentally, java.awt.Color and java.awt.Font both belong to 2D. ###@###.### 2003-12-11 The bug exist in 1.4.2 and in 1.5 as well. The problem is that the DefaultPersistenceDelegate will make the private fields for font and color accessible but the privileges have not been raised: try { // System.out.println("Trying field " + name + " in " + type); f = type.getDeclaredField(name); f.setAccessible(true); } The true stack trace is: java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Color); at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:342) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:257) at XMLEncoderTestPanel.encode(XMLEncoderTestPanel.java:38) at XMLEncoderTestApplet.init(XMLEncoderTestApplet.java:13) at sun.applet.AppletPanel.run(AppletPanel.java:354) at java.lang.Thread.run(Thread.java:565) Caused by: java.security.AccessControlException: access denied (java.lang.reflect.ReflectPermission suppressAccessChecks) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264) at java.security.AccessController.checkPermission(AccessController.java:401) at java.lang.SecurityManager.checkPermission(SecurityManager.java:524) at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:107) at java.beans.DefaultPersistenceDelegate.instantiate(DefaultPersistenceDelegate.java:154) at java.beans.PersistenceDelegate.writeObject(PersistenceDelegate.java:97) at java.beans.Encoder.writeObject(Encoder.java:54) at java.beans.XMLEncoder.writeObject(XMLEncoder.java:254) at java.beans.Encoder.writeObject1(Encoder.java:184) at java.beans.Encoder.cloneStatement(Encoder.java:197) at java.beans.Encoder.writeStatement(Encoder.java:228) at java.beans.XMLEncoder.writeStatement(XMLEncoder.java:329) ... 5 more ###@###.### 2004-01-12 I found out that we have very similar bug - 4741757, that has better synopsis and analysis, also has same priority. So I close this bug as a duplicate of 4741757. ###@###.### 2005-05-03 13:30:20 GMT
03-05-2005

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: dragon
14-06-2004

WORK AROUND Sign the applet. There are probably a lot of security issues related to the XMLEncoder in a sandboxed environment - both applets and web start.
11-06-2004