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:
Maicol 2023-02-15 14:18:43 +01:00 committed by GitHub
parent 19f54ee6ed
commit 2658aca66a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 33 additions and 21 deletions

View File

@ -31,10 +31,10 @@ import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
@ -79,6 +79,7 @@ public final class GraphAdapterBuilder {
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
private final Map<Type, InstanceCreator<?>> instanceCreators;
@SuppressWarnings("ThreadLocalUsage")
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();
Factory(Map<Type, InstanceCreator<?>> instanceCreators) {
@ -240,7 +241,7 @@ public final class GraphAdapterBuilder {
* The queue of elements to write during serialization. Unused during
* 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

View File

@ -46,7 +46,7 @@ public final class InterceptorFactory implements TypeAdapterFactory {
public InterceptorAdapter(TypeAdapter<T> delegate, Intercept intercept) {
try {
this.delegate = delegate;
this.postDeserializer = intercept.postDeserialize().newInstance();
this.postDeserializer = intercept.postDeserialize().getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -49,6 +49,12 @@
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<version>31.1-jre</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -166,6 +166,7 @@ public final class Gson {
* with the type token provided to {@code getAdapter} as key and either
* {@code FutureTypeAdapter} or a regular {@code TypeAdapter} as value.
*/
@SuppressWarnings("ThreadLocalUsage")
private final ThreadLocal<Map<TypeToken<?>, TypeAdapter<?>>> threadLocalAdapterResults = new ThreadLocal<>();
private final ConcurrentMap<TypeToken<?>, TypeAdapter<?>> typeTokenCache = new ConcurrentHashMap<>();

View File

@ -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_DATE_PATTERN;
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_LENIENT;
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_FORMATTING_STYLE;
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_USE_JDK_UNSAFE;
@ -41,11 +41,11 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -104,7 +104,7 @@ public final class GsonBuilder {
private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_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

View File

@ -342,8 +342,8 @@ public final class ConstructorConstructor {
return (T) new TreeMap<>();
}
};
} else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom(
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) {
} else if (type instanceof ParameterizedType && !String.class.isAssignableFrom(
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType())) {
return new ObjectConstructor<T>() {
@Override public T construct() {
return (T) new LinkedHashMap<>();

View File

@ -98,7 +98,7 @@ public final class LazilyParsedNumber extends Number {
}
if (obj instanceof LazilyParsedNumber) {
LazilyParsedNumber other = (LazilyParsedNumber) obj;
return value == other.value || value.equals(other.value);
return value.equals(other.value);
}
return false;
}

View File

@ -304,6 +304,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
return node;
}
@SuppressWarnings("ReferenceEquality")
private void replaceInParent(Node<K, V> node, Node<K, V> replacement) {
Node<K, V> parent = node.parent;
node.parent = null;
@ -315,7 +316,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
if (parent.left == node) {
parent.left = replacement;
} else {
assert (parent.right == node);
assert parent.right == node;
parent.right = replacement;
}
} else {
@ -559,10 +560,13 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
LinkedTreeMapIterator() {
}
@Override public final boolean hasNext() {
@Override
@SuppressWarnings("ReferenceEquality")
public final boolean hasNext() {
return next != header;
}
@SuppressWarnings("ReferenceEquality")
final Node<K, V> nextNode() {
Node<K, V> e = next;
if (e == header) {

View File

@ -37,7 +37,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
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;
}

View File

@ -158,7 +158,7 @@ public final class TreeTypeAdapter<T> extends SerializationDelegatingTypeAdapter
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
boolean matches = exactType != null
? exactType.equals(type) || matchRawType && exactType.getType() == type.getRawType()
? exactType.equals(type) || (matchRawType && exactType.getType() == type.getRawType())
: hierarchyType.isAssignableFrom(type.getRawType());
return matches
? new TreeTypeAdapter<>((JsonSerializer<T>) serializer,

View File

@ -123,7 +123,7 @@ public final class TypeAdapters {
@Override public void write(JsonWriter out, BitSet src) throws IOException {
out.beginArray();
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.endArray();
@ -883,7 +883,7 @@ public final class TypeAdapters {
});
for (Field constantField : constantFields) {
@SuppressWarnings("unchecked")
T constant = (T)(constantField.get(null));
T constant = (T) constantField.get(null);
String name = constant.name();
String toStringVal = constant.toString();

View File

@ -637,7 +637,7 @@ public class JsonWriter implements Closeable, Flushable {
out.close();
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");
}
stackSize = 0;

View File

@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.fail;
import com.google.common.testing.EqualsTester;
import com.google.gson.common.MoreAsserts;
import java.math.BigInteger;
import org.junit.Test;
@ -35,12 +36,11 @@ public final class JsonArrayTest {
}
@Test
@SuppressWarnings("TruthSelfEquals")
public void testEqualsNonEmptyArray() {
JsonArray a = new JsonArray();
JsonArray b = new JsonArray();
assertThat(a).isEqualTo(a);
new EqualsTester().addEqualityGroup(a).testEquals();
a.add(new JsonObject());
assertThat(a.equals(b)).isFalse();

View File

@ -19,6 +19,7 @@ package com.google.gson;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.common.testing.EqualsTester;
import com.google.gson.common.MoreAsserts;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayDeque;
@ -163,12 +164,11 @@ public class JsonObjectTest {
}
@Test
@SuppressWarnings("TruthSelfEquals")
public void testEqualsNonEmptyObject() {
JsonObject a = new JsonObject();
JsonObject b = new JsonObject();
assertThat(a).isEqualTo(a);
new EqualsTester().addEqualityGroup(a).testEquals();
a.add("foo", new JsonObject());
assertThat(a.equals(b)).isFalse();

View File

@ -98,7 +98,7 @@
<compilerArgs>
<!-- Args related to Error Prone, see: https://errorprone.info/docs/installation#maven -->
<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.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>