Java XStream Deep Copy 引发异常 ObjectAccessException [英] Java XStream Deep Copy raises Exception ObjectAccessException

查看:20
本文介绍了Java XStream Deep Copy 引发异常 ObjectAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a XStream for cloning. Here is my simple code, I have not much expertise with it.

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

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

Later I store the clone instances in a hashTable (I know is not very good idea storing on it but is a legacy system).

我将它存储为 Student 的一个类,然后我存储(克隆)Student 的另一个实例并提出.

I store it a class of Student, later I store (clone) other instance of Student and raises.

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

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

Here is my code for storing the clone objects.

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

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

I think here is the source code where the problem is arising. [XStream sources.][Snippet]

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

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

I hope somebody can guide me, I am a little lost on this topic.

这是痕迹.当我清理 clazz 示例 student.setListOfPhones(null) 的依赖项时,它似乎有效;异常是在图中的 2 或 3 级抛出.根据 XStream 的说法,为什么会这样:

Here is the trace. It seems when I clean the dependences of the clazz example student.setListOfPhones(null) it works; the exception is throw on the 2 or 3 level in the graph. Why is this according to XStream it says:

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

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 是 Throw.. 感谢戴夫上帝保佑.

hi i have solved the problem was another method was creating a shallow copy on this class Student and of course when XStream got the class the relationship wasn't deep enough for XStream and NullPointerException was Throw.. thank Dave god Bless.

这篇关于Java XStream Deep Copy 引发异常 ObjectAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆