Skip to content

Commit

Permalink
Code cleanup (google#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

(cherry picked from commit 0a42c31)
  • Loading branch information
MaicolAntali authored and tibor-universe committed Jan 18, 2023
1 parent d9a9e1b commit 61eac8a
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 40 deletions.
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 @@ -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
13 changes: 5 additions & 8 deletions gson/src/main/java/com/google/gson/Gson.java
Expand Up @@ -1234,8 +1234,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 Down Expand Up @@ -1385,11 +1384,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
Expand Up @@ -37,6 +37,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 @@ -166,7 +167,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);
}

public boolean excludeClass(Class<?> clazz, boolean serialize) {
Expand Down
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
Expand Up @@ -13,7 +13,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* This file has been modified by Happeo Oy.
*/

Expand Down Expand Up @@ -42,10 +42,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 InvalidStateException();
}
@Override public void close() throws IOException {
@Override public void close() {
throw new InvalidStateException();
}
};
Expand Down
Expand Up @@ -40,10 +40,10 @@ public final class JsonTreeWriter extends JsonWriter {
@Override public void write(char[] buffer, int offset, int counter) {
throw new InvalidStateException();
}
@Override public void flush() throws IOException {
@Override public void flush() {
throw new InvalidStateException();
}
@Override public void close() throws IOException {
@Override public void close() {
throw new InvalidStateException();
}
};
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);
}
}
}
4 changes: 3 additions & 1 deletion gson/src/main/java/com/google/gson/stream/JsonReader.java
Expand Up @@ -741,7 +741,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
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 61eac8a

Please sign in to comment.