Implementing code review comment from r428: Using Polymorphism for exclusion strategies for expose annotations.
This commit is contained in:
parent
77c2c29316
commit
8567fe6c10
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -23,7 +23,6 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
@ -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;
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user