diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index 3b325a4f..5d8b4b7f 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -122,7 +122,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy { */ static String separateCamelCase(String name, String separator) { StringBuilder translation = new StringBuilder(); - for (int i = 0; i < name.length(); i++) { + for (int i = 0, length = name.length(); i < length; i++) { char character = name.charAt(i); if (Character.isUpperCase(character) && translation.length() != 0) { translation.append(separator); @@ -139,8 +139,9 @@ public enum FieldNamingPolicy implements FieldNamingStrategy { StringBuilder fieldNameBuilder = new StringBuilder(); int index = 0; char firstCharacter = name.charAt(index); + int length = name.length(); - while (index < name.length() - 1) { + while (index < length - 1) { if (Character.isLetter(firstCharacter)) { break; } @@ -149,7 +150,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy { firstCharacter = name.charAt(++index); } - if (index == name.length()) { + if (index == length) { return fieldNameBuilder.toString(); } diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java index 017e27ce..8ba697ed 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java @@ -411,7 +411,7 @@ public final class $Gson$Types { } private static int indexOf(Object[] array, Object toFind) { - for (int i = 0; i < array.length; i++) { + for (int i = 0, length = array.length; i < length; i++) { if (toFind.equals(array[i])) { return i; } @@ -451,7 +451,7 @@ public final class $Gson$Types { this.ownerType = ownerType == null ? null : canonicalize(ownerType); this.rawType = canonicalize(rawType); this.typeArguments = typeArguments.clone(); - for (int t = 0; t < this.typeArguments.length; t++) { + for (int t = 0, length = this.typeArguments.length; t < length; t++) { checkNotNull(this.typeArguments[t]); checkNotPrimitive(this.typeArguments[t]); this.typeArguments[t] = canonicalize(this.typeArguments[t]); @@ -482,15 +482,14 @@ public final class $Gson$Types { } @Override public String toString() { - StringBuilder stringBuilder = new StringBuilder(30 * (typeArguments.length + 1)); - stringBuilder.append(typeToString(rawType)); - - if (typeArguments.length == 0) { - return stringBuilder.toString(); + int length = typeArguments.length; + if (length == 0) { + return typeToString(rawType); } - stringBuilder.append("<").append(typeToString(typeArguments[0])); - for (int i = 1; i < typeArguments.length; i++) { + StringBuilder stringBuilder = new StringBuilder(30 * (length + 1)); + stringBuilder.append(typeToString(rawType)).append("<").append(typeToString(typeArguments[0])); + for (int i = 1; i < length; i++) { stringBuilder.append(", ").append(typeToString(typeArguments[i])); } return stringBuilder.append(">").toString(); diff --git a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java index 3fb7c056..56101706 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java @@ -73,8 +73,10 @@ public final class ArrayTypeAdapter extends TypeAdapter { list.add(instance); } in.endArray(); - Object array = Array.newInstance(componentType, list.size()); - for (int i = 0; i < list.size(); i++) { + + int size = list.size(); + Object array = Array.newInstance(componentType, size); + for (int i = 0; i < size; i++) { Array.set(array, i, list.get(i)); } return array; 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 34e97664..42798d05 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 @@ -158,7 +158,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType()); List fieldNames = getFieldNames(field); BoundField previous = null; - for (int i = 0; i < fieldNames.size(); ++i) { + for (int i = 0, size = fieldNames.size(); i < size; ++i) { String name = fieldNames.get(i); if (i != 0) serialize = false; // only serialize the default name BoundField boundField = createBoundField(context, field, name, diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 31beb5d6..c2839bc8 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -69,33 +69,20 @@ public final class TypeAdapters { public static final TypeAdapter CLASS = new TypeAdapter() { @Override public void write(JsonWriter out, Class value) throws IOException { - if (value == null) { - out.nullValue(); - } else { - throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: " - + value.getName() + ". Forgot to register a type adapter?"); - } + throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: " + + value.getName() + ". Forgot to register a type adapter?"); } @Override public Class read(JsonReader in) throws IOException { - if (in.peek() == JsonToken.NULL) { - in.nextNull(); - return null; - } else { - throw new UnsupportedOperationException( - "Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?"); - } + throw new UnsupportedOperationException( + "Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?"); } - }; + }.nullSafe(); + public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS); public static final TypeAdapter BIT_SET = new TypeAdapter() { @Override public BitSet read(JsonReader in) throws IOException { - if (in.peek() == JsonToken.NULL) { - in.nextNull(); - return null; - } - BitSet bitset = new BitSet(); in.beginArray(); int i = 0; @@ -132,19 +119,14 @@ public final class TypeAdapters { } @Override public void write(JsonWriter out, BitSet src) throws IOException { - if (src == null) { - out.nullValue(); - return; - } - out.beginArray(); - for (int i = 0; i < src.length(); i++) { + for (int i = 0, length = src.length(); i < length; i++) { int value = (src.get(i)) ? 1 : 0; out.value(value); } out.endArray(); } - }; + }.nullSafe(); public static final TypeAdapterFactory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET); @@ -922,4 +904,4 @@ public final class TypeAdapters { } }; } -} +} \ No newline at end of file