Implementing code review comment from r428: Using Polymorphism for exclusion strategies for expose annotations.

This commit is contained in:
Inderjeet Singh 2009-10-01 01:37:57 +00:00
parent 77c2c29316
commit 8567fe6c10
6 changed files with 147 additions and 32 deletions

View File

@ -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();
}
}

View File

@ -24,18 +24,9 @@ import java.lang.reflect.Field;
* Excludes fields that do not have the {@link Expose} annotation * Excludes fields that do not have the {@link Expose} annotation
* *
* @author Inderjeet Singh * @author Inderjeet Singh
* @author Joel Leitch
*/ */
class ExposeAnnotationBasedExclusionStrategy implements ExclusionStrategy { final class ExposeAnnotationSerializationExclusionStrategy implements ExclusionStrategy {
enum Phase {
SERIALIZATION, DESERIALIZATION
}
private final Phase phase;
public ExposeAnnotationBasedExclusionStrategy(Phase phase) {
this.phase = phase;
}
public boolean shouldSkipClass(Class<?> clazz) { public boolean shouldSkipClass(Class<?> clazz) {
return false; return false;
@ -46,13 +37,6 @@ class ExposeAnnotationBasedExclusionStrategy implements ExclusionStrategy {
if (annotation == null) { if (annotation == null) {
return true; return true;
} }
switch (phase) { return !annotation.serialize();
case SERIALIZATION:
return !annotation.serialize();
case DESERIALIZATION:
return !annotation.deserialize();
default:
throw new IllegalStateException();
}
} }
} }

View File

@ -23,7 +23,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import com.google.gson.DefaultTypeAdapters.DefaultDateTypeAdapter; import com.google.gson.DefaultTypeAdapters.DefaultDateTypeAdapter;
import com.google.gson.ExposeAnnotationBasedExclusionStrategy.Phase;
/** /**
* <p>Use this builder to construct a {@link Gson} instance when you need to set configuration * <p>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(); new AnonymousAndLocalClassExclusionStrategy();
private static final InnerClassExclusionStrategy innerClassExclusionStrategy = private static final InnerClassExclusionStrategy innerClassExclusionStrategy =
new InnerClassExclusionStrategy(); new InnerClassExclusionStrategy();
private static final ExposeAnnotationBasedExclusionStrategy private static final ExposeAnnotationSerializationExclusionStrategy
exposeAnnotationSerializationExclusionStrategy = exposeAnnotationSerializationExclusionStrategy =
new ExposeAnnotationBasedExclusionStrategy(Phase.SERIALIZATION); new ExposeAnnotationSerializationExclusionStrategy();
private static final ExposeAnnotationBasedExclusionStrategy private static final ExposeAnnotationDeserializationExclusionStrategy
exposeAnnotationDeserializationExclusionStrategy = exposeAnnotationDeserializationExclusionStrategy =
new ExposeAnnotationBasedExclusionStrategy(Phase.DESERIALIZATION); new ExposeAnnotationDeserializationExclusionStrategy();
private double ignoreVersionsAfter; private double ignoreVersionsAfter;
private ModifierBasedExclusionStrategy modifierBasedExclusionStrategy; private ModifierBasedExclusionStrategy modifierBasedExclusionStrategy;

View File

@ -17,7 +17,6 @@
package com.google.gson; package com.google.gson;
import java.io.IOException; import java.io.IOException;
import java.util.Stack;
/** /**
* Formats Json in a nicely indented way with a specified print margin. * Formats Json in a nicely indented way with a specified print margin.

View File

@ -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;
}
}

View File

@ -16,25 +16,24 @@
package com.google.gson; package com.google.gson;
import com.google.gson.ExposeAnnotationBasedExclusionStrategy.Phase; import java.lang.reflect.Field;
import com.google.gson.annotations.Expose;
import junit.framework.TestCase; 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 * @author Joel Leitch
*/ */
public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase { public class ExposeAnnotationSerializationExclusionStrategyTest extends TestCase {
private ExposeAnnotationBasedExclusionStrategy strategy; private ExposeAnnotationSerializationExclusionStrategy strategy;
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
strategy = new ExposeAnnotationBasedExclusionStrategy(Phase.SERIALIZATION); strategy = new ExposeAnnotationSerializationExclusionStrategy();
} }
public void testNeverSkipClasses() throws Exception { public void testNeverSkipClasses() throws Exception {
@ -46,15 +45,32 @@ public class ExposeAnnotationBasedExclusionStrategyTest extends TestCase {
assertTrue(strategy.shouldSkipField(f)); assertTrue(strategy.shouldSkipField(f));
} }
public void testSkipExplicitlySkippedFields() throws Exception {
Field f = MockObject.class.getField("explicitlyHiddenField");
assertTrue(strategy.shouldSkipField(f));
}
public void testNeverSkipExposedAnnotatedFields() throws Exception { public void testNeverSkipExposedAnnotatedFields() throws Exception {
Field f = MockObject.class.getField("exposedField"); Field f = MockObject.class.getField("exposedField");
assertFalse(strategy.shouldSkipField(f)); assertFalse(strategy.shouldSkipField(f));
} }
public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception {
Field f = MockObject.class.getField("explicitlyExposedField");
assertFalse(strategy.shouldSkipField(f));
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class MockObject { private static class MockObject {
@Expose @Expose
public final int exposedField = 0; 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; public final int hiddenField = 0;
} }
} }