From 0a42c31efe265368b71b629a071e7be6c9593d10 Mon Sep 17 00:00:00 2001 From: Maicol <79454487+MaicolAntali@users.noreply.github.com> Date: Tue, 13 Dec 2022 17:50:25 +0100 Subject: [PATCH] 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 --- .../gson/graph/GraphAdapterBuilder.java | 2 +- .../gson/typeadapters/UtcDateTypeAdapter.java | 18 ++++++------- gson/src/main/java/com/google/gson/Gson.java | 25 ++++++------------- .../com/google/gson/internal/$Gson$Types.java | 3 ++- .../com/google/gson/internal/Excluder.java | 6 +---- .../google/gson/internal/LinkedTreeMap.java | 3 ++- .../gson/internal/bind/JsonTreeReader.java | 4 +-- .../gson/internal/bind/JsonTreeWriter.java | 4 +-- .../gson/internal/bind/TreeTypeAdapter.java | 2 +- .../com/google/gson/stream/JsonReader.java | 4 ++- .../google/gson/metrics/BagOfPrimitives.java | 14 +++++------ 11 files changed, 37 insertions(+), 48 deletions(-) diff --git a/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java b/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java index c48c3cd9..b226b220 100644 --- a/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java +++ b/extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java @@ -298,7 +298,7 @@ public final class GraphAdapterBuilder { } @SuppressWarnings("unchecked") - void read(Graph graph) throws IOException { + void read(Graph graph) { if (graph.nextCreate != null) { throw new IllegalStateException("Unexpected recursive call to read() for " + id); } diff --git a/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java index 1e889d37..2278f842 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java +++ b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java @@ -24,10 +24,10 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; - import com.google.gson.JsonParseException; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; public final class UtcDateTypeAdapter extends TypeAdapter { @@ -47,14 +47,14 @@ public final class UtcDateTypeAdapter extends TypeAdapter { public Date read(JsonReader in) throws IOException { try { switch (in.peek()) { - case NULL: - in.nextNull(); - return null; - default: - String date = in.nextString(); - // Instead of using iso8601Format.parse(value), we use Jackson's date parsing - // This is because Android doesn't support XXX because it is JDK 1.6 - return parse(date, new ParsePosition(0)); + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + // Instead of using iso8601Format.parse(value), we use Jackson's date parsing + // This is because Android doesn't support XXX because it is JDK 1.6 + return parse(date, new ParsePosition(0)); } } catch (ParseException e) { throw new JsonParseException(e); diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 8a26760f..0339d569 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -843,9 +843,7 @@ public final class Gson { } catch (IOException e) { throw new JsonIOException(e); } catch (AssertionError e) { - AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage()); - error.initCause(e); - throw error; + throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e); } finally { writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); @@ -948,9 +946,7 @@ public final class Gson { } catch (IOException e) { throw new JsonIOException(e); } catch (AssertionError e) { - AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage()); - error.initCause(e); - throw error; + throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e); } finally { writer.setLenient(oldLenient); writer.setHtmlSafe(oldHtmlSafe); @@ -1228,8 +1224,7 @@ public final class Gson { reader.peek(); isEmpty = false; TypeAdapter typeAdapter = getAdapter(typeOfT); - T object = typeAdapter.read(reader); - return object; + return typeAdapter.read(reader); } catch (EOFException e) { /* * For compatibility with JSON 1.5 and earlier, we return null for empty @@ -1245,9 +1240,7 @@ public final class Gson { // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException throw new JsonSyntaxException(e); } catch (AssertionError e) { - AssertionError error = new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage()); - error.initCause(e); - throw error; + throw new AssertionError("AssertionError (GSON " + GsonBuildConfig.VERSION + "): " + e.getMessage(), e); } finally { reader.setLenient(oldLenient); } @@ -1381,11 +1374,9 @@ public final class Gson { @Override public String toString() { - return new StringBuilder("{serializeNulls:") - .append(serializeNulls) - .append(",factories:").append(factories) - .append(",instanceCreators:").append(constructorConstructor) - .append("}") - .toString(); + return "{serializeNulls:" + serializeNulls + + ",factories:" + factories + + ",instanceCreators:" + constructorConstructor + + "}"; } } 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 965e010f..4a925aa4 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 @@ -34,6 +34,7 @@ import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; import java.util.Properties; +import java.util.Objects; /** * Static methods for working with types. @@ -167,7 +168,7 @@ public final class $Gson$Types { } private static boolean equal(Object a, Object b) { - return a == b || (a != null && a.equals(b)); + return Objects.equals(a, b); } /** diff --git a/gson/src/main/java/com/google/gson/internal/Excluder.java b/gson/src/main/java/com/google/gson/internal/Excluder.java index 03bd45cb..dd167b48 100644 --- a/gson/src/main/java/com/google/gson/internal/Excluder.java +++ b/gson/src/main/java/com/google/gson/internal/Excluder.java @@ -198,11 +198,7 @@ public final class Excluder implements TypeAdapterFactory, Cloneable { return true; } - if (isAnonymousOrNonStaticLocal(clazz)) { - return true; - } - - return false; + return isAnonymousOrNonStaticLocal(clazz); } public boolean excludeClass(Class clazz, boolean serialize) { diff --git a/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java b/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java index e47e165d..1fe512ad 100644 --- a/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java +++ b/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java @@ -30,6 +30,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.NoSuchElementException; import java.util.Set; +import java.util.Objects; /** * A map of comparable keys to values. Unlike {@code TreeMap}, this class uses @@ -227,7 +228,7 @@ public final class LinkedTreeMap extends AbstractMap implements Seri } private boolean equal(Object a, Object b) { - return a == b || (a != null && a.equals(b)); + return Objects.equals(a, b); } /** diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java index 47e70e68..085090f2 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeReader.java @@ -38,10 +38,10 @@ import java.util.Map; */ public final class JsonTreeReader extends JsonReader { private static final Reader UNREADABLE_READER = new Reader() { - @Override public int read(char[] buffer, int offset, int count) throws IOException { + @Override public int read(char[] buffer, int offset, int count) { throw new AssertionError(); } - @Override public void close() throws IOException { + @Override public void close() { throw new AssertionError(); } }; diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index 6ff1aa46..a87896da 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -36,10 +36,10 @@ public final class JsonTreeWriter extends JsonWriter { @Override public void write(char[] buffer, int offset, int counter) { throw new AssertionError(); } - @Override public void flush() throws IOException { + @Override public void flush() { throw new AssertionError(); } - @Override public void close() throws IOException { + @Override public void close() { throw new AssertionError(); } }; diff --git a/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java index 560234c0..2efd6c6b 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java @@ -176,7 +176,7 @@ public final class TreeTypeAdapter extends SerializationDelegatingTypeAdapter } @SuppressWarnings("unchecked") @Override public R deserialize(JsonElement json, Type typeOfT) throws JsonParseException { - return (R) gson.fromJson(json, typeOfT); + return gson.fromJson(json, typeOfT); } } } diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index ed6bab97..1d180eac 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -737,7 +737,9 @@ public class JsonReader implements Closeable { } // We've read a complete number. Decide if it's a PEEKED_LONG or a PEEKED_NUMBER. - if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative) && (value!=0 || false==negative)) { + // Don't store -0 as long; user might want to read it as double -0.0 + // Don't try to convert Long.MIN_VALUE to positive long; it would overflow MAX_VALUE + if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative) && (value!=0 || !negative)) { peekedLong = negative ? value : -value; pos += i; return peeked = PEEKED_LONG; diff --git a/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitives.java b/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitives.java index 008e31f3..a073974d 100644 --- a/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitives.java +++ b/metrics/src/main/java/com/google/gson/metrics/BagOfPrimitives.java @@ -43,14 +43,12 @@ public class BagOfPrimitives { } public String getExpectedJson() { - StringBuilder sb = new StringBuilder(); - sb.append("{"); - sb.append("\"longValue\":").append(longValue).append(","); - sb.append("\"intValue\":").append(intValue).append(","); - sb.append("\"booleanValue\":").append(booleanValue).append(","); - sb.append("\"stringValue\":\"").append(stringValue).append("\""); - sb.append("}"); - return sb.toString(); + return "{" + + "\"longValue\":" + longValue + "," + + "\"intValue\":" + intValue + "," + + "\"booleanValue\":" + booleanValue + "," + + "\"stringValue\":\"" + stringValue + "\"" + + "}"; } @Override