Skip to content

Commit

Permalink
Code cleanup (#2282)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MaicolAntali committed Dec 13, 2022
1 parent 28affcb commit 0a42c31
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void write(JsonWriter out) throws IOException {
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
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<Date> {
Expand All @@ -47,14 +47,14 @@ public void write(JsonWriter out, Date date) throws IOException {
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);
Expand Down
25 changes: 8 additions & 17 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,7 @@ public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOE
} 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);
Expand Down Expand Up @@ -948,9 +946,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
} 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);
Expand Down Expand Up @@ -1228,8 +1224,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
reader.peek();
isEmpty = false;
TypeAdapter<T> 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
Expand All @@ -1245,9 +1240,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
// 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);
}
Expand Down Expand Up @@ -1381,11 +1374,9 @@ private TypeAdapter<T> delegate() {

@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
+ "}";
}
}
3 changes: 2 additions & 1 deletion gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Objects;

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

private static boolean equal(Object a, Object b) {
return a == b || (a != null && a.equals(b));
return Objects.equals(a, b);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions gson/src/main/java/com/google/gson/internal/Excluder.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ private boolean excludeClassChecks(Class<?> clazz) {
return true;
}

if (isAnonymousOrNonStaticLocal(clazz)) {
return true;
}

return false;
return isAnonymousOrNonStaticLocal(clazz);
}

public boolean excludeClass(Class<?> clazz, boolean serialize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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
Expand Down Expand Up @@ -227,7 +228,7 @@ Node<K, V> findByEntry(Entry<?, ?> entry) {
}

private boolean equal(Object a, Object b) {
return a == b || (a != null && a.equals(b));
return Objects.equals(a, b);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
*/
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();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private final class GsonContextImpl implements JsonSerializationContext, JsonDes
}
@SuppressWarnings("unchecked")
@Override public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (R) gson.fromJson(json, typeOfT);
return gson.fromJson(json, typeOfT);
}
}
}
4 changes: 3 additions & 1 deletion gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ private int peekNumber() throws IOException {
}

// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ public int getIntValue() {
}

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
Expand Down

0 comments on commit 0a42c31

Please sign in to comment.