From 656da80dd38cf01e79d315a065d32ff18e5917a0 Mon Sep 17 00:00:00 2001 From: Nicolas Morel Date: Fri, 8 Aug 2014 17:12:39 +0200 Subject: [PATCH] Add ability to serialize anonymous class and proxy --- .../ser/bean/AbstractBeanJsonSerializer.java | 6 +- .../rebind/BeanJsonDeserializerCreator.java | 3 +- .../property/processor/PropertyProcessor.java | 4 - .../client/GwtJacksonTestSuite.java | 2 + ...AndAnonymousClassSerializationGwtTest.java | 116 ++++++++++++++++++ .../nmorel/gwtjackson/GwtJacksonTest.gwt.xml | 1 + 6 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/advanced/ProxyAndAnonymousClassSerializationGwtTest.java diff --git a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/client/ser/bean/AbstractBeanJsonSerializer.java b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/client/ser/bean/AbstractBeanJsonSerializer.java index 231d8ee1..65576a52 100644 --- a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/client/ser/bean/AbstractBeanJsonSerializer.java +++ b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/client/ser/bean/AbstractBeanJsonSerializer.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.Map; import java.util.Set; +import java.util.logging.Level; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -93,7 +94,10 @@ private InternalSerializer getSerializer( JsonWriter writer, T value, JsonSer } SubtypeSerializer subtypeSerializer = subtypeClassToSerializer.get( value.getClass() ); if ( null == subtypeSerializer ) { - throw ctx.traceError( value, "Cannot find serializer for class " + value.getClass(), writer ); + if (ctx.getLogger().isLoggable( Level.FINE)) { + ctx.getLogger().fine("Cannot find serializer for class " + value.getClass() + ". Fallback to the serializer of " + getSerializedType()); + } + return this; } return subtypeSerializer; } diff --git a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/BeanJsonDeserializerCreator.java b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/BeanJsonDeserializerCreator.java index 9a14ec7e..4fd869b2 100644 --- a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/BeanJsonDeserializerCreator.java +++ b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/BeanJsonDeserializerCreator.java @@ -114,7 +114,8 @@ protected void writeClassBody( SourceWriter source, BeanInfo beanInfo, Immutable source.println(); } - if ( !properties.isEmpty() ) { + // no need to generate properties for non instantiable class + if ( !properties.isEmpty() && beanInfo.getCreatorMethod().isPresent() ) { generateInitPropertiesMethods( source, beanInfo, properties ); source.println(); } diff --git a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/property/processor/PropertyProcessor.java b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/property/processor/PropertyProcessor.java index 2ab2df09..5fbc8c71 100644 --- a/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/property/processor/PropertyProcessor.java +++ b/gwt-jackson/src/main/java/com/github/nmorel/gwtjackson/rebind/property/processor/PropertyProcessor.java @@ -68,10 +68,6 @@ public final class PropertyProcessor { public static ImmutableMap findAllProperties( RebindConfiguration configuration, TreeLogger logger, JacksonTypeOracle typeOracle, BeanInfo beanInfo ) throws UnableToCompleteException { - if ( null != beanInfo.getType().isInterface() || beanInfo.getType().isAbstract() ) { - // no properties on interface and abstract class - return ImmutableMap.of(); - } // we first parse the bean to retrieve all the properties ImmutableMap fieldsMap = PropertyParser.findPropertyAccessors( configuration, logger, beanInfo ); diff --git a/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/GwtJacksonTestSuite.java b/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/GwtJacksonTestSuite.java index aa111742..67cff9f2 100644 --- a/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/GwtJacksonTestSuite.java +++ b/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/GwtJacksonTestSuite.java @@ -20,6 +20,7 @@ import com.github.nmorel.gwtjackson.client.advanced.GenericsGwtTest; import com.github.nmorel.gwtjackson.client.advanced.InheritanceGwtTest; import com.github.nmorel.gwtjackson.client.advanced.PrivateAccessGwtTest; +import com.github.nmorel.gwtjackson.client.advanced.ProxyAndAnonymousClassSerializationGwtTest; import com.github.nmorel.gwtjackson.client.advanced.WildcardGwtTest; import com.github.nmorel.gwtjackson.client.advanced.identity.ObjectIdDeserializationGwtTest; import com.github.nmorel.gwtjackson.client.advanced.identity.ObjectIdGwtTest; @@ -272,6 +273,7 @@ public static Test suite() { suite.addTestSuite( JsonFormatGwtTest.class ); // Advanced use cases + suite.addTestSuite( ProxyAndAnonymousClassSerializationGwtTest.class ); suite.addTestSuite( PrivateAccessGwtTest.class ); suite.addTestSuite( InheritanceGwtTest.class ); suite.addTestSuite( GenericsGwtTest.class ); diff --git a/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/advanced/ProxyAndAnonymousClassSerializationGwtTest.java b/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/advanced/ProxyAndAnonymousClassSerializationGwtTest.java new file mode 100644 index 00000000..8bace81a --- /dev/null +++ b/gwt-jackson/src/test/java/com/github/nmorel/gwtjackson/client/advanced/ProxyAndAnonymousClassSerializationGwtTest.java @@ -0,0 +1,116 @@ +package com.github.nmorel.gwtjackson.client.advanced; + +import com.github.nmorel.gwtjackson.client.GwtJacksonTestCase; +import com.github.nmorel.gwtjackson.client.ObjectWriter; +import com.google.gwt.core.client.GWT; +import com.google.gwt.event.shared.SimpleEventBus; +import com.google.web.bindery.requestfactory.shared.ProxyFor; +import com.google.web.bindery.requestfactory.shared.Request; +import com.google.web.bindery.requestfactory.shared.RequestContext; +import com.google.web.bindery.requestfactory.shared.RequestFactory; +import com.google.web.bindery.requestfactory.shared.Service; +import com.google.web.bindery.requestfactory.shared.ValueProxy; + +/** + */ +public class ProxyAndAnonymousClassSerializationGwtTest extends GwtJacksonTestCase { + + // RequestFactory proxy + + public static class RfBean { + + private int id; + + private String name; + + public int getId() { + return id; + } + + public void setId( int id ) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName( String name ) { + this.name = name; + } + } + + @ProxyFor( RfBean.class ) + public static interface RfBeanProxy extends ValueProxy { + + int getId(); + + void setId( int id ); + + String getName(); + + void setName( String name ); + } + + public static class RfBeanService { + + public void save( RfBean bean ) { + + } + } + + @Service( RfBeanService.class ) + public static interface ServiceContext extends RequestContext { + + Request save( RfBeanProxy bean ); + } + + public static interface TestRequestFactory extends RequestFactory { + + ServiceContext serviceContext(); + } + + public static interface RfBeanProxyWriter extends ObjectWriter {} + + public void testSerializationProxy() { + TestRequestFactory requestFactory = GWT.create( TestRequestFactory.class ); + requestFactory.initialize( new SimpleEventBus() ); + + ServiceContext serviceContext = requestFactory.serviceContext(); + RfBeanProxy proxy = serviceContext.create( RfBeanProxy.class ); + proxy.setId( 54 ); + proxy.setName( "Toto" ); + + RfBeanProxyWriter writer = GWT.create( RfBeanProxyWriter.class ); + String json = writer.write( proxy ); + assertEquals( "{\"id\":54,\"name\":\"Toto\"}", json ); + } + + //####### Anonymous class + + public static abstract class AbstractBean { + + public abstract int getId(); + + public abstract String getName(); + } + + public static interface AbstractBeanWriter extends ObjectWriter {} + + public void testSerializationAnonymousClass() { + AbstractBeanWriter writer = GWT.create( AbstractBeanWriter.class ); + String json = writer.write( new AbstractBean() { + @Override + public int getId() { + return 54; + } + + @Override + public String getName() { + return "Toto"; + } + } ); + assertEquals( "{\"id\":54,\"name\":\"Toto\"}", json ); + } + +} diff --git a/gwt-jackson/src/test/resources/com/github/nmorel/gwtjackson/GwtJacksonTest.gwt.xml b/gwt-jackson/src/test/resources/com/github/nmorel/gwtjackson/GwtJacksonTest.gwt.xml index c5e3842d..47674c9c 100644 --- a/gwt-jackson/src/test/resources/com/github/nmorel/gwtjackson/GwtJacksonTest.gwt.xml +++ b/gwt-jackson/src/test/resources/com/github/nmorel/gwtjackson/GwtJacksonTest.gwt.xml @@ -17,6 +17,7 @@ +