From 610580b8d959a6fead380e681b9337534c7403db Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 3 Dec 2009 20:58:22 +0000 Subject: [PATCH] exposed final fields of Pair for fast access. Pair is used quite a bit during serialization because of its use in ref stack. --- .../com/google/gson/JsonDeserializationVisitor.java | 4 ++-- .../com/google/gson/JsonSerializationVisitor.java | 4 ++-- .../main/java/com/google/gson/MemoryRefStack.java | 2 +- .../main/java/com/google/gson/ObjectNavigator.java | 8 ++++---- .../main/java/com/google/gson/ObjectTypePair.java | 2 +- gson/src/main/java/com/google/gson/Pair.java | 12 ++---------- ...alizedNameAnnotationInterceptingNamingPolicy.java | 2 +- 7 files changed, 13 insertions(+), 21 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java b/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java index 185c8e0c..edae425f 100644 --- a/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonDeserializationVisitor.java @@ -84,8 +84,8 @@ abstract class JsonDeserializationVisitor implements ObjectNavigator.Visitor if (element == null || element.isJsonNull()) { return null; } - Type objType = pair.getSecond().getType(); - return (pair.getFirst()).deserialize(element, objType, context); + Type objType = pair.second.type; + return (pair.first).deserialize(element, objType, context); } final Object visitChildAsObject(Type childType, JsonElement jsonChild) { diff --git a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java index 53e89192..d4722a18 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationVisitor.java @@ -179,8 +179,8 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor { if (pair == null) { return null; } - JsonSerializer serializer = pair.getFirst(); - objTypePair = pair.getSecond(); + JsonSerializer serializer = pair.first; + objTypePair = pair.second; start(objTypePair); try { JsonElement element = diff --git a/gson/src/main/java/com/google/gson/MemoryRefStack.java b/gson/src/main/java/com/google/gson/MemoryRefStack.java index c4103aa6..8e0cf8cb 100644 --- a/gson/src/main/java/com/google/gson/MemoryRefStack.java +++ b/gson/src/main/java/com/google/gson/MemoryRefStack.java @@ -78,7 +78,7 @@ final class MemoryRefStack { for (ObjectTypePair stackObject : stack) { if (stackObject.getObject() == obj.getObject() - && stackObject.getType().equals(obj.getType()) ) { + && stackObject.type.equals(obj.type) ) { return true; } } diff --git a/gson/src/main/java/com/google/gson/ObjectNavigator.java b/gson/src/main/java/com/google/gson/ObjectNavigator.java index 6a39badc..cf46405a 100644 --- a/gson/src/main/java/com/google/gson/ObjectNavigator.java +++ b/gson/src/main/java/com/google/gson/ObjectNavigator.java @@ -91,7 +91,7 @@ final class ObjectNavigator { * If a field is null, it does not get visited. */ public void accept(Visitor visitor) { - TypeInfo objTypeInfo = new TypeInfo(objTypePair.getType()); + TypeInfo objTypeInfo = new TypeInfo(objTypePair.type); if (exclusionStrategy.shouldSkipClass(objTypeInfo.getRawClass())) { return; } @@ -106,7 +106,7 @@ final class ObjectNavigator { visitor.start(objTypePair); try { if (objTypeInfo.isArray()) { - visitor.visitArray(objectToVisit, objTypePair.getType()); + visitor.visitArray(objectToVisit, objTypePair.type); } else if (objTypeInfo.getActualType() == Object.class && isPrimitiveOrString(objectToVisit)) { // TODO(Joel): this is only used for deserialization of "primitives" @@ -116,7 +116,7 @@ final class ObjectNavigator { } else { visitor.startVisitingObject(objectToVisit); ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType(); - Class topLevelClass = new TypeInfo(currObjTypePair.getType()).getRawClass(); + Class topLevelClass = new TypeInfo(currObjTypePair.type).getRawClass(); for (Class curr = topLevelClass; curr != null && !curr.equals(Object.class); curr = curr.getSuperclass()) { if (!curr.isSynthetic()) { @@ -145,7 +145,7 @@ final class ObjectNavigator { || exclusionStrategy.shouldSkipClass(fieldAttributes.getDeclaredClass())) { continue; // skip } else { - TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.getType()); + TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.type); Type declaredTypeOfField = fieldTypeInfo.getActualType(); boolean visitedWithCustomHandler = visitor.visitFieldUsingCustomHandler(f, declaredTypeOfField, obj); diff --git a/gson/src/main/java/com/google/gson/ObjectTypePair.java b/gson/src/main/java/com/google/gson/ObjectTypePair.java index d679e1a3..de91071b 100644 --- a/gson/src/main/java/com/google/gson/ObjectTypePair.java +++ b/gson/src/main/java/com/google/gson/ObjectTypePair.java @@ -24,7 +24,7 @@ import java.lang.reflect.Type; */ final class ObjectTypePair { private Object obj; - private final Type type; + final Type type; private final boolean preserveType; ObjectTypePair(Object obj, Type type, boolean preserveType) { diff --git a/gson/src/main/java/com/google/gson/Pair.java b/gson/src/main/java/com/google/gson/Pair.java index 4691c096..c9c48343 100644 --- a/gson/src/main/java/com/google/gson/Pair.java +++ b/gson/src/main/java/com/google/gson/Pair.java @@ -17,19 +17,11 @@ package com.google.gson; final class Pair { - private final FIRST first; - private final SECOND second; + final FIRST first; + final SECOND second; Pair(FIRST first, SECOND second) { this.first = first; this.second = second; } - - public FIRST getFirst() { - return first; - } - - public SECOND getSecond() { - return second; - } } diff --git a/gson/src/main/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicy.java b/gson/src/main/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicy.java index 24224e93..0100dc69 100644 --- a/gson/src/main/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/SerializedNameAnnotationInterceptingNamingPolicy.java @@ -33,7 +33,7 @@ import java.lang.reflect.Field; * * @author Joel Leitch */ -class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNamingStrategy { +final class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNamingStrategy { private static final JsonFieldNameValidator fieldNameValidator = new JsonFieldNameValidator(); private final FieldNamingStrategy delegate;