diff --git a/gson/src/main/java/com/google/gson/internal/CurrentWrite.java b/gson/src/main/java/com/google/gson/internal/CurrentWrite.java new file mode 100644 index 0000000000..62aa6e52f2 --- /dev/null +++ b/gson/src/main/java/com/google/gson/internal/CurrentWrite.java @@ -0,0 +1,35 @@ +package com.google.gson.internal; + +public class CurrentWrite implements CharSequence{ + private char[] chars; + private String cachedString; + + void setChars(char[] chars) { + this.chars = chars; + this.cachedString = null; + } + + @Override + public int length() { + return chars.length; + } + + @Override + public char charAt(int i) { + return chars[i]; + } + + @Override + public CharSequence subSequence(int start, int end) { + return new String(chars, start, end - start); + } + + // Must return string representation to satisfy toString() contract + @Override + public String toString() { + if (cachedString == null) { + cachedString = new String(chars); + } + return cachedString; + } +} \ No newline at end of file diff --git a/gson/src/main/java/com/google/gson/internal/Streams.java b/gson/src/main/java/com/google/gson/internal/Streams.java index 46df853f5a..f114dfbcdb 100644 --- a/gson/src/main/java/com/google/gson/internal/Streams.java +++ b/gson/src/main/java/com/google/gson/internal/Streams.java @@ -120,40 +120,5 @@ public Writer append(CharSequence csq, int start, int end) throws IOException { appendable.append(csq, start, end); return this; } - - /** A mutable char sequence pointing at a single char[]. */ - private static class CurrentWrite implements CharSequence { - private char[] chars; - private String cachedString; - - void setChars(char[] chars) { - this.chars = chars; - this.cachedString = null; - } - - @Override - public int length() { - return chars.length; - } - - @Override - public char charAt(int i) { - return chars[i]; - } - - @Override - public CharSequence subSequence(int start, int end) { - return new String(chars, start, end - start); - } - - // Must return string representation to satisfy toString() contract - @Override - public String toString() { - if (cachedString == null) { - cachedString = new String(chars); - } - return cachedString; - } - } } -} +} \ No newline at end of file diff --git a/gson/src/main/java/com/google/gson/internal/bind/BoundFieldCreationContext.java b/gson/src/main/java/com/google/gson/internal/bind/BoundFieldCreationContext.java new file mode 100644 index 0000000000..ba06658446 --- /dev/null +++ b/gson/src/main/java/com/google/gson/internal/bind/BoundFieldCreationContext.java @@ -0,0 +1,30 @@ +package com.google.gson.internal.bind; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +class BoundFieldCreationContext{ + final Gson context; + final Field field; + final Method accessor; + final String serializedName; + final TypeToken fieldType; + + BoundFieldCreationContext( + final Gson context, + final Field field, + final Method accessor, + final String serializedName, + final TypeToken fieldType){ + + this.context = context; + this.field = field; + this.accessor = accessor; + this.serializedName = serializedName; + this.fieldType = fieldType; + + } +} \ No newline at end of file diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java index 79e93bcc77..631c98f4e4 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java @@ -175,30 +175,26 @@ private static void checkAccessible( } private BoundField createBoundField( - final Gson context, - final Field field, - final Method accessor, - final String serializedName, - final TypeToken fieldType, + final BoundFieldCreationContext boundFieldCreationContext, final boolean serialize, final boolean blockInaccessible) { - final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType()); + final boolean isPrimitive = Primitives.isPrimitive(boundFieldCreationContext.fieldType.getRawType()); - int modifiers = field.getModifiers(); + int modifiers = boundFieldCreationContext.field.getModifiers(); final boolean isStaticFinalField = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers); - JsonAdapter annotation = field.getAnnotation(JsonAdapter.class); + JsonAdapter annotation = boundFieldCreationContext.field.getAnnotation(JsonAdapter.class); TypeAdapter mapped = null; if (annotation != null) { // This is not safe; requires that user has specified correct adapter class for @JsonAdapter mapped = jsonAdapterFactory.getTypeAdapter( - constructorConstructor, context, fieldType, annotation, false); + constructorConstructor, boundFieldCreationContext.context, boundFieldCreationContext.fieldType, annotation, false); } final boolean jsonAdapterPresent = mapped != null; if (mapped == null) { - mapped = context.getAdapter(fieldType); + mapped = boundFieldCreationContext.context.getAdapter(boundFieldCreationContext.fieldType); } @SuppressWarnings("unchecked") @@ -208,31 +204,31 @@ private BoundField createBoundField( writeTypeAdapter = jsonAdapterPresent ? typeAdapter - : new TypeAdapterRuntimeTypeWrapper<>(context, typeAdapter, fieldType.getType()); + : new TypeAdapterRuntimeTypeWrapper<>(boundFieldCreationContext.context, typeAdapter, boundFieldCreationContext.fieldType.getType()); } else { // Will never actually be used, but we set it to avoid confusing nullness-analysis tools writeTypeAdapter = typeAdapter; } - return new BoundField(serializedName, field) { + return new BoundField(boundFieldCreationContext.serializedName, boundFieldCreationContext.field) { @Override void write(JsonWriter writer, Object source) throws IOException, IllegalAccessException { if (blockInaccessible) { - if (accessor == null) { + if (boundFieldCreationContext.accessor == null) { checkAccessible(source, field); } else { // Note: This check might actually be redundant because access check for canonical // constructor should have failed already - checkAccessible(source, accessor); + checkAccessible(source, boundFieldCreationContext.accessor); } } Object fieldValue; - if (accessor != null) { + if (boundFieldCreationContext.accessor != null) { try { - fieldValue = accessor.invoke(source); + fieldValue = boundFieldCreationContext.accessor.invoke(source); } catch (InvocationTargetException e) { String accessorDescription = - ReflectionHelper.getAccessibleObjectDescription(accessor, false); + ReflectionHelper.getAccessibleObjectDescription(boundFieldCreationContext.accessor, false); throw new JsonIOException( "Accessor " + accessorDescription + " threw exception", e.getCause()); } @@ -391,13 +387,10 @@ private FieldsData getBoundFields( Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType()); List fieldNames = getFieldNames(field); String serializedName = fieldNames.get(0); + BoundFieldCreationContext boundFieldCreationContext = new BoundFieldCreationContext(context, field, accessor, serializedName, TypeToken.get(fieldType)); BoundField boundField = createBoundField( - context, - field, - accessor, - serializedName, - TypeToken.get(fieldType), + boundFieldCreationContext, serialize, blockInaccessible); @@ -666,4 +659,4 @@ T finalize(Object[] accumulator) { } } } -} +} \ No newline at end of file