From e37add7836afe3768f1e517eed3ffbf440bff6bb Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 9 Oct 2009 01:35:45 +0000 Subject: [PATCH] Implemented some code review comments from r482 --- .../com/google/gson/ExclusionStrategy.java | 4 ++-- .../java/com/google/gson/FieldAttributes.java | 2 +- gson/src/main/java/com/google/gson/Gson.java | 24 +++++-------------- .../gson/JsonDeserializationVisitor.java | 4 ---- .../gson/JsonSerializationContextDefault.java | 2 +- .../google/gson/JsonSerializationVisitor.java | 4 ---- .../java/com/google/gson/ObjectNavigator.java | 4 +--- .../java/com/google/gson/ObjectTypePair.java | 8 ++----- .../com/google/gson/FieldAttributesTest.java | 1 + 9 files changed, 14 insertions(+), 39 deletions(-) diff --git a/gson/src/main/java/com/google/gson/ExclusionStrategy.java b/gson/src/main/java/com/google/gson/ExclusionStrategy.java index d2f90b24..9ea4ab8e 100644 --- a/gson/src/main/java/com/google/gson/ExclusionStrategy.java +++ b/gson/src/main/java/com/google/gson/ExclusionStrategy.java @@ -19,8 +19,8 @@ package com.google.gson; /** * A strategy (or policy) definition that is used to decide whether or not a field or top-level * class should be serialized or deserialized as part of the JSON output/input. For serialization, - * if the {@link this#shouldSkipClass(Class)} method returns false then that class or field type - * will not be part of the JSON output. For deserialization, if {@link this#shouldSkipClass(Class)} + * if the {@link #shouldSkipClass(Class)} method returns false then that class or field type + * will not be part of the JSON output. For deserialization, if {@link #shouldSkipClass(Class)} * returns false, then it will not be set as part of the Java object structure. * *

The following are a few examples that shows how you can use this exclusion mechanism. diff --git a/gson/src/main/java/com/google/gson/FieldAttributes.java b/gson/src/main/java/com/google/gson/FieldAttributes.java index fcac2f4b..76aa5e88 100644 --- a/gson/src/main/java/com/google/gson/FieldAttributes.java +++ b/gson/src/main/java/com/google/gson/FieldAttributes.java @@ -90,7 +90,7 @@ final class FieldAttributes { } /** - * Return the {@link T} annotation object from this field if it exist; otherwise returns + * Return the T annotation object from this field if it exist; otherwise returns * {@code null}. * * @param annotation the class of the annotation that will be retrieved diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 941e5bcf..4c03c2bd 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -202,7 +202,7 @@ public final class Gson { if (src == null) { return JsonNull.createJsonNull(); } - return toJsonTree(src, src.getClass(), false); + return toJsonTree(src, src.getClass()); } /** @@ -222,16 +222,12 @@ public final class Gson { * @since 1.4 */ public JsonElement toJsonTree(Object src, Type typeOfSrc) { - return toJsonTree(src, typeOfSrc, true); - } - - private JsonElement toJsonTree(Object src, Type typeOfSrc, boolean preserveType) { if (src == null) { return JsonNull.createJsonNull(); } JsonSerializationContextDefault context = new JsonSerializationContextDefault( createDefaultObjectNavigatorFactory(serializationStrategy), serializeNulls, serializers); - return context.serialize(src, typeOfSrc, preserveType); + return context.serialize(src, typeOfSrc, true); } /** @@ -251,7 +247,7 @@ public final class Gson { if (src == null) { return serializeNulls ? NULL_STRING : ""; } - return toJson(src, src.getClass(), false); + return toJson(src, src.getClass()); } /** @@ -270,12 +266,8 @@ public final class Gson { * @return Json representation of {@code src} */ public String toJson(Object src, Type typeOfSrc) { - return toJson(src, typeOfSrc, true); - } - - private String toJson(Object src, Type typeOfSrc, boolean preserveType) { StringWriter writer = new StringWriter(); - toJson(src, typeOfSrc, writer, preserveType); + toJson(src, typeOfSrc, writer); return writer.toString(); } @@ -295,7 +287,7 @@ public final class Gson { public void toJson(Object src, Appendable writer) { try { if (src != null) { - toJson(src, src.getClass(), writer, false); + toJson(src, src.getClass(), writer); } else if (serializeNulls) { writeOutNullString(writer); } @@ -320,11 +312,7 @@ public final class Gson { * @since 1.2 */ public void toJson(Object src, Type typeOfSrc, Appendable writer) { - toJson(src, typeOfSrc, writer, true); - } - - private void toJson(Object src, Type typeOfSrc, Appendable writer, boolean preserveType) { - JsonElement jsonElement = toJsonTree(src, typeOfSrc, preserveType); + JsonElement jsonElement = toJsonTree(src, typeOfSrc); toJson(jsonElement, writer); } diff --git a/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java b/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java index 95bb6e75..88132108 100644 --- a/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java @@ -98,8 +98,4 @@ abstract class JsonDeserializationVisitor implements ObjectNavigator.Visitor // This happens primarily because of custom deserializers return childVisitor.getTarget(); } - - public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair) { - return objTypePair; - } } diff --git a/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java b/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java index 9e5bacd6..2b3a1e7c 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationContextDefault.java @@ -42,7 +42,7 @@ final class JsonSerializationContextDefault implements JsonSerializationContext if (src == null) { return JsonNull.createJsonNull(); } - return serialize(src, src.getClass(), false); + return serialize(src, src.getClass(), true); } public JsonElement serialize(Object src, Type typeOfSrc) { diff --git a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java index b21ff42a..e3b499a8 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java @@ -236,8 +236,4 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { public JsonElement getJsonElement() { return root; } - - public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair) { - return objTypePair.toMoreSpecificType(); - } } diff --git a/gson/src/main/java/com/google/gson/ObjectNavigator.java b/gson/src/main/java/com/google/gson/ObjectNavigator.java index b98a18cc..c481c7b0 100644 --- a/gson/src/main/java/com/google/gson/ObjectNavigator.java +++ b/gson/src/main/java/com/google/gson/ObjectNavigator.java @@ -63,8 +63,6 @@ final class ObjectNavigator { */ public boolean visitFieldUsingCustomHandler(Field f, Type actualTypeOfField, Object parent); - public ObjectTypePair getActualTypeIfMoreSpecific(ObjectTypePair objTypePair); - /** * Retrieve the current target */ @@ -117,7 +115,7 @@ final class ObjectNavigator { objectToVisit = visitor.getTarget(); } else { visitor.startVisitingObject(objectToVisit); - ObjectTypePair currObjTypePair = visitor.getActualTypeIfMoreSpecific(objTypePair); + ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType(); Class topLevelClass = new TypeInfo(currObjTypePair.getType()).getRawClass(); for (Class curr = topLevelClass; curr != null && !curr.equals(Object.class); curr = curr.getSuperclass()) { diff --git a/gson/src/main/java/com/google/gson/ObjectTypePair.java b/gson/src/main/java/com/google/gson/ObjectTypePair.java index f9758f5c..69542fd4 100644 --- a/gson/src/main/java/com/google/gson/ObjectTypePair.java +++ b/gson/src/main/java/com/google/gson/ObjectTypePair.java @@ -23,8 +23,6 @@ import java.lang.reflect.Type; * @author Inderjeet Singh */ final class ObjectTypePair { - private static final int PRIME = 31; - private final Object obj; private final Type type; private final boolean preserveType; @@ -46,9 +44,7 @@ final class ObjectTypePair { @SuppressWarnings("unchecked") Pair getMatchingSerializer( ParameterizedTypeHandlerMap> serializers) { - if (obj == null) { - return null; - } + Preconditions.checkNotNull(obj); JsonSerializer serializer = null; if (!preserveType) { // First try looking up the serializer for the actual type @@ -95,7 +91,7 @@ final class ObjectTypePair { public int hashCode() { // Not using type.hashCode() since I am not sure if the subclasses of type reimplement // hashCode() to be equal for equal types - return ((obj == null) ? PRIME : obj.hashCode()); + return ((obj == null) ? 31 : obj.hashCode()); } @Override diff --git a/gson/src/test/java/com/google/gson/FieldAttributesTest.java b/gson/src/test/java/com/google/gson/FieldAttributesTest.java index b6477b9e..e1e18ec3 100644 --- a/gson/src/test/java/com/google/gson/FieldAttributesTest.java +++ b/gson/src/test/java/com/google/gson/FieldAttributesTest.java @@ -72,6 +72,7 @@ public class FieldAttributesTest extends TestCase { } private static class Foo { + @SuppressWarnings("unused") public transient List bar; } }