我正在使用带有循环引用和泛型的 protostuff 二进制文件。作为一个非常简单的场景,我有以下类:
public class A {
private String name;
private B b;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
this.b.setA(this);
}
}
///////////////////////////////////////////////////////////////////////////
public class B {
private String name;
private A a;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
///////////////////////////////////////////////////////////////////////////
public class Container<E> {
private E element;
private String name;
public Container() {
}
public Container(E e, String name) {
super();
this.element = e;
this.name = name;
}
public E getElement() {
return element;
}
public void setElement(E e) {
this.element = e;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
当我运行以下单元测试以检查往返序列化/反序列化是否正确执行时,我得到了一个非常奇怪的结果。最后一个断言失败:
public class CircularRefTest {
@Test
public void testCircularReferences() {
A a = new A();
a.setName("a");
B b = new B();
b.setName("b");
a.setB(b);
Container<A> container = new Container<A>(a, "container");
Schema<Container> schema = RuntimeSchema.getSchema(Container.class);
LinkedBuffer buffer = LinkedBuffer.allocate(256);
byte[] data = GraphIOUtil.toByteArray(container, schema, buffer);
Container<A> copy = new Container<A>();
GraphIOUtil.mergeFrom(data, copy, schema);
assertEquals(container.getName(), copy.getName());
assertEquals(container.getElement().getName(), copy.getElement().getName());
assertEquals(container.getElement().getB().getName(), copy.getElement().getB().getName());
// something weird happens here with the circular references here
System.out.println(copy.getElement().getB().getA().getClass());
assertTrue(copy.getElement().getB().getA() instanceof A); // fails
}
}
Protostuff 正在破坏从子类返回到父类的循环引用。最后一个断言应该通过,但由于某种原因,该类的类型为 Container。
我做错了什么?
如果我将 Container 类更改为使用强类型对象,则单元测试会通过。这是一个错误??
我使用的maven Artifact 是:
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-api</artifactId>
<version>1.0.4</version>
</dependency>
最佳答案
我非常仔细地查看了我项目中的完整依赖项列表,我发现了以下内容:
protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.2
protostuff-runtime: 1.0.2
protostuff-core: 1.0.2
1.0.2 jar 看起来很奇怪。然后我删除了所有依赖项。
然后我添加了以下依赖项:
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.4</version>
</dependency>
这会拉入以下 jar :
protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.4
protostuff-runtime: 1.0.4
protostuff-core: 1.0.4
然后我重新运行单元测试,一切正常!
只是为了仔细检查我对所有基于版本 1.0.2 的 jar 运行了单元测试,测试失败了。 看起来这个问题已经在 1.0.4 版本中修复了
关于java - 为什么在结合使用泛型和循环引用时 protostuff 会损坏对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9101464/