为什么当使用泛型和循环引用时,protostuff 会破坏对象?

发布于 2025-01-01 06:58:11 字数 3106 浏览 3 评论 0原文

我正在使用带有循环引用和泛型的 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 神器是:

<dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-api</artifactId>
        <version>1.0.4</version>
    </dependency>

I am using protostuff binary with circular references and generics. As a very simplistic scenario i have the following classes:

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

When i run the following unit test to check that the round trip serialisation / deserialisation has executed correctly i get a very odd result. the last assertion fails:

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 is corrupting the circular references from the child class back up to the parent. The last assertion should pass, but for some reason the class is of type Container.

What am i doing wrong ?

If i change the Container class to use strongly typed objects then the unit test passes. Is this a bug ??

The maven artifact i am using is:

<dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-api</artifactId>
        <version>1.0.4</version>
    </dependency>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2025-01-08 06:58:11

我非常仔细地查看了项目中依赖项的完整列表,发现了以下内容:

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版本已经修复了

I looked very carefully through the full list of dependencies in my project, i found the following:

protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.2
protostuff-runtime: 1.0.2
protostuff-core: 1.0.2

I the 1.0.2 jars looked odd. I then deleted all the dependencies.

I then added the following dependencies:

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

This pulled in the following jars:

protostuff-api: 1.0.4
protostuff-collectionsschema: 1.0.4
protostuff-runtime: 1.0.4
protostuff-core: 1.0.4

I then re ran the unit test & everything worked !

Just to double check i ran the unit test with all the jars based on version 1.0.2, the test fails.
It looks like this problem has been fixed in version 1.0.4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文