newInstance默认使用XMLEncoder / Decoder调用?

时间:2012-09-01 12:57:57

标签: xml-serialization xmlencoder

通过反复试验,我发现标准的java.bean XMLEncoder / Decoder总是使用静态工厂“newInstance”方法,而不是null构造函数(但只有当它被命名为“newInstance”时) 。我找不到这是任何文档或DefaultPersistence委托。

我找错了地方吗?

示例:

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();
}
}

`

运行loadTestFile时来自newInstance的堆栈转储是:

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 个答案:

答案 0 :(得分:0)

我想我已经解决了这个问题。

与Oracle Bug ID 4920456相关,其中建议将 newInstance()静态作为解决方案 - 但这会导致调用静态方法而不是null构造函数。

java.beans.Statement invoke 方法内联文档解释了“对于类方法,[它]通过采用静态方法的并集来模拟元类的影响实际类的实现,使用“Class.class”的实例方法和构造函数定义的重载“newInstance”方法。“

副作用似乎是当一个类中存在名为 newInstance 的无参数静态方法时,当 java.lang.Class.newInstance()时调用此方法应该调用方法来调用null构造函数。

解决方案不是使用“newInstance”作为使用XMLEncoder的类中的方法名称。