From db7ba746910703a86abaaa579c64349184916b44 Mon Sep 17 00:00:00 2001 From: "m.ross" Date: Sat, 17 Nov 2018 13:40:49 -0500 Subject: [PATCH] Proxy serializer --- .../serializers/FSTProxySerializer.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java new file mode 100644 index 00000000..99318d32 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTProxySerializer.java @@ -0,0 +1,48 @@ +package org.nustaq.serialization.serializers; + + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +import java.io.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; + + +public class FSTProxySerializer extends FSTBasicObjectSerializer { + + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { + Class[] ifaces = clzInfo.getClazz().getInterfaces(); + ClassLoader cl = out.getConf().getClassLoader(); + out.writeInt(ifaces.length); + for (Class i : ifaces) + out.writeUTF(i.getName()); + out.writeObject(Proxy.getInvocationHandler(toWrite)); + } + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo referencee, int streamPositioin) throws IOException, ClassNotFoundException { + ClassLoader cl = in.getConf().getClassLoader(); + int numIfaces = in.readInt(); + String[] interfaces = new String[numIfaces]; + for (int i = 0; i < numIfaces; i++) { + interfaces[i] = in.readUTF(); + } + Class[] classObjs = new Class[interfaces.length]; + + for(int i = 0; i < interfaces.length; ++i) { + try { + classObjs[i] = Class.forName(interfaces[i], false, cl); + } catch (ClassNotFoundException e) { + classObjs[i] = Class.forName(interfaces[i], false, this.getClass().getClassLoader()); + } + } + InvocationHandler ih = (InvocationHandler)in.readObject(); + Object res = Proxy.newProxyInstance(in.getConf().getClassLoader(),classObjs,ih); + in.registerObject(res,streamPositioin,serializationInfo,referencee); + return res; + } +} \ No newline at end of file