abstractnestedcircularreferencetest.java

来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 57 行

JAVA
57
字号
package com.thoughtworks.acceptance;import java.io.Serializable;import java.io.ObjectInputStream;import java.io.IOException;import java.io.ObjectOutputStream;public abstract class AbstractNestedCircularReferenceTest extends AbstractAcceptanceTest {    public static class WeirdThing implements Serializable {        public transient Object anotherObject;        private NestedThing nestedThing = new NestedThing();        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {            in.defaultReadObject();            anotherObject = in.readObject();        }        private void writeObject(ObjectOutputStream out) throws IOException {            out.defaultWriteObject();            out.writeObject(anotherObject);        }        private class NestedThing implements Serializable {            private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {                in.defaultReadObject();            }            private void writeObject(ObjectOutputStream out) throws IOException {                out.defaultWriteObject();            }        }    }    public void testWeirdCircularReference() {        // I cannot fully explain what's special about WeirdThing, however without ensuring that a reference is only        // put in the references map once, this fails.        // This case was first noticed when serializing JComboBox, deserializing it and then serializing it again.        // Upon the second serialization, it would cause the Sun 1.4.1 JVM to crash:        // Object in = new javax.swing.JComboBox();        // Object out = xstream.fromXML(xstream.toXML(in));        // xstream.toXML(out); ....causes JVM crash on 1.4.1        // WeirdThing is the least possible code I can create to reproduce the problem.        // setup        WeirdThing in = new WeirdThing();        in.anotherObject = in;        // execute        WeirdThing out = (WeirdThing) xstream.fromXML(xstream.toXML(in));        // verify        assertSame(out, out.anotherObject);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?