通过反复试验,我发现标准 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)