Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #2282

Merged
merged 22 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4685531
Simplify `if` condition in JsonReader.peekNumber()
MaicolAntali Dec 11, 2022
63aefb5
Remove `if` to simplify a `return` in Excluder.excludeClassChecks()
MaicolAntali Dec 11, 2022
97ceee9
Remove redundant variable in Gson.fromJson()
MaicolAntali Dec 11, 2022
0a792e9
equal condition replace by `Objects.equals()` in $Gson$Types.equal()
MaicolAntali Dec 11, 2022
1c3427b
equal condition replace by `Objects.equals()` in LinkedTreeMap.equal()
MaicolAntali Dec 11, 2022
99e8327
Replace `switch` with `if` in UtcDateTypeAdapter.read()
MaicolAntali Dec 11, 2022
a5f20c0
Remove redundant `throws` clause in GraphAdapterBuilder.read()
MaicolAntali Dec 11, 2022
aaad72f
Remove redundant `throws` clause in JsonTreeReader.UNREADABLE_READER
MaicolAntali Dec 11, 2022
a9b4e95
Remove redundant `throws` clause in JsonTreeWriter.UNREADABLE_READER
MaicolAntali Dec 11, 2022
b998890
Remove unnecessary `.initCause()` call
MaicolAntali Dec 11, 2022
3d577f3
Remove redundant cast in TreeTypeAdapter.GsonContextImpl.deserialize
MaicolAntali Dec 11, 2022
14291de
Replace `StringBuilder` with `String`
MaicolAntali Dec 11, 2022
7d3753d
Fix the import and restore the `switch`
MaicolAntali Dec 12, 2022
aba8387
Fix the import
MaicolAntali Dec 12, 2022
e8ec4d3
Add the `util.Objects` import
MaicolAntali Dec 12, 2022
b881da6
Fix indentation
MaicolAntali Dec 12, 2022
9856a94
Add a comment to clarify the condition
MaicolAntali Dec 12, 2022
c5d809a
Fix indentation
MaicolAntali Dec 12, 2022
a3b02c0
Fix imports
MaicolAntali Dec 12, 2022
db0ef3d
Fix indentation
MaicolAntali Dec 12, 2022
fa2dd8c
Fix indentation
MaicolAntali Dec 12, 2022
d7eef65
Fix indentation
MaicolAntali Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Expand Up @@ -19,15 +19,12 @@
import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.*;
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -46,16 +43,14 @@ public void write(JsonWriter out, Date date) throws IOException {
@Override
public Date read(JsonReader in) throws IOException {
try {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
if (Objects.requireNonNull(in.peek()) == JsonToken.NULL) {
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
in.nextNull();
return null;
}
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
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 +
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
",instanceCreators:" + constructorConstructor +
"}";
}
}
9 changes: 2 additions & 7 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Expand Up @@ -28,12 +28,7 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.*;

/**
* Static methods for working with types.
Expand Down Expand Up @@ -167,7 +162,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
Expand Up @@ -198,11 +198,7 @@ private boolean excludeClassChecks(Class<?> clazz) {
return true;
}

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

return false;
return isAnonymousOrNonStaticLocal(clazz);
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
}

public boolean excludeClass(Class<?> clazz, boolean serialize) {
Expand Down
11 changes: 2 additions & 9 deletions gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java
Expand Up @@ -22,14 +22,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.*;
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved

/**
* A map of comparable keys to values. Unlike {@code TreeMap}, this class uses
Expand Down Expand Up @@ -227,7 +220,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
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
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
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);
}
}
}
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/stream/JsonReader.java
Expand Up @@ -737,7 +737,7 @@ 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)) {
if (last == NUMBER_CHAR_DIGIT && fitsInLong && (value != Long.MIN_VALUE || negative) && (value!=0 || !negative)) {
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
peekedLong = negative ? value : -value;
pos += i;
return peeked = PEEKED_LONG;
Expand Down
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 + "," +
MaicolAntali marked this conversation as resolved.
Show resolved Hide resolved
"\"intValue\":" + intValue + "," +
"\"booleanValue\":" + booleanValue + "," +
"\"stringValue\":\"" + stringValue + "\"" +
"}";
}

@Override
Expand Down