I have something that you may want to pick up and run with.
im using gwt autobeans and had to figure out a way to make gson work around incompatible proxies on the server side. what works is to use groovy to be the enclosing classloader during TypeAdapter registration and for groovy to copmile the adapter class and register it. the generation code can be cleaned up and some references to my gson singletions exist here. i think the generated fromJson method is now superfluous as well. this is the only groovy in my project, fwiw, using pure java syntax, so cleaner solutions exist with bytecode engineering, but this one was an easy sample to plug in.
I't been too long since i had time to develop new features in Jason and i feel like this is the day, i will look into this issue after work and post some feedback. Thanks for the tip!
I have something that you may want to pick up and run with.
im using gwt autobeans and had to figure out a way to make gson work around incompatible proxies on the server side. what works is to use groovy to be the enclosing classloader during TypeAdapter registration and for groovy to copmile the adapter class and register it. the generation code can be cleaned up and some references to my gson singletions exist here. i think the generated fromJson method is now superfluous as well. this is the only groovy in my project, fwiw, using pure java syntax, so cleaner solutions exist with bytecode engineering, but this one was an easy sample to plug in.
cheers.
import com.google.gson.GsonBuilder; import com.google.gson.InstanceCreator; import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; import groovy.lang.GroovyClassLoader; import org.apache.commons.beanutils.PropertyUtils;
import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.LinkedHashMap; import java.util.Map;
public class GroovyGsonShimFactory { private Map<Class, Method> shimMethods = new LinkedHashMap<>();
private void generateGroovyProxy(Class ifaceClass) { String shimClassName = ifaceClass.getSimpleName() + "$Proxy"; String ifaceClassCanonicalName = ifaceClass.getCanonicalName(); String s = "import com.google.gson.;\n" + "import org.apache.commons.beanutils.BeanUtils;\n" + "import java.lang.reflect.;\n" + "import java.util.*;\n\n" + "public class "+shimClassName+" implements "+ifaceClassCanonicalName+" {\n" ;
}
public T getShim(String json, Class ifaceClass) {
if (!shimMethods.containsKey(ifaceClass))
generateGroovyProxy(ifaceClass);
T shim = null;//= gson().shimMethods(json, CowSchema.class);
try {
shim = (T) shimMethods.get(ifaceClass).invoke(null, json);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return shim;
}
}