Variable initialization past Xstream

ぃ、小莉子 提交于 2019-12-14 01:28:38

问题


Consider the following declaration as part of SomeClass

private Set<String> blah    = new HashSet<String>();

Made in a class, which is later

XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);

StringBuilder json = new StringBuilder(xstream.toXML(SomeClass));

rd = (SomeClass) xstream.fromXML(json.toString());

When i @Test

assertTrue(rd.getBlah().size() == 0);

I get an NPE on rd.getBlah()

When I, instead of initializing up front, place initialization to a constructor of SomeClass

public SomeClass() {
  blah = new HashSet<String>();
}

Same problem - NPE on rd.getBlah()

When i modify the getter to check for null first, it works, but ..

public Set<String> getBlah() {
   if (blah == null)
      return new HashSet<Sgring>();
   return blah;
}

I am puzzled ... Why does XStream not initialize variables and whether lazy instantiation is necessary?


回答1:


XStream uses the same mechanism as the JDK serialization. When using the enhanced mode with the optimized reflection API, it does not invoke the default constructor. The solution is to implement the readResolve method as below:

public class SomeClass{
    private Set<String> blah;

    public SomeClass(){
        // do stuff
    }

    public Set<String> getBlah(){
        return blah;
    }

    private Object readResolve() {
        if(blah == null){
            blah = new HashSet<String>();
        }
        return this;
    }
}

Reference



来源:https://stackoverflow.com/questions/10020156/variable-initialization-past-xstream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!