From 3def821cdbc7b7b093cf3ffbc7e9616fb0f3f463 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Wed, 22 Jul 2015 20:42:09 -0300 Subject: [PATCH 1/3] Fix Get Adapters in Proxy Class --- .../gson/GsonBuilderWrapper.java | 17 ++++++++++--- .../gson/GsonDeserializerTest.java | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java index 80645f94c..4ffc64b12 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java @@ -90,9 +90,20 @@ private void registerAdapter(Class adapterType, Object adapter) { } private Class getAdapterType(Object adapter) { - Type[] genericInterfaces = adapter.getClass().getGenericInterfaces(); - ParameterizedType type = (ParameterizedType) genericInterfaces[0]; - Type actualType = type.getActualTypeArguments()[0]; + final Class klazz; + if(adapter.getClass().getName().contains("$Proxy$")){ + final String[] split = adapter.getClass().getName().split("\\$Proxy\\$"); + try { + klazz = Class.forName(split[0]); + } catch (final ClassNotFoundException e) { + throw new RuntimeException(e); + } + }else{ + klazz = adapter.getClass(); + } + final Type[] genericInterfaces = klazz.getGenericInterfaces(); + final ParameterizedType type = (ParameterizedType) genericInterfaces[0]; + final Type actualType = type.getActualTypeArguments()[0]; if (actualType instanceof ParameterizedType) { return (Class) ((ParameterizedType) actualType).getRawType(); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java index 12cc29835..af7215ae1 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java @@ -169,6 +169,9 @@ public Dog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContex return dog; } } + + private class DogDeserializer$Proxy$$somentigin extends DogDeserializer implements JsonDeserializer { + } static class DogGenericController extends GenericController { @@ -278,6 +281,27 @@ public void shouldBeAbleToDeserializeADogWithDeserializerAdapter() throws Except assertThat(dog.age, is(25)); } + @Test + public void shouldBeAbleToDeserializeADogWithCdiProxyAdapter() throws Exception { + List> deserializers = new ArrayList<>(); + List> serializers = new ArrayList<>(); + deserializers.add(new DogDeserializer$Proxy$$somentigin()); + + builder = new GsonBuilderWrapper(new MockInstanceImpl<>(serializers), new MockInstanceImpl<>(deserializers), + new Serializee(new DefaultReflectionProvider()), new DefaultReflectionProvider()); + deserializer = new GsonDeserialization(builder, provider, request, container, deserializeeInstance); + + InputStream stream = asStream("{'dog':{'name':'Renan Reis','age':'0'}}"); + + Object[] deserialized = deserializer.deserialize(stream, dogParameter); + + assertThat(deserialized.length, is(1)); + assertThat(deserialized[0], is(instanceOf(Dog.class))); + Dog dog = (Dog) deserialized[0]; + assertThat(dog.name, is("Renan")); + assertThat(dog.age, is(25)); + } + @Test public void shouldBeAbleToDeserializeADogWhenMethodHasMoreThanOneArgument() throws Exception { InputStream stream = asStream("{'dog':{'name':'Brutus','age':7}}"); From 977c23d460ab27dbbe6173f8b890ab60051ddfd5 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Thu, 23 Jul 2015 09:04:39 -0300 Subject: [PATCH 2/3] Use CDIProxies#isCDIProxy to verify if proxy --- .../gson/GsonBuilderWrapper.java | 9 +-- .../gson/GsonBuilderWrapperTest.java | 56 +++++++++++++++++++ .../gson/GsonDeserializerTest.java | 39 +++---------- 3 files changed, 68 insertions(+), 36 deletions(-) create mode 100644 vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapperTest.java diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java index 4ffc64b12..5764abf44 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java @@ -15,6 +15,7 @@ */ package br.com.caelum.vraptor.serialization.gson; +import static br.com.caelum.vraptor.proxy.CDIProxies.isCDIProxy; import static java.util.Collections.singletonList; import java.lang.reflect.ParameterizedType; @@ -26,15 +27,15 @@ import javax.enterprise.inject.Instance; import javax.inject.Inject; -import br.com.caelum.vraptor.core.ReflectionProvider; -import br.com.caelum.vraptor.serialization.Serializee; - import com.google.gson.ExclusionStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializer; import com.google.gson.JsonSerializer; +import br.com.caelum.vraptor.core.ReflectionProvider; +import br.com.caelum.vraptor.serialization.Serializee; + /** * Builder Wrapper for JSON using GSON. * @@ -91,7 +92,7 @@ private void registerAdapter(Class adapterType, Object adapter) { private Class getAdapterType(Object adapter) { final Class klazz; - if(adapter.getClass().getName().contains("$Proxy$")){ + if(isCDIProxy(adapter.getClass())){ final String[] split = adapter.getClass().getName().split("\\$Proxy\\$"); try { klazz = Class.forName(split[0]); diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapperTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapperTest.java new file mode 100644 index 000000000..54c861799 --- /dev/null +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapperTest.java @@ -0,0 +1,56 @@ +package br.com.caelum.vraptor.serialization.gson; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Type; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import br.com.caelum.vraptor.WeldJunitRunner; + +@RunWith(WeldJunitRunner.class) +public class GsonBuilderWrapperTest { + private @Inject GsonBuilderWrapper builder; + private Gson gson; + + @Before + public void init(){ + gson = builder.create(); + } + + @Test + public void test() { + String json = gson.toJson(new Bean()); + assertEquals("{\"test123\":{}}", json); + } +} + +class Bean{ + +} + + +@RegisterStrategy(RegisterType.SINGLE) +@RequestScoped +class BeanSerializer implements JsonSerializer { + private static final JsonObject element = new JsonObject(); + static{ + element.add("test123", new JsonObject()); + } + + @Override + public JsonElement serialize(Bean src, Type typeOfSrc, JsonSerializationContext context) { + return element; + } +} \ No newline at end of file diff --git a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java index af7215ae1..0a5235d37 100644 --- a/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java +++ b/vraptor-core/src/test/java/br/com/caelum/vraptor/serialization/gson/GsonDeserializerTest.java @@ -40,8 +40,6 @@ import javax.enterprise.inject.Instance; import javax.servlet.http.HttpServletRequest; -import net.vidageek.mirror.dsl.Mirror; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -49,6 +47,12 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializer; + import br.com.caelum.vraptor.Consumes; import br.com.caelum.vraptor.controller.BeanClass; import br.com.caelum.vraptor.controller.ControllerMethod; @@ -62,12 +66,7 @@ import br.com.caelum.vraptor.serialization.Serializee; import br.com.caelum.vraptor.util.test.MockInstanceImpl; import br.com.caelum.vraptor.view.GenericController; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializer; +import net.vidageek.mirror.dsl.Mirror; public class GsonDeserializerTest { @@ -169,9 +168,6 @@ public Dog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContex return dog; } } - - private class DogDeserializer$Proxy$$somentigin extends DogDeserializer implements JsonDeserializer { - } static class DogGenericController extends GenericController { @@ -281,27 +277,6 @@ public void shouldBeAbleToDeserializeADogWithDeserializerAdapter() throws Except assertThat(dog.age, is(25)); } - @Test - public void shouldBeAbleToDeserializeADogWithCdiProxyAdapter() throws Exception { - List> deserializers = new ArrayList<>(); - List> serializers = new ArrayList<>(); - deserializers.add(new DogDeserializer$Proxy$$somentigin()); - - builder = new GsonBuilderWrapper(new MockInstanceImpl<>(serializers), new MockInstanceImpl<>(deserializers), - new Serializee(new DefaultReflectionProvider()), new DefaultReflectionProvider()); - deserializer = new GsonDeserialization(builder, provider, request, container, deserializeeInstance); - - InputStream stream = asStream("{'dog':{'name':'Renan Reis','age':'0'}}"); - - Object[] deserialized = deserializer.deserialize(stream, dogParameter); - - assertThat(deserialized.length, is(1)); - assertThat(deserialized[0], is(instanceOf(Dog.class))); - Dog dog = (Dog) deserialized[0]; - assertThat(dog.name, is("Renan")); - assertThat(dog.age, is(25)); - } - @Test public void shouldBeAbleToDeserializeADogWhenMethodHasMoreThanOneArgument() throws Exception { InputStream stream = asStream("{'dog':{'name':'Brutus','age':7}}"); From 5409dba1a5bb9237b62048d7163275fa259b13c5 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Thu, 23 Jul 2015 09:54:18 -0300 Subject: [PATCH 3/3] Use CDIProxies.extractRawTypeIfPossible to retrieve original class in CDI proxy --- .../serialization/gson/GsonBuilderWrapper.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java index 5764abf44..474d88a50 100644 --- a/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java +++ b/vraptor-core/src/main/java/br/com/caelum/vraptor/serialization/gson/GsonBuilderWrapper.java @@ -15,7 +15,7 @@ */ package br.com.caelum.vraptor.serialization.gson; -import static br.com.caelum.vraptor.proxy.CDIProxies.isCDIProxy; +import static br.com.caelum.vraptor.proxy.CDIProxies.extractRawTypeIfPossible; import static java.util.Collections.singletonList; import java.lang.reflect.ParameterizedType; @@ -91,17 +91,7 @@ private void registerAdapter(Class adapterType, Object adapter) { } private Class getAdapterType(Object adapter) { - final Class klazz; - if(isCDIProxy(adapter.getClass())){ - final String[] split = adapter.getClass().getName().split("\\$Proxy\\$"); - try { - klazz = Class.forName(split[0]); - } catch (final ClassNotFoundException e) { - throw new RuntimeException(e); - } - }else{ - klazz = adapter.getClass(); - } + final Class klazz = extractRawTypeIfPossible(adapter.getClass()); final Type[] genericInterfaces = klazz.getGenericInterfaces(); final ParameterizedType type = (ParameterizedType) genericInterfaces[0]; final Type actualType = type.getActualTypeArguments()[0];