From 8567fe6c101f2472662b9362b880a55fc3dcca15 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 1 Oct 2009 01:37:57 +0000 Subject: [PATCH] Implementing code review comment from r428: Using Polymorphism for exclusion strategies for expose annotations. --- ...ationDeserializationExclusionStrategy.java | 41 ++++++++++ ...tationSerializationExclusionStrategy.java} | 22 +----- .../java/com/google/gson/GsonBuilder.java | 9 +-- .../com/google/gson/JsonPrintFormatter.java | 1 - ...nDeserializationExclusionStrategyTest.java | 76 +++++++++++++++++++ ...onSerializationExclusionStrategyTest.java} | 30 ++++++-- 6 files changed, 147 insertions(+), 32 deletions(-) create mode 100644 gson/src/main/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategy.java rename gson/src/main/java/com/google/gson/{ExposeAnnotationBasedExclusionStrategy.java => ExposeAnnotationSerializationExclusionStrategy.java} (69%) create mode 100644 gson/src/test/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategyTest.java rename gson/src/test/java/com/google/gson/{ExposeAnnotationBasedExclusionStrategyTest.java => ExposeAnnotationSerializationExclusionStrategyTest.java} (63%) diff --git a/gson/src/main/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategy.java b/gson/src/main/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategy.java new file mode 100644 index 00000000..b54b642d --- /dev/null +++ b/gson/src/main/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategy.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson; + +import java.lang.reflect.Field; + +import com.google.gson.annotations.Expose; + +/** + * Excludes fields that do not have the {@link Expose} annotation + * + * @author Inderjeet Singh + * @author Joel Leitch + */ +final class ExposeAnnotationDeserializationExclusionStrategy implements ExclusionStrategy { + + public boolean shouldSkipClass(Class clazz) { + return false; + } + + public boolean shouldSkipField(Field f) { + Expose annotation = f.getAnnotation(Expose.class); + if (annotation == null) { + return true; + } + return !annotation.deserialize(); + } +} diff --git a/gson/src/main/java/com/google/gson/ExposeAnnotationBasedExclusionStrategy.java b/gson/src/main/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategy.java similarity index 69% rename from gson/src/main/java/com/google/gson/ExposeAnnotationBasedExclusionStrategy.java rename to gson/src/main/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategy.java index 58e54cdc..f8d68c00 100644 --- a/gson/src/main/java/com/google/gson/ExposeAnnotationBasedExclusionStrategy.java +++ b/gson/src/main/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategy.java @@ -24,18 +24,9 @@ import java.lang.reflect.Field; * Excludes fields that do not have the {@link Expose} annotation * * @author Inderjeet Singh + * @author Joel Leitch */ -class ExposeAnnotationBasedExclusionStrategy implements ExclusionStrategy { - - enum Phase { - SERIALIZATION, DESERIALIZATION - } - - private final Phase phase; - - public ExposeAnnotationBasedExclusionStrategy(Phase phase) { - this.phase = phase; - } +final class ExposeAnnotationSerializationExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipClass(Class clazz) { return false; @@ -46,13 +37,6 @@ class ExposeAnnotationBasedExclusionStrategy implements ExclusionStrategy { if (annotation == null) { return true; } - switch (phase) { - case SERIALIZATION: - return !annotation.serialize(); - case DESERIALIZATION: - return !annotation.deserialize(); - default: - throw new IllegalStateException(); - } + return !annotation.serialize(); } } diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 0a10b53c..fd01a16f 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -23,7 +23,6 @@ import java.util.LinkedList; import java.util.List; import com.google.gson.DefaultTypeAdapters.DefaultDateTypeAdapter; -import com.google.gson.ExposeAnnotationBasedExclusionStrategy.Phase; /** *

Use this builder to construct a {@link Gson} instance when you need to set configuration @@ -55,12 +54,12 @@ public final class GsonBuilder { new AnonymousAndLocalClassExclusionStrategy(); private static final InnerClassExclusionStrategy innerClassExclusionStrategy = new InnerClassExclusionStrategy(); - private static final ExposeAnnotationBasedExclusionStrategy + private static final ExposeAnnotationSerializationExclusionStrategy exposeAnnotationSerializationExclusionStrategy = - new ExposeAnnotationBasedExclusionStrategy(Phase.SERIALIZATION); - private static final ExposeAnnotationBasedExclusionStrategy + new ExposeAnnotationSerializationExclusionStrategy(); + private static final ExposeAnnotationDeserializationExclusionStrategy exposeAnnotationDeserializationExclusionStrategy = - new ExposeAnnotationBasedExclusionStrategy(Phase.DESERIALIZATION); + new ExposeAnnotationDeserializationExclusionStrategy(); private double ignoreVersionsAfter; private ModifierBasedExclusionStrategy modifierBasedExclusionStrategy; diff --git a/gson/src/main/java/com/google/gson/JsonPrintFormatter.java b/gson/src/main/java/com/google/gson/JsonPrintFormatter.java index c5054212..f9f00656 100644 --- a/gson/src/main/java/com/google/gson/JsonPrintFormatter.java +++ b/gson/src/main/java/com/google/gson/JsonPrintFormatter.java @@ -17,7 +17,6 @@ package com.google.gson; import java.io.IOException; -import java.util.Stack; /** * Formats Json in a nicely indented way with a specified print margin. diff --git a/gson/src/test/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategyTest.java b/gson/src/test/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategyTest.java new file mode 100644 index 00000000..9dfcb13f --- /dev/null +++ b/gson/src/test/java/com/google/gson/ExposeAnnotationDeserializationExclusionStrategyTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +import java.lang.reflect.Field; + +import junit.framework.TestCase; + +import com.google.gson.annotations.Expose; + +/** + * Unit tests for the {@link ExposeAnnotationDeserializationExclusionStrategy} class. + * + * @author Joel Leitch + */ +public class ExposeAnnotationDeserializationExclusionStrategyTest extends TestCase { + private ExposeAnnotationDeserializationExclusionStrategy strategy; + + @Override + protected void setUp() throws Exception { + super.setUp(); + strategy = new ExposeAnnotationDeserializationExclusionStrategy(); + } + + public void testNeverSkipClasses() throws Exception { + assertFalse(strategy.shouldSkipClass(MockObject.class)); + } + + public void testSkipNonAnnotatedFields() throws Exception { + Field f = MockObject.class.getField("hiddenField"); + assertTrue(strategy.shouldSkipField(f)); + } + + public void testSkipExplicitlySkippedFields() throws Exception { + Field f = MockObject.class.getField("explicitlyHiddenField"); + assertTrue(strategy.shouldSkipField(f)); + } + + public void testNeverSkipExposedAnnotatedFields() throws Exception { + Field f = MockObject.class.getField("exposedField"); + assertFalse(strategy.shouldSkipField(f)); + } + + public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception { + Field f = MockObject.class.getField("explicitlyExposedField"); + assertFalse(strategy.shouldSkipField(f)); + } + + @SuppressWarnings("unused") + private static class MockObject { + @Expose + public final int exposedField = 0; + + @Expose(deserialize=true) + public final int explicitlyExposedField = 0; + + @Expose(deserialize=false) + public final int explicitlyHiddenField = 0; + + public final int hiddenField = 0; + } +} diff --git a/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java b/gson/src/test/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategyTest.java similarity index 63% rename from gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java rename to gson/src/test/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategyTest.java index 1f63f95e..a07512e5 100644 --- a/gson/src/test/java/com/google/gson/ExposeAnnotationBasedExclusionStrategyTest.java +++ b/gson/src/test/java/com/google/gson/ExposeAnnotationSerializationExclusionStrategyTest.java @@ -16,25 +16,24 @@ package com.google.gson; -import com.google.gson.ExposeAnnotationBasedExclusionStrategy.Phase; -import com.google.gson.annotations.Expose; +import java.lang.reflect.Field; import junit.framework.TestCase; -import java.lang.reflect.Field; +import com.google.gson.annotations.Expose; /** - * Unit tests for the {@link ExposeAnnotationBasedExclusionStrategy} class. + * Unit tests for the {@link ExposeAnnotationSerializationExclusionStrategy} class. * * @author Joel Leitch */ -public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase { - private ExposeAnnotationBasedExclusionStrategy strategy; +public class ExposeAnnotationSerializationExclusionStrategyTest extends TestCase { + private ExposeAnnotationSerializationExclusionStrategy strategy; @Override protected void setUp() throws Exception { super.setUp(); - strategy = new ExposeAnnotationBasedExclusionStrategy(Phase.SERIALIZATION); + strategy = new ExposeAnnotationSerializationExclusionStrategy(); } public void testNeverSkipClasses() throws Exception { @@ -46,15 +45,32 @@ public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase { assertTrue(strategy.shouldSkipField(f)); } + public void testSkipExplicitlySkippedFields() throws Exception { + Field f = MockObject.class.getField("explicitlyHiddenField"); + assertTrue(strategy.shouldSkipField(f)); + } + public void testNeverSkipExposedAnnotatedFields() throws Exception { Field f = MockObject.class.getField("exposedField"); assertFalse(strategy.shouldSkipField(f)); } + public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception { + Field f = MockObject.class.getField("explicitlyExposedField"); + assertFalse(strategy.shouldSkipField(f)); + } + @SuppressWarnings("unused") private static class MockObject { @Expose public final int exposedField = 0; + + @Expose(serialize=true) + public final int explicitlyExposedField = 0; + + @Expose(serialize=false) + public final int explicitlyHiddenField = 0; + public final int hiddenField = 0; } }