Skip to main content

JDK7 breaks backward compatibility with XMLEncoder

9 replies [Last post]
vity01
Offline
Joined: 2006-03-25

See following code:

<br />
import java.beans.*;<br />
import java.io.IOException;<br />
import java.net.MalformedURLException;<br />
import java.net.URL;</p>
<p>public class TestEncoder {<br />
    public static void main(String[] args) throws IOException {<br />
        XMLEncoder e = new XMLEncoder(System.out);<br />
        e.setPersistenceDelegate(URL.class, new PrimitivePersistenceDelegate());<br />
        e = new XMLEncoder(System.out);//intention<br />
        final TestBean bean = new TestBean();<br />
        try {<br />
            bean.setUrl(new URL("http://test"));<br />
        } catch (MalformedURLException e1) {<br />
            e1.printStackTrace();<br />
        }<br />
        final MyExceptionListener exListener = new MyExceptionListener();<br />
        e.setExceptionListener(exListener);<br />
        e.writeObject(bean);<br />
        if (exListener.ex != null)<br />
            throw new IOException("XMLEnc failed", exListener.ex);<br />
        e.close();<br />
    }</p>
<p>    static class PrimitivePersistenceDelegate extends PersistenceDelegate {</p>
<p>        protected Expression instantiate(Object oldInstance, Encoder out) {<br />
            return new Expression(oldInstance, oldInstance.getClass(),<br />
                    "new", new Object[]{oldInstance.toString()});<br />
        }<br />
    }</p>
<p>    private static class MyExceptionListener implements ExceptionListener {<br />
        Exception ex;</p>
<p>        public void exceptionThrown(Exception e) {<br />
            if (this.ex == null)<br />
                this.ex = e;<br />
        }<br />
    }</p>
<p>    public static class TestBean {<br />
        private URL url;</p>
<p>        public TestBean() {</p>
<p>        }</p>
<p>        public URL getUrl() {<br />
            return url;<br />
        }</p>
<p>        public void setUrl(URL url) {<br />
            this.url = url;<br />
        }<br />
    }</p>
<p>}<br />

This demo code produces different results for JDK 1.6.0_14 and for JDK 7 M4.
JDK 7
<br />
<?xml version="1.0" encoding="UTF-8"?></p>
<p>

JDK 1.6

<br />
<?xml version="1.0" encoding="UTF-8"?> </p>
<p>    <a href="http://test" title="http://test">http://test</a> </p>
<p>

JDK 1.6 serializes this property, but XMLEncoder stopped to cache persistence delegates => property is not serialized.

Is this an intention? JSR296 - Swing Application Framework uses such code => it will stop to work.

+demo output, fix in testcase

Message was edited by: vity01

+ Notes

Message was edited by: vity01

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11

just curious - did you file a report in Sun's bug parade? If so, it might be helpful if you could mail me the review id (or forward the automatic response mail) - then I could try to push it a bit.

Thanks
Jeanette

PS: did you get my mail (could have been "no connection - wrong number" ;)?

vity01
Offline
Joined: 2006-03-25

Here it is:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6882911

What email? I didn't get anything from you during last few days.

kleopatra
Offline
Joined: 2003-06-11

cool - they were quick in accepting it (for their standards )

Now lets drum up support, voting for it isn't much, but better than nothing.

Cheers
Jeanette

PS: the mail contained nothing more than my question posted here afterwards. So nothing important lost.

vity01
Offline
Joined: 2006-03-25

I waited 14 days ;-) for dispatch state :-)

kleopatra
Offline
Joined: 2003-06-11

I CANT believe it ... you **** or what? This is breaking backward compatibility big style. Each and every single application out there which uses XMLEncoder will simply stop working.

Yet another example for Sun's carelessness when it comes to compatibility, nothing but bigot lip-service ...

Red hot furious (and that on a Monday morning ...)
Jeanette

vity01
Offline
Joined: 2006-03-25

LOL, angry Janette - I would like to see that :-).
I am still waiting for response from developers.

kleopatra
Offline
Joined: 2003-06-11

even more enraging - this time against myself ;-) than breaking backward compatibility is that the old behaviour was undocumented, that is an implemenation detail. Haha ...

The old issue:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4968523

suggested to document the static behaviour and a first suggestion was to make the setPersistenceDelegate static, so I thought it was rather safe to assume that would be what would happen. The final decision comes without given a why:

It is necessary to remove static behavior.
Posted Date : 2007-02-02 14:00:36.0

Hehe, and they had been well aware that they changed behaviour, seeing a comment in a report that was closed as the duplicate of the above (#4467303):

This bug is fixed together with 4968523,
because that fix afftect the behavior of method
Encoder.setPersistenceDelegate.
Posted Date : 2007-02-05 18:20:50.0

And yet another nice reading from the early days:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4474171

CU
Jeanette

atehrani
Offline
Joined: 2004-01-14

Commenting out line 10 provides me the correct output on JDK7 b70

vity01
Offline
Joined: 2006-03-25

As you can notice my comment, that was an intention.
Previous versions used caching. Many existing applications rely on that.