newInstance called by default using XMLEncoder/Decoder?

347 Views Asked by At

By trial and error, I have found that the standard java.bean XMLEncoder/Decoder will always use a static factory "newInstance" method when one is provided rather than the null constructor (but only if it is named "newInstance") . I can not find this is any documentation or in the DefaultPersistence delegate.

Am I looking in the wrong places?

Example:

public class TestClass {

private boolean changed = false;

public TestClass() {
}

public static TestClass newInstance() {
    Thread.dumpStack();
    return new TestClass();
}

public boolean isChanged() {
    return changed;
}

public void setChanged(boolean changed) {
    this.changed = changed;
}

public static void doTestSave() throws FileNotFoundException, IOException {
    BufferedOutputStream buffer = new BufferedOutputStream(new java.io.FileOutputStream("Test.xml"));
    XMLEncoder e = new XMLEncoder(buffer);
    Thread.currentThread().setContextClassLoader(TestClass.class.getClassLoader());
    TestClass t = new TestClass();
    t.setChanged(true);
    e.writeObject(t);
    e.close();
    buffer.close();
}

public static Object loadTestFile() throws FileNotFoundException, IOException {
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("Test.xml"));
    XMLDecoder e = new XMLDecoder(buffer);
    e.close();
    buffer.close();
        return e.readObject();
}
}

`

The stack dump from newInstance when loadTestFile is run is:

at java.lang.Thread.dumpStack(Thread.java:1273)
at kcl.waterloo.XMLCoder.TestClass.newInstance(TestClass.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:37)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:244)
at java.beans.Statement.invokeInternal(Statement.java:239)
at java.beans.Statement.access$000(Statement.java:39)
at java.beans.Statement$2.run(Statement.java:140)
at java.security.AccessController.doPrivileged(Native Method)
at java.beans.Statement.invoke(Statement.java:137)
at java.beans.Expression.getValue(Expression.java:98)
at com.sun.beans.MutableExpression.getValue(ObjectHandler.java:445)
at com.sun.beans.ObjectHandler.getValue(ObjectHandler.java:108)
at com.sun.beans.ObjectHandler.eval(ObjectHandler.java:130)
at com.sun.beans.ObjectHandler.startElement(ObjectHandler.java:238)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:142)
at java.beans.XMLDecoder.getHandler(XMLDecoder.java:238)
at java.beans.XMLDecoder.readObject(XMLDecoder.java:201)
at kcl.waterloo.XMLCoder.TestClass.loadTestFile(TestClass.java:74)
1

There are 1 best solutions below

1
On

I think I have worked this out.

Relates to Oracle Bug ID 4920456 where making newInstance() static was suggested as a solution - but this leads to the static method being called instead of the null constructor.

The java.beans.Statement invoke method in-line docs explains that "For class methods, [it] simluate[s] the effect of a meta class by taking the union of the static methods of the actual class, with the instance methods of "Class.class" and the overloaded "newInstance" methods defined by the constructors."

A side-effect seems to be that when a no argument static method named newInstance is present in a class, this method is called when the java.lang.Class.newInstance() method should be called to invoke the null constructor.

The solution is not to use "newInstance" as a method name in a class using XMLEncoder.