0

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

1 回答 1

0

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

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

java.beans.Statement 调用方法内联文档解释说:“对于类方法,[它]通过将实际类的静态方法与“Class.class”和构造函数定义的重载“newInstance”方法。”

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

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

于 2012-09-02T02:48:31.860 回答