JDK-6245149 : java.beans.XMLEncoder does not enocde java.net.URI objects
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.beans
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2005-03-23
  • Updated: 2010-04-02
  • Resolved: 2005-08-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
6 b46Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux 2.6.7-1-686 #1 Thu Jul 8 05:36:53 EDT 2004 i686 GNU/Linux
(Debian unstable)

A DESCRIPTION OF THE PROBLEM :
The following code is affected by the problem:

		XMLEncoder e = new XMLEncoder( System.out);
		URI uri = new URI("/foo/bar");
		e.writeObject( uri);
		e.close();

It results in the following error message and XML output:

java.lang.IllegalAccessException: Class java.beans.Statement can not access a member of class java.net.URI with modifiers "private"
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(URI0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecoder">
</java>

The problem occurs, because the URI class does not provide a default constructor (which wouldn't make sense, since it's immutable).

The problem can be solved by providing an appropriate PersistenceDelegate. It would be nice, if that could be provided with a future JDK release. If not, please at least consider throwing an exception with a better error description.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the supplied source code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecoder">
 <object class="java.net.URI">
  <null/>
  <null/>
  <string>/foo/bar</string>
  <null/>
  <null/>
 </object>
</java>

ACTUAL -
java.lang.IllegalAccessException: Class java.beans.Statement can not access a member of class java.net.URI with modifiers "private"
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(URI0);
Continuing ...
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2" class="java.beans.XMLDecoder">
</java>


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.IllegalAccessException: Class java.beans.Statement can not access a member of class java.net.URI with modifiers "private"
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(URI0);
Continuing ...


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.beans.XMLEncoder;
import java.beans.DefaultPersistenceDelegate;
import java.net.URI;

public class URIEncodingBug {

	public static void main(String[] argv)
		throws Exception
	{
		XMLEncoder e = new XMLEncoder( System.out);
		URI uri = new URI("/foo/bar");
		e.writeObject( uri);
		e.close();
	}
}

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

CUSTOMER SUBMITTED WORKAROUND :
Uncomment the e.setPersistenceDelegate line...
###@###.### 2005-03-23 22:14:53 GMT

Comments
EVALUATION Yes, there is no nullary constructor for URI so that a custom persistence delegate needs to be registered. ###@###.### 2005-07-19 16:02:07 GMT
19-07-2005