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
This commit is contained in:
Maicol 2022-12-13 17:50:25 +01:00 committed by GitHub
parent 28affcbdb9
commit 0a42c31efe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 37 additions and 48 deletions

View File

@ -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);
}

View File

@ -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<Date> {
@ -47,14 +47,14 @@ public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
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);

View File

@ -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<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
@ -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
+ "}";
}
}

View File

@ -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);
}
/**

View File

@ -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) {

View File

@ -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<K, V> extends AbstractMap<K, V> implements Seri
}
private boolean equal(Object a, Object b) {
return a == b || (a != null && a.equals(b));
return Objects.equals(a, b);
}
/**

View File

@ -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();
}
};

View File

@ -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();
}
};

View File

@ -176,7 +176,7 @@ public final class TreeTypeAdapter<T> extends SerializationDelegatingTypeAdapter
}
@SuppressWarnings("unchecked")
@Override public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (R) gson.fromJson(json, typeOfT);
return gson.fromJson(json, typeOfT);
}
}
}

View File

@ -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;

View File

@ -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