exposed final fields of Pair for fast access. Pair is used quite a bit during serialization because of its use in ref stack.

This commit is contained in:
Inderjeet Singh 2009-12-03 20:58:22 +00:00
parent fff9147988
commit 610580b8d9
7 changed files with 13 additions and 21 deletions

View File

@ -84,8 +84,8 @@ abstract class JsonDeserializationVisitor<T> implements ObjectNavigator.Visitor
if (element == null || element.isJsonNull()) { if (element == null || element.isJsonNull()) {
return null; return null;
} }
Type objType = pair.getSecond().getType(); Type objType = pair.second.type;
return (pair.getFirst()).deserialize(element, objType, context); return (pair.first).deserialize(element, objType, context);
} }
final Object visitChildAsObject(Type childType, JsonElement jsonChild) { final Object visitChildAsObject(Type childType, JsonElement jsonChild) {

View File

@ -179,8 +179,8 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
if (pair == null) { if (pair == null) {
return null; return null;
} }
JsonSerializer serializer = pair.getFirst(); JsonSerializer serializer = pair.first;
objTypePair = pair.getSecond(); objTypePair = pair.second;
start(objTypePair); start(objTypePair);
try { try {
JsonElement element = JsonElement element =

View File

@ -78,7 +78,7 @@ final class MemoryRefStack {
for (ObjectTypePair stackObject : stack) { for (ObjectTypePair stackObject : stack) {
if (stackObject.getObject() == obj.getObject() if (stackObject.getObject() == obj.getObject()
&& stackObject.getType().equals(obj.getType()) ) { && stackObject.type.equals(obj.type) ) {
return true; return true;
} }
} }

View File

@ -91,7 +91,7 @@ final class ObjectNavigator {
* If a field is null, it does not get visited. * If a field is null, it does not get visited.
*/ */
public void accept(Visitor visitor) { public void accept(Visitor visitor) {
TypeInfo objTypeInfo = new TypeInfo(objTypePair.getType()); TypeInfo objTypeInfo = new TypeInfo(objTypePair.type);
if (exclusionStrategy.shouldSkipClass(objTypeInfo.getRawClass())) { if (exclusionStrategy.shouldSkipClass(objTypeInfo.getRawClass())) {
return; return;
} }
@ -106,7 +106,7 @@ final class ObjectNavigator {
visitor.start(objTypePair); visitor.start(objTypePair);
try { try {
if (objTypeInfo.isArray()) { if (objTypeInfo.isArray()) {
visitor.visitArray(objectToVisit, objTypePair.getType()); visitor.visitArray(objectToVisit, objTypePair.type);
} else if (objTypeInfo.getActualType() == Object.class } else if (objTypeInfo.getActualType() == Object.class
&& isPrimitiveOrString(objectToVisit)) { && isPrimitiveOrString(objectToVisit)) {
// TODO(Joel): this is only used for deserialization of "primitives" // TODO(Joel): this is only used for deserialization of "primitives"
@ -116,7 +116,7 @@ final class ObjectNavigator {
} else { } else {
visitor.startVisitingObject(objectToVisit); visitor.startVisitingObject(objectToVisit);
ObjectTypePair currObjTypePair = objTypePair.toMoreSpecificType(); 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); for (Class<?> curr = topLevelClass; curr != null && !curr.equals(Object.class);
curr = curr.getSuperclass()) { curr = curr.getSuperclass()) {
if (!curr.isSynthetic()) { if (!curr.isSynthetic()) {
@ -145,7 +145,7 @@ final class ObjectNavigator {
|| exclusionStrategy.shouldSkipClass(fieldAttributes.getDeclaredClass())) { || exclusionStrategy.shouldSkipClass(fieldAttributes.getDeclaredClass())) {
continue; // skip continue; // skip
} else { } else {
TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.getType()); TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.type);
Type declaredTypeOfField = fieldTypeInfo.getActualType(); Type declaredTypeOfField = fieldTypeInfo.getActualType();
boolean visitedWithCustomHandler = boolean visitedWithCustomHandler =
visitor.visitFieldUsingCustomHandler(f, declaredTypeOfField, obj); visitor.visitFieldUsingCustomHandler(f, declaredTypeOfField, obj);

View File

@ -24,7 +24,7 @@ import java.lang.reflect.Type;
*/ */
final class ObjectTypePair { final class ObjectTypePair {
private Object obj; private Object obj;
private final Type type; final Type type;
private final boolean preserveType; private final boolean preserveType;
ObjectTypePair(Object obj, Type type, boolean preserveType) { ObjectTypePair(Object obj, Type type, boolean preserveType) {

View File

@ -17,19 +17,11 @@ package com.google.gson;
final class Pair<FIRST, SECOND> { final class Pair<FIRST, SECOND> {
private final FIRST first; final FIRST first;
private final SECOND second; final SECOND second;
Pair(FIRST first, SECOND second) { Pair(FIRST first, SECOND second) {
this.first = first; this.first = first;
this.second = second; this.second = second;
} }
public FIRST getFirst() {
return first;
}
public SECOND getSecond() {
return second;
}
} }

View File

@ -33,7 +33,7 @@ import java.lang.reflect.Field;
* *
* @author Joel Leitch * @author Joel Leitch
*/ */
class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNamingStrategy { final class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNamingStrategy {
private static final JsonFieldNameValidator fieldNameValidator = new JsonFieldNameValidator(); private static final JsonFieldNameValidator fieldNameValidator = new JsonFieldNameValidator();
private final FieldNamingStrategy delegate; private final FieldNamingStrategy delegate;