Skip to content

Commit 0a42c31

Browse files
authored
Code cleanup (#2282)
* Simplify `if` condition in JsonReader.peekNumber() * Remove `if` to simplify a `return` in Excluder.excludeClassChecks() * Remove redundant variable in Gson.fromJson() * equal condition replace by `Objects.equals()` in $Gson$Types.equal() * equal condition replace by `Objects.equals()` in LinkedTreeMap.equal() * Replace `switch` with `if` in UtcDateTypeAdapter.read() * Remove redundant `throws` clause in GraphAdapterBuilder.read() * Remove redundant `throws` clause in JsonTreeReader.UNREADABLE_READER * Remove redundant `throws` clause in JsonTreeWriter.UNREADABLE_READER * Remove unnecessary `.initCause()` call * Remove redundant cast in TreeTypeAdapter.GsonContextImpl.deserialize * Replace `StringBuilder` with `String` * Fix the import and restore the `switch` * Fix the import * Add the `util.Objects` import * Fix indentation * Add a comment to clarify the condition * Fix indentation * Fix imports * Fix indentation * Fix indentation * Fix indentation
1 parent 28affcb commit 0a42c31

File tree

11 files changed

+37
-48
lines changed

11 files changed

+37
-48
lines changed

extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void write(JsonWriter out) throws IOException {
298298
}
299299

300300
@SuppressWarnings("unchecked")
301-
void read(Graph graph) throws IOException {
301+
void read(Graph graph) {
302302
if (graph.nextCreate != null) {
303303
throw new IllegalStateException("Unexpected recursive call to read() for " + id);
304304
}

extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
import java.util.GregorianCalendar;
2525
import java.util.Locale;
2626
import java.util.TimeZone;
27-
2827
import com.google.gson.JsonParseException;
2928
import com.google.gson.TypeAdapter;
3029
import com.google.gson.stream.JsonReader;
30+
import com.google.gson.stream.JsonToken;
3131
import com.google.gson.stream.JsonWriter;
3232

3333
public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
@@ -47,14 +47,14 @@ public void write(JsonWriter out, Date date) throws IOException {
4747
public Date read(JsonReader in) throws IOException {
4848
try {
4949
switch (in.peek()) {
50-
case NULL:
51-
in.nextNull();
52-
return null;
53-
default:
54-
String date = in.nextString();
55-
// Instead of using iso8601Format.parse(value), we use Jackson's date parsing
56-
// This is because Android doesn't support XXX because it is JDK 1.6
57-
return parse(date, new ParsePosition(0));
50+
case NULL:
51+
in.nextNull();
52+
return null;
53+
default:
54+
String date = in.nextString();
55+
// Instead of using iso8601Format.parse(value), we use Jackson's date parsing
56+
// This is because Android doesn't support XXX because it is JDK 1.6
57+
return parse(date, new ParsePosition(0));
5858
}
5959
} catch (ParseException e) {
6060
throw new JsonParseException(e);

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,7 @@ public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOE
843843
} catch (IOException e) {
844844
throw new JsonIOException(e);
845845
} catch (AssertionError e) {
846-
AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage());
847-
error.initCause(e);
848-
throw error;
846+
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
849847
} finally {
850848
writer.setLenient(oldLenient);
851849
writer.setHtmlSafe(oldHtmlSafe);
@@ -948,9 +946,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
948946
} catch (IOException e) {
949947
throw new JsonIOException(e);
950948
} catch (AssertionError e) {
951-
AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage());
952-
error.initCause(e);
953-
throw error;
949+
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
954950
} finally {
955951
writer.setLenient(oldLenient);
956952
writer.setHtmlSafe(oldHtmlSafe);
@@ -1228,8 +1224,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
12281224
reader.peek();
12291225
isEmpty = false;
12301226
TypeAdapter<T> typeAdapter = getAdapter(typeOfT);
1231-
T object = typeAdapter.read(reader);
1232-
return object;
1227+
return typeAdapter.read(reader);
12331228
} catch (EOFException e) {
12341229
/*
12351230
* For compatibility with JSON 1.5 and earlier, we return null for empty
@@ -1245,9 +1240,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
12451240
// TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
12461241
throw new JsonSyntaxException(e);
12471242
} catch (AssertionError e) {
1248-
AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage());
1249-
error.initCause(e);
1250-
throw error;
1243+
throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e);
12511244
} finally {
12521245
reader.setLenient(oldLenient);
12531246
}
@@ -1381,11 +1374,9 @@ private TypeAdapter<T> delegate() {
13811374

13821375
@Override
13831376
public String toString() {
1384-
return new StringBuilder("{serializeNulls:")
1385-
.append(serializeNulls)
1386-
.append(",factories:").append(factories)
1387-
.append(",instanceCreators:").append(constructorConstructor)
1388-
.append("}")
1389-
.toString();
1377+
return "{serializeNulls:" + serializeNulls
1378+
+ ",factories:" + factories
1379+
+ ",instanceCreators:" + constructorConstructor
1380+
+ "}";
13901381
}
13911382
}

gson/src/main/java/com/google/gson/internal/$Gson$Types.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Map;
3535
import java.util.NoSuchElementException;
3636
import java.util.Properties;
37+
import java.util.Objects;
3738

3839
/**
3940
* Static methods for working with types.
@@ -167,7 +168,7 @@ public static Class<?> getRawType(Type type) {
167168
}
168169

169170
private static boolean equal(Object a, Object b) {
170-
return a == b || (a != null && a.equals(b));
171+
return Objects.equals(a, b);
171172
}
172173

173174
/**

gson/src/main/java/com/google/gson/internal/Excluder.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ private boolean excludeClassChecks(Class<?> clazz) {
198198
return true;
199199
}
200200

201-
if (isAnonymousOrNonStaticLocal(clazz)) {
202-
return true;
203-
}
204-
205-
return false;
201+
return isAnonymousOrNonStaticLocal(clazz);
206202
}
207203

208204
public boolean excludeClass(Class<?> clazz, boolean serialize) {

gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.LinkedHashMap;
3131
import java.util.NoSuchElementException;
3232
import java.util.Set;
33+
import java.util.Objects;
3334

3435
/**
3536
* A map of comparable keys to values. Unlike {@code TreeMap}, this class uses
@@ -227,7 +228,7 @@ Node<K, V> findByEntry(Entry<?, ?> entry) {
227228
}
228229

229230
private boolean equal(Object a, Object b) {
230-
return a == b || (a != null && a.equals(b));
231+
return Objects.equals(a, b);
231232
}
232233

233234
/**

gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
*/
3939
public final class JsonTreeReader extends JsonReader {
4040
private static final Reader UNREADABLE_READER = new Reader() {
41-
@Override public int read(char[] buffer, int offset, int count) throws IOException {
41+
@Override public int read(char[] buffer, int offset, int count) {
4242
throw new AssertionError();
4343
}
44-
@Override public void close() throws IOException {
44+
@Override public void close() {
4545
throw new AssertionError();
4646
}
4747
};

gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public final class JsonTreeWriter extends JsonWriter {
3636
@Override public void write(char[] buffer, int offset, int counter) {
3737
throw new AssertionError();
3838
}
39-
@Override public void flush() throws IOException {
39+
@Override public void flush() {
4040
throw new AssertionError();
4141
}
42-
@Override public void close() throws IOException {
42+
@Override public void close() {
4343
throw new AssertionError();
4444
}
4545
};

gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private final class GsonContextImpl implements JsonSerializationContext, JsonDes
176176
}
177177
@SuppressWarnings("unchecked")
178178
@Override public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
179-
return (R) gson.fromJson(json, typeOfT);
179+
return gson.fromJson(json, typeOfT);
180180
}
181181
}
182182
}

gson/src/main/java/com/google/gson/stream/JsonReader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,9 @@ private int peekNumber() throws IOException {
737737
}
738738

739739
// We've read a complete number. Decide if it's a PEEKED_LONG or a PEEKED_NUMBER.
740-
if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative) && (value!=0 || false==negative)) {
740+
// Don't store -0 as long; user might want to read it as double -0.0
741+
// Don't try to convert Long.MIN_VALUE to positive long; it would overflow MAX_VALUE
742+
if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative) && (value!=0 || !negative)) {
741743
peekedLong = negative ? value : -value;
742744
pos += i;
743745
return peeked = PEEKED_LONG;

metrics/src/main/java/com/google/gson/metrics/BagOfPrimitives.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ public int getIntValue() {
4343
}
4444

4545
public String getExpectedJson() {
46-
StringBuilder sb = new StringBuilder();
47-
sb.append("{");
48-
sb.append("\"longValue\":").append(longValue).append(",");
49-
sb.append("\"intValue\":").append(intValue).append(",");
50-
sb.append("\"booleanValue\":").append(booleanValue).append(",");
51-
sb.append("\"stringValue\":\"").append(stringValue).append("\"");
52-
sb.append("}");
53-
return sb.toString();
46+
return "{"
47+
+ "\"longValue\":" + longValue + ","
48+
+ "\"intValue\":" + intValue + ","
49+
+ "\"booleanValue\":" + booleanValue + ","
50+
+ "\"stringValue\":\"" + stringValue + "\""
51+
+ "}";
5452
}
5553

5654
@Override

0 commit comments

Comments
 (0)