Candyjing1024 / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

deep copy of ArrayAsListSerializer: Caused by: java.lang.NullPointerException #135

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I used ArrayAsListSerializer from Martin's project kryo-serializers to do 
deep clone of objects.
However, copy method is not implemented.
I tried to implemented copy like that:
        protected List<?> createCopy(Kryo kryo, List<?> original) {
        return kryo.newInstance(original.getClass());
    }

        @Override
    public List copy(Kryo kryo, List original) {
        List copy = createCopy(kryo, original);
        for (Object elem : original) {
            copy.add(kryo.copy(elem));
        }
        return copy;
    }

What is the expected output? 
But I receive the following error:
Caused by: java.lang.NullPointerException
    at java.util.Arrays$ArrayList.size(Arrays.java:3362)
    at java.util.AbstractList.add(AbstractList.java:91)
    at pt.ptinovacao.deepcloner.kryo.ArraysAsListSerializer.copy(ArraysAsListSerializer.java:127)
    at pt.ptinovacao.deepcloner.kryo.ArraysAsListSerializer.copy(ArraysAsListSerializer.java:1)
    at com.esotericsoftware.kryo.Kryo.copy(Kryo.java:830)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.copy(FieldSerializer.java:638)
    ... 28 more

What do you see instead?
Successful copy of object.

What version of the Kryo are you using?
2.21

PS: This new discussion is started on this issue:
https://code.google.com/p/kryo/issues/detail?id=134

Original issue reported on code.google.com by agentilr...@gmail.com on 27 Sep 2013 at 11:26

GoogleCodeExporter commented 8 years ago
I added copy methods to all serializers of the kryo-serializers addon, and 
pushed kryo-serializers-0.24 to sonatype. In some hours it should be in maven 
central.

Original comment by martin.grotzke on 29 Sep 2013 at 10:58

GoogleCodeExporter commented 8 years ago
Hi Martin,

I saw your change into ArrayAsListSerializer but I didn't found copy method.
https://github.com/magro/kryo-serializers/blob/master/src/main/java/de/javakaffe
e/kryoserializers/ArraysAsListSerializer.java

I saw a comment on default constructor, when you set class to immutable:
public ArraysAsListSerializer() {
        try {
            _arrayField = Class.forName( "java.util.Arrays$ArrayList" ).getDeclaredField( "a" );
            _arrayField.setAccessible( true );
        } catch ( final Exception e ) {
            throw new RuntimeException( e );
        }
        // Immutable causes #copy(obj) to return the original object
        setImmutable(true);
    }

So, copy method don't returns a copy of object, but the original.
You will implement the copy method?

Thanks.

Original comment by agentilr...@gmail.com on 3 Oct 2013 at 12:02