gpt4 book ai didi

Java XStream Deep Copy 引发异常 ObjectAccessException

转载 作者:行者123 更新时间:2023-12-01 14:36:13 25 4
gpt4 key购买 nike

我有一个用于克隆的 XStream。这是我的简单代码,我对此没有太多专业知识。

com.thoughtworks.xstream.XStream XSTREAM = new com.thoughtworks.xstream.XStream();

后来我将克隆实例存储在哈希表中(我知道存储在其上并不是一个好主意,但它是一个遗留系统)。

我将其存储为 Student 类,稍后我存储(克隆)Student 的其他实例并引发。

com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call com.model.Student_$$_javassist_83.writeReplace():null java.lang.NullPointerException

这是我用于存储克隆对象的代码。

public void keep(String key, Object value) 
{
Object obj = XSTREAM.fromXML(XSTREAM.toXML(value));
storage.put(key,obj);
}

我认为这是出现问题的源代码。 [XStream 来源。][片段]

 public Object callWriteReplace(Object object) 
{
Method writeReplaceMethod = getMethod(object.getClass(), "writeReplace", null, true);
if (writeReplaceMethod != null) {
try {
Object[] EMPTY_ARGS = new Object[0];
return writeReplaceMethod.invoke(object, EMPTY_ARGS);
} catch (IllegalAccessException e) {
throw new ObjectAccessException("Could not call " + object.getClass().getName() + ".writeReplace()", e);
} catch (InvocationTargetException e) {
throw new ObjectAccessException("Could not call " + object.getClass().getName() + ".writeReplace()", e.getTargetException());
}
} else {
return object;
}
}

我希望有人能指导我,我对这个话题有点迷失。

这是痕迹。看起来,当我清理 clazz 示例 student.setListOfPhones(null) 的依赖项时,它就可以工作了;异常在图中的第 2 或第 3 层抛出。根据 XStream 的说法,这是为什么:

making it suitable for large object graphs or systems with high message throughput.

堆栈跟踪:

com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call com.model.Subjects_$$_javassist_224.writeReplace() : null
java.lang.NullPointerException
at javassist.util.proxy.RuntimeSupport$DefaultMethodHandler.invoke(RuntimeSupport.java:37)
at com.model.Subjects_$$_javassist_224.writeReplace(Subject_$$_javassist_224.java)
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 com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.callWriteReplace(SerializationMethodInvoker.java:88)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:60)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:229)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:208)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:171)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:116)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)

最佳答案

嗨,我已经解决了这个问题,另一种方法是在这个类 Student 上创建一个浅拷贝,当然,当 XStream 获取该类时,XStream 的关系不够深,并且抛出了 NullPointerException 。感谢 Dave 上帝保佑。

关于Java XStream Deep Copy 引发异常 ObjectAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com