chpooo / kryo

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

Does not handle MethodHandle #107

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

can Kryo handle MethodHandle objects? Or how could I get that to work - I can't 
write my own serializer since I don't know the internals of MethodHandle, or?

The following program gives the error message below:

import java.io.FileOutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import org.objenesis.strategy.StdInstantiatorStrategy;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;

public class Main {

    public static void main(String[] args) throws Exception {
        Kryo kryo = new Kryo();
        kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
        kryo.register(MethodHandle.class);

        Output out = new Output(new FileOutputStream("d.dat"));
        kryo.writeObject(out,  new T1());
        out.close();            
    }

    public static class T1 {
        MethodHandle a;
        int b = 4;

        public T1() {
            try {
                a = MethodHandles.lookup().unreflectGetter(getClass().getDeclaredField("b")).bindTo(this);
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
    }

}

Exception in thread "main" com.esotericsoftware.kryo.KryoException: 
java.lang.NullPointerException
Serialization trace:
vmtarget (java.lang.invoke.DirectMethodHandle)
vmtarget (java.lang.invoke.AdapterMethodHandle)
vmtarget (java.lang.invoke.MethodHandleImpl$FieldAccessor)
vmtarget (java.lang.invoke.BoundMethodHandle)
a (Main$T1)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:585)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:501)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:501)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:501)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:501)
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:564)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:213)
    at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:486)
    at Main.main(Main.java:18)
Caused by: java.lang.NullPointerException
    at com.esotericsoftware.kryo.serializers.FieldSerializer$ObjectField.write(FieldSerializer.java:562)
    ... 15 more

Thanks,

Original issue reported on code.google.com by jakobsem...@gmail.com on 9 Apr 2013 at 2:55

GoogleCodeExporter commented 8 years ago
You should be able to write a serializer using the public API for method 
handles. Eg, the class, strings, etc.

Original comment by nathan.s...@gmail.com on 11 Apr 2013 at 4:33