0

我有一个用于克隆的 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 它说:

使其适用于具有高消息吞吐量的大型对象图或系统。

堆栈跟踪:

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)
4

1 回答 1

1

嗨,我已经解决了这个问题,另一种方法是在这个学生类上创建一个浅拷贝,当然,当 XStream 得到这个类时,XStream 的关系还不够深,并且 NullPointerException 是 Throw .. 感谢 Dave God Bless。

于 2013-05-10T11:14:34.767 回答