Skip to content

Commit

Permalink
Fix Error Prone warnings
Browse files Browse the repository at this point in the history
Note that for the `final` variable warnings, it seems some of those variables
made `final` intentionally, without the compiler requiring it.
But for consistency I removed the `final` from them as well.
  • Loading branch information
Marcono1234 committed Sep 23, 2024
1 parent b09a31f commit 9ff1617
Show file tree
Hide file tree
Showing 39 changed files with 110 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public GraphAdapterBuilder() {
}

public GraphAdapterBuilder addType(Type type) {
final ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
ObjectConstructor<?> objectConstructor = constructorConstructor.get(TypeToken.get(type));
InstanceCreator<Object> instanceCreator =
new InstanceCreator<Object>() {
@Override
Expand Down Expand Up @@ -95,8 +95,8 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return null;
}

final TypeAdapter<T> typeAdapter = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
TypeAdapter<T> typeAdapter = gson.getDelegateAdapter(this, type);
TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
return null;
}

final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean equals(Object o) {
if (!(o instanceof Sandwich)) {
return false;
}
final Sandwich other = (Sandwich) o;
Sandwich other = (Sandwich) o;
if (this.bread == null ? other.bread != null : !this.bread.equals(other.bread)) {
return false;
}
Expand All @@ -115,7 +115,7 @@ public boolean equals(Object o) {
if (!(o instanceof MultipleSandwiches)) {
return false;
}
final MultipleSandwiches other = (MultipleSandwiches) o;
MultipleSandwiches other = (MultipleSandwiches) o;
if (this.sandwiches == null
? other.sandwiches != null
: !this.sandwiches.equals(other.sandwiches)) {
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public <T> TypeAdapter<T> getAdapter(Class<T> type) {
* public int numReads = 0;
* public int numWrites = 0;
* public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
* final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
* TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
* return new TypeAdapter<T>() {
* public void write(JsonWriter out, T value) throws IOException {
* ++numWrites;
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/TypeAdapterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
* }
*
* private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(
* final TypeAdapter<E> elementAdapter) {
* TypeAdapter<E> elementAdapter) {
* return new TypeAdapter<Multiset<E>>() {
* public void write(JsonWriter out, Multiset<E> value) throws IOException {
* if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ static String checkInstantiable(Class<?> c) {
}

public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) {
final Type type = typeToken.getType();
final Class<? super T> rawType = typeToken.getRawType();
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();

// first try an instance creator

@SuppressWarnings("unchecked") // types must agree
final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
if (typeCreator != null) {
return new ObjectConstructor<T>() {
@Override
Expand All @@ -113,7 +113,7 @@ public T construct() {

// Next try raw type match for instance creators
@SuppressWarnings("unchecked") // types must agree
final InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType);
InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType);
if (rawTypeCreator != null) {
return new ObjectConstructor<T>() {
@Override
Expand Down Expand Up @@ -145,7 +145,7 @@ public T construct() {

// Check whether type is instantiable; otherwise ReflectionAccessFilter recommendation
// of adjusting filter suggested below is irrelevant since it would not solve the problem
final String exceptionMessage = checkInstantiable(rawType);
String exceptionMessage = checkInstantiable(rawType);
if (exceptionMessage != null) {
return new ObjectConstructor<T>() {
@Override
Expand All @@ -161,7 +161,7 @@ public T construct() {
// finally try unsafe
return newUnsafeAllocator(rawType);
} else {
final String message =
String message =
"Unable to create instance of "
+ rawType
+ "; ReflectionAccessFilter does not permit using reflection or Unsafe. Register an"
Expand All @@ -181,7 +181,7 @@ public T construct() {
* constructor.
*/
private static <T> ObjectConstructor<T> newSpecialCollectionConstructor(
final Type type, Class<? super T> rawType) {
Type type, Class<? super T> rawType) {
if (EnumSet.class.isAssignableFrom(rawType)) {
return new ObjectConstructor<T>() {
@Override
Expand Down Expand Up @@ -233,7 +233,7 @@ private static <T> ObjectConstructor<T> newDefaultConstructor(
return null;
}

final Constructor<? super T> constructor;
Constructor<? super T> constructor;
try {
constructor = rawType.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
Expand All @@ -249,7 +249,7 @@ private static <T> ObjectConstructor<T> newDefaultConstructor(
|| Modifier.isPublic(constructor.getModifiers())));

if (!canAccess) {
final String message =
String message =
"Unable to invoke no-args constructor of "
+ rawType
+ ";"
Expand All @@ -267,7 +267,7 @@ public T construct() {
// Only try to make accessible if allowed; in all other cases checks above should
// have verified that constructor is accessible
if (filterResult == FilterResult.ALLOW) {
final String exceptionMessage = ReflectionHelper.tryMakeAccessible(constructor);
String exceptionMessage = ReflectionHelper.tryMakeAccessible(constructor);
if (exceptionMessage != null) {
/*
* Create ObjectConstructor which throws exception.
Expand Down Expand Up @@ -323,7 +323,7 @@ public T construct() {
/** Constructors for common interface types like Map and List and their subtypes. */
@SuppressWarnings("unchecked") // use runtime checks to guarantee that 'T' is what it is
private static <T> ObjectConstructor<T> newDefaultImplementationConstructor(
final Type type, Class<? super T> rawType) {
Type type, Class<? super T> rawType) {

/*
* IMPORTANT: Must only create instances for classes with public no-args constructor.
Expand Down Expand Up @@ -409,7 +409,7 @@ public T construct() {
return null;
}

private <T> ObjectConstructor<T> newUnsafeAllocator(final Class<? super T> rawType) {
private <T> ObjectConstructor<T> newUnsafeAllocator(Class<? super T> rawType) {
if (useJdkUnsafe) {
return new ObjectConstructor<T>() {
@Override
Expand Down Expand Up @@ -445,7 +445,7 @@ public T construct() {
}

// Explicit final variable to allow usage in the anonymous class below
final String exceptionMessageF = exceptionMessage;
String exceptionMessageF = exceptionMessage;

return new ObjectConstructor<T>() {
@Override
Expand Down
6 changes: 3 additions & 3 deletions gson/src/main/java/com/google/gson/internal/Excluder.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public Excluder withExclusionStrategy(
}

@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();

final boolean skipSerialize = excludeClass(rawType, true);
final boolean skipDeserialize = excludeClass(rawType, false);
boolean skipSerialize = excludeClass(rawType, true);
boolean skipDeserialize = excludeClass(rawType, false);

if (!skipSerialize && !skipDeserialize) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@
@SuppressWarnings("serial") // ignore warning about missing serialVersionUID
public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Serializable {
@SuppressWarnings({"unchecked", "rawtypes"}) // to avoid Comparable<Comparable<Comparable<...>>>
private static final Comparator<Comparable> NATURAL_ORDER =
new Comparator<Comparable>() {
@Override
public int compare(Comparable a, Comparable b) {
return a.compareTo(b);
}
};
private static final Comparator<Comparable> NATURAL_ORDER = Comparator.naturalOrder();

private final Comparator<? super K> comparator;
private final boolean allowNullValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private abstract static class AccessChecker {
// TODO: Ideally should use Multi-Release JAR for this version specific code
if (JavaVersion.isJava9OrLater()) {
try {
final Method canAccessMethod =
Method canAccessMethod =
AccessibleObject.class.getDeclaredMethod("canAccess", Object.class);
accessChecker =
new AccessChecker() {
Expand Down
10 changes: 5 additions & 5 deletions gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private static UnsafeAllocator create() {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
final Object unsafe = f.get(null);
final Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
Object unsafe = f.get(null);
Method allocateInstance = unsafeClass.getMethod("allocateInstance", Class.class);
return new UnsafeAllocator() {
@Override
@SuppressWarnings("unchecked")
Expand All @@ -77,8 +77,8 @@ public <T> T newInstance(Class<T> c) throws Exception {
Method getConstructorId =
ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
getConstructorId.setAccessible(true);
final int constructorId = (Integer) getConstructorId.invoke(null, Object.class);
final Method newInstance =
int constructorId = (Integer) getConstructorId.invoke(null, Object.class);
Method newInstance =
ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, int.class);
newInstance.setAccessible(true);
return new UnsafeAllocator() {
Expand All @@ -99,7 +99,7 @@ public <T> T newInstance(Class<T> c) throws Exception {
// Class<?> instantiationClass, Class<?> constructorClass);
// }
try {
final Method newInstance =
Method newInstance =
ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
newInstance.setAccessible(true);
return new UnsafeAllocator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
private final Map<String, T> stringToConstant = new HashMap<>();
private final Map<T, String> constantToName = new HashMap<>();

private EnumTypeAdapter(final Class<T> classOfT) {
private EnumTypeAdapter(Class<T> classOfT) {
try {
// Uses reflection to find enum constants to work around name mismatches for obfuscated
// classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ public int nextInt() throws IOException {
}

JsonElement nextJsonElement() throws IOException {
final JsonToken peeked = peek();
JsonToken peeked = peek();
if (peeked == JsonToken.NAME
|| peeked == JsonToken.END_ARRAY
|| peeked == JsonToken.END_OBJECT
|| peeked == JsonToken.END_DOCUMENT) {
throw new IllegalStateException("Unexpected " + peeked + " when reading a JsonElement.");
}
final JsonElement element = (JsonElement) peekStack();
JsonElement element = (JsonElement) peekStack();
skipValue();
return element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private NumberTypeAdapter(ToNumberStrategy toNumberStrategy) {
}

private static TypeAdapterFactory newFactory(ToNumberStrategy toNumberStrategy) {
final NumberTypeAdapter adapter = new NumberTypeAdapter(toNumberStrategy);
NumberTypeAdapter adapter = new NumberTypeAdapter(toNumberStrategy);
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ObjectTypeAdapter(Gson gson, ToNumberStrategy toNumberStrategy) {
this.toNumberStrategy = toNumberStrategy;
}

private static TypeAdapterFactory newFactory(final ToNumberStrategy toNumberStrategy) {
private static TypeAdapterFactory newFactory(ToNumberStrategy toNumberStrategy) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private List<String> getFieldNames(Field f) {
}

@Override
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<? super T> raw = type.getRawType();

if (!Object.class.isAssignableFrom(raw)) {
Expand Down Expand Up @@ -175,18 +175,18 @@ private static <M extends AccessibleObject & Member> void checkAccessible(
}

private BoundField createBoundField(
final Gson context,
final Field field,
final Method accessor,
final String serializedName,
final TypeToken<?> fieldType,
final boolean serialize,
final boolean blockInaccessible) {
Gson context,
Field field,
Method accessor,
String serializedName,
TypeToken<?> fieldType,
boolean serialize,
boolean blockInaccessible) {

final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());
boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());

int modifiers = field.getModifiers();
final boolean isStaticFinalField = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);
boolean isStaticFinalField = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers);

JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
TypeAdapter<?> mapped = null;
Expand All @@ -196,14 +196,14 @@ private BoundField createBoundField(
jsonAdapterFactory.getTypeAdapter(
constructorConstructor, context, fieldType, annotation, false);
}
final boolean jsonAdapterPresent = mapped != null;
boolean jsonAdapterPresent = mapped != null;
if (mapped == null) {
mapped = context.getAdapter(fieldType);
}

@SuppressWarnings("unchecked")
final TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) mapped;
final TypeAdapter<Object> writeTypeAdapter;
TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) mapped;
TypeAdapter<Object> writeTypeAdapter;
if (serialize) {
writeTypeAdapter =
jsonAdapterPresent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ public void write(JsonWriter out, Locale value) throws IOException {

@SuppressWarnings("TypeParameterNaming")
public static <TT> TypeAdapterFactory newFactory(
final TypeToken<TT> type, final TypeAdapter<TT> typeAdapter) {
TypeToken<TT> type, TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
@Override
Expand All @@ -826,8 +826,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
}

@SuppressWarnings("TypeParameterNaming")
public static <TT> TypeAdapterFactory newFactory(
final Class<TT> type, final TypeAdapter<TT> typeAdapter) {
public static <TT> TypeAdapterFactory newFactory(Class<TT> type, TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
@Override
Expand All @@ -844,7 +843,7 @@ public String toString() {

@SuppressWarnings("TypeParameterNaming")
public static <TT> TypeAdapterFactory newFactory(
final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
Class<TT> unboxed, Class<TT> boxed, TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
@Override
Expand All @@ -868,9 +867,7 @@ public String toString() {

@SuppressWarnings("TypeParameterNaming")
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(
final Class<TT> base,
final Class<? extends TT> sub,
final TypeAdapter<? super TT> typeAdapter) {
Class<TT> base, Class<? extends TT> sub, TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
@Override
Expand All @@ -897,12 +894,12 @@ public String toString() {
* that the deserialized type matches the type requested.
*/
public static <T1> TypeAdapterFactory newTypeHierarchyFactory(
final Class<T1> clazz, final TypeAdapter<T1> typeAdapter) {
Class<T1> clazz, TypeAdapter<T1> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
public <T2> TypeAdapter<T2> create(Gson gson, TypeToken<T2> typeToken) {
final Class<? super T2> requestedType = typeToken.getRawType();
Class<? super T2> requestedType = typeToken.getRawType();
if (!clazz.isAssignableFrom(requestedType)) {
return null;
}
Expand Down
Loading

0 comments on commit 9ff1617

Please sign in to comment.