Fix error prone warnings (#2316)
* Fix `OperatorPrecedence` warn in `JsonWriter#close` * Fix `ReferenceEquality` warn in `LinkedTreeMap#replaceInParent` * Fix `UnnecessaryParentheses` warn in `LinkedTreeMap#replaceInParent` * Fix `ReferenceEquality` warn in `LinkedTreeMap#hasNext` * Fix `ReferenceEquality` warn in `LinkedTreeMap#nextNode` * Adds `error_prone_annotations` to the `pom.xml` of `gson` * Fix `InlineMeSuggester` warns in `JsonParser` * Fix `UnnecessaryParentheses` warns in `ConstructorConstructor#newDefaultImplementationConstructor` * Fix `ThreadLocalUsage` warn in `Gson` * Fix `JdkObsolete` warn in `GsonBuilder` * Fix `ReferenceEquality` warn in `LazilyParsedNumber#equals` * Fix `OperatorPrecedence` warn in `TreeTypeAdapter#create` * Fix `OperatorPrecedence` warn in `ArrayTypeAdapter` * Fix `UnnecessaryParentheses` warn in `TypeAdapters` * Adds `-XepExcludedPaths` flag to ErrorProne plugin to exclude tests and proto path * Fix `ClassNewInstance` warn in `InterceptorAdapter` * Fix `ThreadLocalUsage` warn in `GraphAdapterBuilder` * Fix `JdkObsolete` warn in `GraphAdapterBuilder` * Revert "Adds `error_prone_annotations` to the `pom.xml` of `gson`" This reverts commit 14af14dfa23b46a54f4855a70ccf2b0a2cdc3e3f. * Revert "Fix `InlineMeSuggester` warns in `JsonParser`" This reverts commit 095bfd517e06510e4cc9cc6b1aac58ad9bf3038a. * Adds `@SuppressWarnings("ThreadLocalUsage")` * Fix `OperatorPrecedence` in `JsonWriter` * Revert "Fix `ReferenceEquality` warn in `LinkedTreeMap#nextNode`" This reverts commit 387746c7f7e3d0943c8f80501f5d9c3710f4862e. * Adds `@SuppressWarnings("ReferenceEquality")` * Adds `guava-testlib` to the gson `pom.xml` * `@SuppressWarnings("TruthSelfEquals")` removed to use `EqualsTester()`
This commit is contained in:
parent
19f54ee6ed
commit
2658aca66a
|
@ -31,10 +31,10 @@ import com.google.gson.stream.JsonToken;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.IdentityHashMap;
|
import java.util.IdentityHashMap;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
|
||||||
|
@ -79,6 +79,7 @@ public final class GraphAdapterBuilder {
|
||||||
|
|
||||||
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
|
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
|
||||||
private final Map<Type, InstanceCreator<?>> instanceCreators;
|
private final Map<Type, InstanceCreator<?>> instanceCreators;
|
||||||
|
@SuppressWarnings("ThreadLocalUsage")
|
||||||
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();
|
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();
|
||||||
|
|
||||||
Factory(Map<Type, InstanceCreator<?>> instanceCreators) {
|
Factory(Map<Type, InstanceCreator<?>> instanceCreators) {
|
||||||
|
@ -240,7 +241,7 @@ public final class GraphAdapterBuilder {
|
||||||
* The queue of elements to write during serialization. Unused during
|
* The queue of elements to write during serialization. Unused during
|
||||||
* deserialization.
|
* deserialization.
|
||||||
*/
|
*/
|
||||||
private final Queue<Element<?>> queue = new LinkedList<>();
|
private final Queue<Element<?>> queue = new ArrayDeque<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The instance currently being deserialized. Used as a backdoor between
|
* The instance currently being deserialized. Used as a backdoor between
|
||||||
|
|
|
@ -46,7 +46,7 @@ public final class InterceptorFactory implements TypeAdapterFactory {
|
||||||
public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
|
public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
|
||||||
try {
|
try {
|
||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
this.postDeserializer = intercept.postDeserialize().newInstance();
|
this.postDeserializer = intercept.postDeserialize().getDeclaredConstructor().newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,12 @@
|
||||||
<version>1.1.3</version>
|
<version>1.1.3</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava-testlib</artifactId>
|
||||||
|
<version>31.1-jre</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -166,6 +166,7 @@ public final class Gson {
|
||||||
* with the type token provided to {@code getAdapter} as key and either
|
* with the type token provided to {@code getAdapter} as key and either
|
||||||
* {@code FutureTypeAdapter} or a regular {@code TypeAdapter} as value.
|
* {@code FutureTypeAdapter} or a regular {@code TypeAdapter} as value.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("ThreadLocalUsage")
|
||||||
private final ThreadLocal<Map<TypeToken<?>, TypeAdapter<?>>> threadLocalAdapterResults = new ThreadLocal<>();
|
private final ThreadLocal<Map<TypeToken<?>, TypeAdapter<?>>> threadLocalAdapterResults = new ThreadLocal<>();
|
||||||
|
|
||||||
private final ConcurrentMap<TypeToken<?>, TypeAdapter<?>> typeTokenCache = new ConcurrentHashMap<>();
|
private final ConcurrentMap<TypeToken<?>, TypeAdapter<?>> typeTokenCache = new ConcurrentHashMap<>();
|
||||||
|
|
|
@ -19,11 +19,11 @@ package com.google.gson;
|
||||||
import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS;
|
import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS;
|
||||||
import static com.google.gson.Gson.DEFAULT_DATE_PATTERN;
|
import static com.google.gson.Gson.DEFAULT_DATE_PATTERN;
|
||||||
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML;
|
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML;
|
||||||
|
import static com.google.gson.Gson.DEFAULT_FORMATTING_STYLE;
|
||||||
import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE;
|
import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE;
|
||||||
import static com.google.gson.Gson.DEFAULT_LENIENT;
|
import static com.google.gson.Gson.DEFAULT_LENIENT;
|
||||||
import static com.google.gson.Gson.DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
import static com.google.gson.Gson.DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
||||||
import static com.google.gson.Gson.DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
import static com.google.gson.Gson.DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
||||||
import static com.google.gson.Gson.DEFAULT_FORMATTING_STYLE;
|
|
||||||
import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS;
|
import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS;
|
||||||
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
|
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
|
||||||
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
|
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
|
||||||
|
@ -41,11 +41,11 @@ import com.google.gson.stream.JsonReader;
|
||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
@ -104,7 +104,7 @@ public final class GsonBuilder {
|
||||||
private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
|
private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
|
||||||
private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
||||||
private ToNumberStrategy numberToNumberStrategy = DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
private ToNumberStrategy numberToNumberStrategy = DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
||||||
private final LinkedList<ReflectionAccessFilter> reflectionFilters = new LinkedList<>();
|
private final ArrayDeque<ReflectionAccessFilter> reflectionFilters = new ArrayDeque<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a GsonBuilder instance that can be used to build Gson with various configuration
|
* Creates a GsonBuilder instance that can be used to build Gson with various configuration
|
||||||
|
|
|
@ -342,8 +342,8 @@ public final class ConstructorConstructor {
|
||||||
return (T) new TreeMap<>();
|
return (T) new TreeMap<>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom(
|
} else if (type instanceof ParameterizedType && !String.class.isAssignableFrom(
|
||||||
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) {
|
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType())) {
|
||||||
return new ObjectConstructor<T>() {
|
return new ObjectConstructor<T>() {
|
||||||
@Override public T construct() {
|
@Override public T construct() {
|
||||||
return (T) new LinkedHashMap<>();
|
return (T) new LinkedHashMap<>();
|
||||||
|
|
|
@ -98,7 +98,7 @@ public final class LazilyParsedNumber extends Number {
|
||||||
}
|
}
|
||||||
if (obj instanceof LazilyParsedNumber) {
|
if (obj instanceof LazilyParsedNumber) {
|
||||||
LazilyParsedNumber other = (LazilyParsedNumber) obj;
|
LazilyParsedNumber other = (LazilyParsedNumber) obj;
|
||||||
return value == other.value || value.equals(other.value);
|
return value.equals(other.value);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -304,6 +304,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ReferenceEquality")
|
||||||
private void replaceInParent(Node<K, V> node, Node<K, V> replacement) {
|
private void replaceInParent(Node<K, V> node, Node<K, V> replacement) {
|
||||||
Node<K, V> parent = node.parent;
|
Node<K, V> parent = node.parent;
|
||||||
node.parent = null;
|
node.parent = null;
|
||||||
|
@ -315,7 +316,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
|
||||||
if (parent.left == node) {
|
if (parent.left == node) {
|
||||||
parent.left = replacement;
|
parent.left = replacement;
|
||||||
} else {
|
} else {
|
||||||
assert (parent.right == node);
|
assert parent.right == node;
|
||||||
parent.right = replacement;
|
parent.right = replacement;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -559,10 +560,13 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
|
||||||
LinkedTreeMapIterator() {
|
LinkedTreeMapIterator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public final boolean hasNext() {
|
@Override
|
||||||
|
@SuppressWarnings("ReferenceEquality")
|
||||||
|
public final boolean hasNext() {
|
||||||
return next != header;
|
return next != header;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ReferenceEquality")
|
||||||
final Node<K, V> nextNode() {
|
final Node<K, V> nextNode() {
|
||||||
Node<K, V> e = next;
|
Node<K, V> e = next;
|
||||||
if (e == header) {
|
if (e == header) {
|
||||||
|
|
|
@ -37,7 +37,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||||
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
||||||
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||||
Type type = typeToken.getType();
|
Type type = typeToken.getType();
|
||||||
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
|
if (!(type instanceof GenericArrayType || (type instanceof Class && ((Class<?>) type).isArray()))) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -158,7 +158,7 @@ public final class TreeTypeAdapter<T> extends SerializationDelegatingTypeAdapter
|
||||||
@Override
|
@Override
|
||||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||||
boolean matches = exactType != null
|
boolean matches = exactType != null
|
||||||
? exactType.equals(type) || matchRawType && exactType.getType() == type.getRawType()
|
? exactType.equals(type) || (matchRawType && exactType.getType() == type.getRawType())
|
||||||
: hierarchyType.isAssignableFrom(type.getRawType());
|
: hierarchyType.isAssignableFrom(type.getRawType());
|
||||||
return matches
|
return matches
|
||||||
? new TreeTypeAdapter<>((JsonSerializer<T>) serializer,
|
? new TreeTypeAdapter<>((JsonSerializer<T>) serializer,
|
||||||
|
|
|
@ -123,7 +123,7 @@ public final class TypeAdapters {
|
||||||
@Override public void write(JsonWriter out, BitSet src) throws IOException {
|
@Override public void write(JsonWriter out, BitSet src) throws IOException {
|
||||||
out.beginArray();
|
out.beginArray();
|
||||||
for (int i = 0, length = src.length(); i < length; i++) {
|
for (int i = 0, length = src.length(); i < length; i++) {
|
||||||
int value = (src.get(i)) ? 1 : 0;
|
int value = src.get(i) ? 1 : 0;
|
||||||
out.value(value);
|
out.value(value);
|
||||||
}
|
}
|
||||||
out.endArray();
|
out.endArray();
|
||||||
|
@ -883,7 +883,7 @@ public final class TypeAdapters {
|
||||||
});
|
});
|
||||||
for (Field constantField : constantFields) {
|
for (Field constantField : constantFields) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
T constant = (T)(constantField.get(null));
|
T constant = (T) constantField.get(null);
|
||||||
String name = constant.name();
|
String name = constant.name();
|
||||||
String toStringVal = constant.toString();
|
String toStringVal = constant.toString();
|
||||||
|
|
||||||
|
|
|
@ -637,7 +637,7 @@ public class JsonWriter implements Closeable, Flushable {
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
int size = stackSize;
|
int size = stackSize;
|
||||||
if (size > 1 || size == 1 && stack[size - 1] != NONEMPTY_DOCUMENT) {
|
if (size > 1 || (size == 1 && stack[size - 1] != NONEMPTY_DOCUMENT)) {
|
||||||
throw new IOException("Incomplete document");
|
throw new IOException("Incomplete document");
|
||||||
}
|
}
|
||||||
stackSize = 0;
|
stackSize = 0;
|
||||||
|
|
|
@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||||
import static com.google.common.truth.Truth.assertWithMessage;
|
import static com.google.common.truth.Truth.assertWithMessage;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.google.gson.common.MoreAsserts;
|
import com.google.gson.common.MoreAsserts;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -35,12 +36,11 @@ public final class JsonArrayTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("TruthSelfEquals")
|
|
||||||
public void testEqualsNonEmptyArray() {
|
public void testEqualsNonEmptyArray() {
|
||||||
JsonArray a = new JsonArray();
|
JsonArray a = new JsonArray();
|
||||||
JsonArray b = new JsonArray();
|
JsonArray b = new JsonArray();
|
||||||
|
|
||||||
assertThat(a).isEqualTo(a);
|
new EqualsTester().addEqualityGroup(a).testEquals();
|
||||||
|
|
||||||
a.add(new JsonObject());
|
a.add(new JsonObject());
|
||||||
assertThat(a.equals(b)).isFalse();
|
assertThat(a.equals(b)).isFalse();
|
||||||
|
|
|
@ -19,6 +19,7 @@ package com.google.gson;
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import com.google.common.testing.EqualsTester;
|
||||||
import com.google.gson.common.MoreAsserts;
|
import com.google.gson.common.MoreAsserts;
|
||||||
import java.util.AbstractMap.SimpleEntry;
|
import java.util.AbstractMap.SimpleEntry;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
|
@ -163,12 +164,11 @@ public class JsonObjectTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("TruthSelfEquals")
|
|
||||||
public void testEqualsNonEmptyObject() {
|
public void testEqualsNonEmptyObject() {
|
||||||
JsonObject a = new JsonObject();
|
JsonObject a = new JsonObject();
|
||||||
JsonObject b = new JsonObject();
|
JsonObject b = new JsonObject();
|
||||||
|
|
||||||
assertThat(a).isEqualTo(a);
|
new EqualsTester().addEqualityGroup(a).testEquals();
|
||||||
|
|
||||||
a.add("foo", new JsonObject());
|
a.add("foo", new JsonObject());
|
||||||
assertThat(a.equals(b)).isFalse();
|
assertThat(a.equals(b)).isFalse();
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -98,7 +98,7 @@
|
||||||
<compilerArgs>
|
<compilerArgs>
|
||||||
<!-- Args related to Error Prone, see: https://errorprone.info/docs/installation#maven -->
|
<!-- Args related to Error Prone, see: https://errorprone.info/docs/installation#maven -->
|
||||||
<arg>-XDcompilePolicy=simple</arg>
|
<arg>-XDcompilePolicy=simple</arg>
|
||||||
<arg>-Xplugin:ErrorProne</arg>
|
<arg>-Xplugin:ErrorProne -XepExcludedPaths:.*gson/src/test.*|.*extras/src/test.*|.*proto.*</arg>
|
||||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
|
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
|
||||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
|
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
|
||||||
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
|
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user