Skip to content

Commit dc20b75

Browse files
authored
Fix error prone warns (#2320)
* Adds `@SuppressWarnings("NarrowingCompoundAssignment")` * Adds `@SuppressWarnings("TypeParameterUnusedInFormals")` * Adds `@SuppressWarnings("JavaUtilDate")` * Adds a limit to `String.split()` * Add `error_prone_annotations` to the `pom.xml` * Adds `@InlineMe(...)` to deprecated methods * Adds `@SuppressWarnings("ImmutableEnumChecker")` * Adds `@SuppressWarnings("ModifiedButNotUsed")` * Adds `@SuppressWarnings("MixedMutabilityReturnType")` * Removes an unused import * Adds `requires` to `module-info.java` * Adds ErrorProne `link` into `pom.xml` * Remove unused imports Removed from: - ParseBenchmark * Adds `@SuppressWarnings("EqualsGetClass")` * Excludes from `proto` just the generated code. Replaces `.*proto.*` with `.*/generated-test-sources/protobuf/.*` in such way will be excluded just the generated code and not the whole `proto` directory * Removes an unused variable Removes the `descriptor` variable because is unused. * Fixes the `BadImport` warn into `ProtosWithAnnotationsTest` * Fixes the `BadImport` warns into `ProtosWithAnnotationsTest` * Enables ErrorProne in `gson/src/test.*` Removes the `gson/src/test.*` path from the `-XepExcludedPaths` parameter of the ErrorProne plugin * Fixes `UnusedVariable` warns This commit fix all `UnusedVariable` warns given by ErrorProne in the `gson/src/test.*` path. Some field is annotated with `@Keep` that means is used by reflection. In some other case the record is annotated with `@SuppressWarnings("unused")` * Fixes `JavaUtilDate` warns This commit fix all `JavaUtilDate` warns given by ErrorProne in the `gson/src/test.*` path. Classes/Methods are annotated with `@SuppressWarnings("JavaUtilDate")` because it's not possible use differente Date API. * Fixes `EqualsGetClass` warns This commit fix all `EqualsGetClass` warns given by ErrorProne in the `gson/src/test.*` path. I have rewrite the `equals()` methods to use `instanceof` * Replaces pattern matching for instanceof with casts * Fixes `JdkObsolete` warns This commit fix all `JdkObsolete` warns given by ErrorProne in the `gson/src/test.*` path. In some cases I have replaced the obsolete JDK classes with the newest alternatives. In other cases, I have added the `@SuppressWarnings("JdkObsolete")` * Fixes `ClassCanBeStatic` warns This commit fix all `ClassCanBeStatic` warns given by ErrorProne in the `gson/src/test.*` path. I have added the `static` keyword, or I have added `@SuppressWarnings("ClassCanBeStatic")` * Fixes `UndefinedEquals` warns This commit fix all `UndefinedEquals` warns given by ErrorProne in the `gson/src/test.*` path. I have added `@SuppressWarnings("UndefinedEquals")` or fixed the asserts Note: In this commit I have also renamed a test from `testRawCollectionDeserializationNotAlllowed` to `testRawCollectionDeserializationNotAllowed` * Fixes `GetClassOnEnum` warns This commit fix all `GetClassOnEnum` warns given by ErrorProne in the `gson/src/test.*` path. I have replaced the `.getClass()` with `.getDeclaringClass()` * Fixes `ImmutableEnumChecker` warns This commit fix all `ImmutableEnumChecker` warns given by ErrorProne in the `gson/src/test.*` path. I have added the `final` keyword, or I have added the `@SuppressWarnings("ImmutableEnumChecker")` annotation * Fixes `StaticAssignmentOfThrowable` warns This commit fix all `StaticAssignmentOfThrowable` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `AssertionFailureIgnored` warns This commit fix all `AssertionFailureIgnored` warns given by ErrorProne in the `gson/src/test.*` path. I have added the `@SuppressWarnings("AssertionFailureIgnored")` annotation * Fixes `ModifiedButNotUsed` warns This commit fix all `ModifiedButNotUsed` warns given by ErrorProne in the `gson/src/test.*` path. I have added the `@SuppressWarnings("ModifiedButNotUsed")` annotation * Fixes `MissingSummary` warns This commit fix all `MissingSummary` warns given by ErrorProne in the `gson/src/test.*` path. I have remove the Javadoc `@author` * Fixes `FloatingPointLiteralPrecision` warns This commit fix all `FloatingPointLiteralPrecision` warns given by ErrorProne in the `gson/src/test.*` path. I have added the `@SuppressWarnings("FloatingPointLiteralPrecision")` annotation * Fixes `StringSplitter` warns This commit fix all `StringSplitter` warns given by ErrorProne in the `gson/src/test.*` path. I have replaced the `String.split(...)` with `Splitter` * Fixes `EmptyCatch` warns This commit fix all `EmptyCatch` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `UnicodeEscape` warns This commit fix all `UnicodeEscape` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `EmptyBlockTag` warns This commit fix all `EmptyBlockTag` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `LongFloatConversion` warns This commit fix all `LongFloatConversion` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `LongDoubleConversion` warns This commit fix all `LongDoubleConversion` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `TruthAssertExpected` warns This commit fix all `TruthAssertExpected` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `UnusedMethod` warns This commit fix all `UnusedMethod` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `UnusedTypeParameter` warns This commit fix all `UnusedTypeParameter` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `CatchFail` warns This commit fix all `CatchFail` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `MathAbsoluteNegative` warns This commit fix all `MathAbsoluteNegative` warns given by ErrorProne in the `gson/src/test.*` path. * Fixes `LoopOverCharArray` warns This commit fix all `LoopOverCharArray` warns given by ErrorProne in the `gson/src/test.*` path. `toCharArray` allocates a new array, using `charAt` is more efficient * Fixes `HidingField` warns This commit fix all `HidingField` warns given by ErrorProne in the `gson/src/test.*` path. * Implements code review feedback * Implements code review feedback This commit implements some other code review feedback * Enable ErrorProne in `extra` Thi commit removes the `.*extras/src/test.*` path from the `-XepExcludedPaths` parameter of ErrorProne. * Fix the `JavaUtilDate` warns This commit fix all `JavaUtilDate` warns given by ErrorProne in the `extras/src/test.*` path. * Implements code review feedback * Removes redundant new-line * Implements code review feedback * Adds `JDK11` to run test with `--release 11` * Revert "Adds `JDK11` to run test with `--release 11`" This reverts commit a7cca38.
1 parent bfbbd0d commit dc20b75

File tree

60 files changed

+225
-193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+225
-193
lines changed

extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.TimeZone;
3030
import org.junit.Test;
3131

32+
@SuppressWarnings("JavaUtilDate")
3233
public final class UtcDateTypeAdapterTest {
3334
private final Gson gson = new GsonBuilder()
3435
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())

gson/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
<version>31.1-jre</version>
5656
<scope>test</scope>
5757
</dependency>
58+
<dependency>
59+
<groupId>com.google.errorprone</groupId>
60+
<artifactId>error_prone_annotations</artifactId>
61+
<version>2.18.0</version>
62+
<optional>true</optional>
63+
</dependency>
5864
</dependencies>
5965

6066
<build>

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException
10111011
* @see #fromJson(String, Class)
10121012
* @see #fromJson(String, TypeToken)
10131013
*/
1014-
@SuppressWarnings("unchecked")
1014+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
10151015
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
10161016
return (T) fromJson(json, TypeToken.get(typeOfT));
10171017
}
@@ -1104,7 +1104,7 @@ public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException
11041104
* @see #fromJson(Reader, Class)
11051105
* @see #fromJson(Reader, TypeToken)
11061106
*/
1107-
@SuppressWarnings("unchecked")
1107+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
11081108
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
11091109
return (T) fromJson(json, TypeToken.get(typeOfT));
11101110
}
@@ -1183,7 +1183,7 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
11831183
* @see #fromJson(Reader, Type)
11841184
* @see #fromJson(JsonReader, TypeToken)
11851185
*/
1186-
@SuppressWarnings("unchecked")
1186+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
11871187
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
11881188
return (T) fromJson(reader, TypeToken.get(typeOfT));
11891189
}
@@ -1297,7 +1297,7 @@ public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxExce
12971297
* @see #fromJson(JsonElement, Class)
12981298
* @see #fromJson(JsonElement, TypeToken)
12991299
*/
1300-
@SuppressWarnings("unchecked")
1300+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
13011301
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
13021302
return (T) fromJson(json, TypeToken.get(typeOfT));
13031303
}

gson/src/main/java/com/google/gson/JsonDeserializationContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ public interface JsonDeserializationContext {
4040
* @return An object of type typeOfT.
4141
* @throws JsonParseException if the parse tree does not contain expected data.
4242
*/
43+
@SuppressWarnings("TypeParameterUnusedInFormals")
4344
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
4445
}

gson/src/main/java/com/google/gson/JsonParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.gson;
1717

18+
import com.google.errorprone.annotations.InlineMe;
1819
import com.google.gson.internal.Streams;
1920
import com.google.gson.stream.JsonReader;
2021
import com.google.gson.stream.JsonToken;
@@ -111,18 +112,21 @@ public static JsonElement parseReader(JsonReader reader)
111112

112113
/** @deprecated Use {@link JsonParser#parseString} */
113114
@Deprecated
115+
@InlineMe(replacement = "JsonParser.parseString(json)", imports = "com.google.gson.JsonParser")
114116
public JsonElement parse(String json) throws JsonSyntaxException {
115117
return parseString(json);
116118
}
117119

118120
/** @deprecated Use {@link JsonParser#parseReader(Reader)} */
119121
@Deprecated
122+
@InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser")
120123
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
121124
return parseReader(json);
122125
}
123126

124127
/** @deprecated Use {@link JsonParser#parseReader(JsonReader)} */
125128
@Deprecated
129+
@InlineMe(replacement = "JsonParser.parseReader(json)", imports = "com.google.gson.JsonParser")
126130
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException {
127131
return parseReader(json);
128132
}

gson/src/main/java/com/google/gson/internal/JavaVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int getMajorJavaVersion(String javaVersion) {
4545
// Parses both legacy 1.8 style and newer 9.0.4 style
4646
private static int parseDotted(String javaVersion) {
4747
try {
48-
String[] parts = javaVersion.split("[._]");
48+
String[] parts = javaVersion.split("[._]", 3);
4949
int firstVer = Integer.parseInt(parts[0]);
5050
if (firstVer == 1 && parts.length > 1) {
5151
return Integer.parseInt(parts[1]);

gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private boolean includeField(Field f, boolean serialize) {
8181
}
8282

8383
/** first element holds the default name */
84+
@SuppressWarnings("MixedMutabilityReturnType")
8485
private List<String> getFieldNames(Field f) {
8586
SerializedName annotation = f.getAnnotation(SerializedName.class);
8687
if (annotation == null) {

gson/src/main/java/com/google/gson/internal/bind/TreeTypeAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ private final class GsonContextImpl implements JsonSerializationContext, JsonDes
174174
@Override public JsonElement serialize(Object src, Type typeOfSrc) {
175175
return gson.toJsonTree(src, typeOfSrc);
176176
}
177-
@SuppressWarnings("unchecked")
178-
@Override public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
177+
@Override
178+
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
179+
public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
179180
return gson.fromJson(json, typeOfT);
180181
}
181182
}

gson/src/main/java/com/google/gson/internal/sql/SqlDateTypeAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* this class state. DateFormat isn't thread safe either, so this class has
3737
* to synchronize its read and write methods.
3838
*/
39+
@SuppressWarnings("JavaUtilDate")
3940
final class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
4041
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
4142
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal

gson/src/main/java/com/google/gson/internal/sql/SqlTimeTypeAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* this class state. DateFormat isn't thread safe either, so this class has
3838
* to synchronize its read and write methods.
3939
*/
40+
@SuppressWarnings("JavaUtilDate")
4041
final class SqlTimeTypeAdapter extends TypeAdapter<Time> {
4142
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
4243
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal

gson/src/main/java/com/google/gson/internal/sql/SqlTimestampTypeAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.sql.Timestamp;
2828
import java.util.Date;
2929

30+
@SuppressWarnings("JavaUtilDate")
3031
class SqlTimestampTypeAdapter extends TypeAdapter<Timestamp> {
3132
static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
3233
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal

gson/src/main/java/com/google/gson/internal/sql/SqlTypesSupport.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* it is {@code false} all other constants will be {@code null} and
3434
* there will be no support for {@code java.sql} types.
3535
*/
36+
@SuppressWarnings("JavaUtilDate")
3637
public final class SqlTypesSupport {
3738
/**
3839
* {@code true} if {@code java.sql} types are supported,

gson/src/main/java/com/google/gson/stream/JsonReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ private char readEscapeCharacter() throws IOException {
16031603
throw syntaxError("Unterminated escape sequence");
16041604
}
16051605
// Equivalent to Integer.parseInt(stringPool.get(buffer, pos, 4), 16);
1606-
char result = 0;
1606+
int result = 0;
16071607
for (int i = pos, end = i + 4; i < end; i++) {
16081608
char c = buffer[i];
16091609
result <<= 4;
@@ -1618,7 +1618,7 @@ private char readEscapeCharacter() throws IOException {
16181618
}
16191619
}
16201620
pos += 4;
1621-
return result;
1621+
return (char) result;
16221622

16231623
case 't':
16241624
return '\t';

gson/src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
exports com.google.gson.reflect;
2525
exports com.google.gson.stream;
2626

27+
// Dependency on Error Prone Annotations
28+
requires static com.google.errorprone.annotations;
29+
2730
// Optional dependency on java.sql
2831
requires static java.sql;
2932

gson/src/test/java/com/google/gson/CommentsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.Test;
2424

2525
/**
26+
* Tests that by default Gson accepts several forms of comments.
27+
*
2628
* @author Jesse Wilson
2729
*/
2830
public final class CommentsTest {

gson/src/test/java/com/google/gson/InnerClassExclusionStrategyTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void testIncludeStaticNestedClassField() throws Exception {
5656
assertThat(excluder.excludeField(f, true)).isFalse();
5757
}
5858

59+
@SuppressWarnings("ClassCanBeStatic")
5960
class InnerClass {
6061
}
6162

gson/src/test/java/com/google/gson/JsonArrayTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.junit.Test;
2727

2828
/**
29+
* Tests handling of JSON arrays.
30+
*
2931
* @author Jesse Wilson
3032
*/
3133
public final class JsonArrayTest {

gson/src/test/java/com/google/gson/JsonNullTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.junit.Test;
2323

2424
/**
25+
* Tests handling of JSON nulls.
26+
*
2527
* @author Jesse Wilson
2628
*/
2729
public final class JsonNullTest {

gson/src/test/java/com/google/gson/ObjectTypeAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void testDeserializeDeeplyNestedObjects() throws IOException {
111111
assertThat(actualTimes).isEqualTo(times);
112112
}
113113

114-
@SuppressWarnings("unused")
114+
@SuppressWarnings({"unused", "ClassCanBeStatic"})
115115
private class RuntimeType {
116116
Object a = 5;
117117
Object b = Arrays.asList(1, 2, null);

gson/src/test/java/com/google/gson/OverrideCoreTypeAdaptersTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.junit.Test;
2626

2727
/**
28+
* Tests handling of Core Type Adapters
29+
*
2830
* @author Jesse Wilson
2931
*/
3032
public class OverrideCoreTypeAdaptersTest {

gson/src/test/java/com/google/gson/ParameterizedTypeFixtures.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson;
1818

19+
import com.google.common.base.Objects;
1920
import com.google.gson.internal.$Gson$Types;
2021

2122
import com.google.gson.internal.Primitives;
@@ -35,7 +36,7 @@
3536
*/
3637
public class ParameterizedTypeFixtures {
3738

38-
public static class MyParameterizedType<T> {
39+
public static final class MyParameterizedType<T> {
3940
public final T value;
4041
public MyParameterizedType(T value) {
4142
this.value = value;
@@ -80,27 +81,16 @@ public int hashCode() {
8081
return value == null ? 0 : value.hashCode();
8182
}
8283

83-
@SuppressWarnings("unchecked")
8484
@Override
8585
public boolean equals(Object obj) {
8686
if (this == obj) {
8787
return true;
8888
}
89-
if (obj == null) {
90-
return false;
91-
}
92-
if (getClass() != obj.getClass()) {
93-
return false;
94-
}
95-
MyParameterizedType<T> other = (MyParameterizedType<T>) obj;
96-
if (value == null) {
97-
if (other.value != null) {
98-
return false;
99-
}
100-
} else if (!value.equals(other.value)) {
89+
if (!(obj instanceof MyParameterizedType<?>)) {
10190
return false;
10291
}
103-
return true;
92+
MyParameterizedType<?> that = (MyParameterizedType<?>) obj;
93+
return Objects.equal(getValue(), that.getValue());
10494
}
10595
}
10696

@@ -112,8 +102,6 @@ public static class MyParameterizedTypeInstanceCreator<T>
112102
* This means that the fields of the same objects will be overwritten by Gson.
113103
* This is usually fine in tests since there we deserialize just once, but quite
114104
* dangerous in practice.
115-
*
116-
* @param instanceOfT
117105
*/
118106
public MyParameterizedTypeInstanceCreator(T instanceOfT) {
119107
this.instanceOfT = instanceOfT;

gson/src/test/java/com/google/gson/VersionExclusionStrategyTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.errorprone.annotations.Keep;
2122
import com.google.gson.annotations.Since;
2223
import com.google.gson.annotations.Until;
2324
import com.google.gson.internal.Excluder;
@@ -75,13 +76,15 @@ public void testOlderVersion() throws Exception {
7576
private static class MockClassSince {
7677

7778
@Since(VERSION)
79+
@Keep
7880
public final int someField = 0;
7981
}
8082

8183
@Until(VERSION)
8284
private static class MockClassUntil {
8385

8486
@Until(VERSION)
87+
@Keep
8588
public final int someField = 0;
8689
}
8790

@@ -91,6 +94,7 @@ private static class MockClassBoth {
9194

9295
@Since(VERSION)
9396
@Until(VERSION + 2)
97+
@Keep
9498
public final int someField = 0;
9599
}
96100
}

gson/src/test/java/com/google/gson/common/TestTypes.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gson.common;
1818

19+
import com.google.common.base.Objects;
1920
import com.google.gson.JsonDeserializationContext;
2021
import com.google.gson.JsonDeserializer;
2122
import com.google.gson.JsonElement;
@@ -146,26 +147,18 @@ public int hashCode() {
146147
}
147148

148149
@Override
149-
public boolean equals(Object obj) {
150-
if (this == obj)
150+
public boolean equals(Object o) {
151+
if (this == o) {
151152
return true;
152-
if (obj == null)
153-
return false;
154-
if (getClass() != obj.getClass())
155-
return false;
156-
BagOfPrimitives other = (BagOfPrimitives) obj;
157-
if (booleanValue != other.booleanValue)
158-
return false;
159-
if (intValue != other.intValue)
160-
return false;
161-
if (longValue != other.longValue)
162-
return false;
163-
if (stringValue == null) {
164-
if (other.stringValue != null)
165-
return false;
166-
} else if (!stringValue.equals(other.stringValue))
153+
}
154+
if (!(o instanceof BagOfPrimitives)) {
167155
return false;
168-
return true;
156+
}
157+
BagOfPrimitives that = (BagOfPrimitives) o;
158+
return longValue == that.longValue
159+
&& getIntValue() == that.getIntValue()
160+
&& booleanValue == that.booleanValue
161+
&& Objects.equal(stringValue, that.stringValue);
169162
}
170163

171164
@Override
@@ -233,7 +226,7 @@ public static class ClassWithNoFields {
233226
// Nothing here..
234227
@Override
235228
public boolean equals(Object other) {
236-
return other.getClass() == ClassWithNoFields.class;
229+
return other instanceof ClassWithNoFields;
237230
}
238231
}
239232

0 commit comments

Comments
 (0)