Skip to content

Commit f7a164d

Browse files
authored
Fail Maven build on compiler warnings; remove some warning suppressions (#2183)
* Fail Maven build on compiler warnings; remove some warning suppressions * Fix compiler warnings causing failure for newer JDK * Improve placement of "raw" and "unchecked" warning suppressions * Adjust javac documentation link * Fix compilation error on newer JDKs
1 parent 325f37c commit f7a164d

30 files changed

Lines changed: 164 additions & 168 deletions

extras/src/main/java/com/google/gson/graph/GraphAdapterBuilder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
* Writes a graph of objects as a list of named nodes.
4343
*/
4444
// TODO: proper documentation
45-
@SuppressWarnings("rawtypes")
4645
public final class GraphAdapterBuilder {
4746
private final Map<Type, InstanceCreator<?>> instanceCreators;
4847
private final ConstructorConstructor constructorConstructor;
@@ -78,7 +77,7 @@ public void registerOn(GsonBuilder gsonBuilder) {
7877
}
7978
}
8079

81-
static class Factory implements TypeAdapterFactory, InstanceCreator {
80+
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
8281
private final Map<Type, InstanceCreator<?>> instanceCreators;
8382
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();
8483

@@ -215,7 +214,6 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
215214
* <p>Gson should only ever call this method when we're expecting it to;
216215
* that is only when we've called back into Gson to deserialize a tree.
217216
*/
218-
@SuppressWarnings("unchecked")
219217
@Override
220218
public Object createInstance(Type type) {
221219
Graph graph = graphThreadLocal.get();
@@ -242,14 +240,14 @@ static class Graph {
242240
* The queue of elements to write during serialization. Unused during
243241
* deserialization.
244242
*/
245-
private final Queue<Element> queue = new LinkedList<>();
243+
private final Queue<Element<?>> queue = new LinkedList<>();
246244

247245
/**
248246
* The instance currently being deserialized. Used as a backdoor between
249247
* the graph traversal (which needs to know instances) and instance creators
250248
* which create them.
251249
*/
252-
private Element nextCreate;
250+
private Element<Object> nextCreate;
253251

254252
private Graph(Map<Object, Element<?>> map) {
255253
this.map = map;
@@ -299,11 +297,12 @@ void write(JsonWriter out) throws IOException {
299297
typeAdapter.write(out, value);
300298
}
301299

300+
@SuppressWarnings("unchecked")
302301
void read(Graph graph) throws IOException {
303302
if (graph.nextCreate != null) {
304303
throw new IllegalStateException("Unexpected recursive call to read() for " + id);
305304
}
306-
graph.nextCreate = this;
305+
graph.nextCreate = (Element<Object>) this;
307306
value = typeAdapter.fromJsonTree(element);
308307
if (value == null) {
309308
throw new IllegalStateException("non-null value deserialized to null: " + element);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
package com.google.gson.typeadapters;
1818

19-
import javax.annotation.PostConstruct;
20-
2119
import com.google.gson.Gson;
2220
import com.google.gson.GsonBuilder;
23-
24-
import junit.framework.TestCase;
25-
2621
import java.util.Arrays;
2722
import java.util.List;
23+
import javax.annotation.PostConstruct;
24+
import junit.framework.TestCase;
2825

2926
public class PostConstructAdapterFactoryTest extends TestCase {
3027
public void test() throws Exception {
@@ -55,6 +52,7 @@ public void testList() {
5552
assertEquals(sandwiches, sandwichesFromJson);
5653
}
5754

55+
@SuppressWarnings("overrides") // for missing hashCode() override
5856
static class Sandwich {
5957
public String bread;
6058
public String cheese;
@@ -89,6 +87,7 @@ public boolean equals(Object o) {
8987
}
9088
}
9189

90+
@SuppressWarnings("overrides") // for missing hashCode() override
9291
static class MultipleSandwiches {
9392
public List<Sandwich> sandwiches;
9493

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,13 @@ private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(final TypeAda
503503
* @throws IllegalArgumentException if this GSON cannot serialize and
504504
* deserialize {@code type}.
505505
*/
506-
@SuppressWarnings("unchecked")
507506
public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
508507
Objects.requireNonNull(type, "type must not be null");
509508
TypeAdapter<?> cached = typeTokenCache.get(type);
510509
if (cached != null) {
511-
return (TypeAdapter<T>) cached;
510+
@SuppressWarnings("unchecked")
511+
TypeAdapter<T> adapter = (TypeAdapter<T>) cached;
512+
return adapter;
512513
}
513514

514515
Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
@@ -520,6 +521,7 @@ public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
520521
}
521522

522523
// the key and value type parameters always agree
524+
@SuppressWarnings("unchecked")
523525
FutureTypeAdapter<T> ongoingCall = (FutureTypeAdapter<T>) threadCalls.get(type);
524526
if (ongoingCall != null) {
525527
return ongoingCall;
@@ -532,6 +534,7 @@ public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
532534
for (TypeAdapterFactory factory : factories) {
533535
TypeAdapter<T> candidate = factory.create(this, type);
534536
if (candidate != null) {
537+
@SuppressWarnings("unchecked")
535538
TypeAdapter<T> existingAdapter = (TypeAdapter<T>) typeTokenCache.putIfAbsent(type, candidate);
536539
// If other thread concurrently added adapter prefer that one instead
537540
if (existingAdapter != null) {
@@ -780,17 +783,17 @@ public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOE
780783
*
781784
* @throws JsonIOException if there was a problem writing to the writer
782785
*/
783-
@SuppressWarnings("unchecked")
784786
public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOException {
785-
TypeAdapter<?> adapter = getAdapter(TypeToken.get(typeOfSrc));
787+
@SuppressWarnings("unchecked")
788+
TypeAdapter<Object> adapter = (TypeAdapter<Object>) getAdapter(TypeToken.get(typeOfSrc));
786789
boolean oldLenient = writer.isLenient();
787790
writer.setLenient(true);
788791
boolean oldHtmlSafe = writer.isHtmlSafe();
789792
writer.setHtmlSafe(htmlSafe);
790793
boolean oldSerializeNulls = writer.getSerializeNulls();
791794
writer.setSerializeNulls(serializeNulls);
792795
try {
793-
((TypeAdapter<Object>) adapter).write(writer, src);
796+
adapter.write(writer, src);
794797
} catch (IOException e) {
795798
throw new JsonIOException(e);
796799
} catch (AssertionError e) {
@@ -957,12 +960,12 @@ public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException
957960
* @throws JsonParseException if json is not a valid representation for an object of type typeOfT
958961
* @throws JsonSyntaxException if json is not a valid representation for an object of type
959962
*/
960-
@SuppressWarnings("unchecked")
961963
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
962964
if (json == null) {
963965
return null;
964966
}
965967
StringReader reader = new StringReader(json);
968+
@SuppressWarnings("unchecked")
966969
T target = (T) fromJson(reader, typeOfT);
967970
return target;
968971
}
@@ -1017,9 +1020,9 @@ public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException
10171020
* @throws JsonSyntaxException if json is not a valid representation for an object of type
10181021
* @since 1.2
10191022
*/
1020-
@SuppressWarnings("unchecked")
10211023
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
10221024
JsonReader jsonReader = newJsonReader(json);
1025+
@SuppressWarnings("unchecked")
10231026
T object = (T) fromJson(jsonReader, typeOfT);
10241027
assertFullConsumption(object, jsonReader);
10251028
return object;
@@ -1052,14 +1055,14 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
10521055
* @throws JsonIOException if there was a problem writing to the Reader
10531056
* @throws JsonSyntaxException if json is not a valid representation for an object of type
10541057
*/
1055-
@SuppressWarnings("unchecked")
10561058
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
10571059
boolean isEmpty = true;
10581060
boolean oldLenient = reader.isLenient();
10591061
reader.setLenient(true);
10601062
try {
10611063
reader.peek();
10621064
isEmpty = false;
1065+
@SuppressWarnings("unchecked")
10631066
TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT);
10641067
TypeAdapter<T> typeAdapter = getAdapter(typeToken);
10651068
T object = typeAdapter.read(reader);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,22 +541,23 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
541541
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
542542
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
543543
*/
544-
@SuppressWarnings({"unchecked", "rawtypes"})
545544
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
546545
Objects.requireNonNull(type);
547546
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
548547
|| typeAdapter instanceof JsonDeserializer<?>
549548
|| typeAdapter instanceof InstanceCreator<?>
550549
|| typeAdapter instanceof TypeAdapter<?>);
551550
if (typeAdapter instanceof InstanceCreator<?>) {
552-
instanceCreators.put(type, (InstanceCreator) typeAdapter);
551+
instanceCreators.put(type, (InstanceCreator<?>) typeAdapter);
553552
}
554553
if (typeAdapter instanceof JsonSerializer<?> || typeAdapter instanceof JsonDeserializer<?>) {
555554
TypeToken<?> typeToken = TypeToken.get(type);
556555
factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(typeToken, typeAdapter));
557556
}
558557
if (typeAdapter instanceof TypeAdapter<?>) {
559-
factories.add(TypeAdapters.newFactory(TypeToken.get(type), (TypeAdapter)typeAdapter));
558+
@SuppressWarnings({"unchecked", "rawtypes"})
559+
TypeAdapterFactory factory = TypeAdapters.newFactory(TypeToken.get(type), (TypeAdapter)typeAdapter);
560+
factories.add(factory);
560561
}
561562
return this;
562563
}
@@ -589,7 +590,6 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
589590
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
590591
* @since 1.7
591592
*/
592-
@SuppressWarnings({"unchecked", "rawtypes"})
593593
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
594594
Objects.requireNonNull(baseType);
595595
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@@ -599,7 +599,9 @@ public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAd
599599
hierarchyFactories.add(TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter));
600600
}
601601
if (typeAdapter instanceof TypeAdapter<?>) {
602-
factories.add(TypeAdapters.newTypeHierarchyFactory(baseType, (TypeAdapter)typeAdapter));
602+
@SuppressWarnings({"unchecked", "rawtypes"})
603+
TypeAdapterFactory factory = TypeAdapters.newTypeHierarchyFactory(baseType, (TypeAdapter)typeAdapter);
604+
factories.add(factory);
603605
}
604606
return this;
605607
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
* @author Inderjeet Singh
2828
*/
29+
@SuppressWarnings("serial") // ignore warning about missing serialVersionUID
2930
public final class LazilyParsedNumber extends Number {
3031
private final String value;
3132

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*
3939
* <p>This implementation was derived from Android 4.1's TreeMap class.
4040
*/
41+
@SuppressWarnings("serial") // ignore warning about missing serialVersionUID
4142
public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Serializable {
4243
@SuppressWarnings({ "unchecked", "rawtypes" }) // to avoid Comparable<Comparable<Comparable<...>>>
4344
private static final Comparator<Comparable> NATURAL_ORDER = new Comparator<Comparable>() {
@@ -504,10 +505,9 @@ static final class Node<K, V> implements Entry<K, V> {
504505
return oldValue;
505506
}
506507

507-
@SuppressWarnings("rawtypes")
508508
@Override public boolean equals(Object o) {
509509
if (o instanceof Entry) {
510-
Entry other = (Entry) o;
510+
Entry<?, ?> other = (Entry<?, ?>) o;
511511
return (key == null ? other.getKey() == null : key.equals(other.getKey()))
512512
&& (value == null ? other.getValue() == null : value.equals(other.getValue()));
513513
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
*/
3636
public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
3737
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
38-
@SuppressWarnings({"unchecked", "rawtypes"})
3938
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
4039
Type type = typeToken.getType();
4140
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
@@ -44,8 +43,11 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
4443

4544
Type componentType = $Gson$Types.getArrayComponentType(type);
4645
TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
47-
return new ArrayTypeAdapter(
46+
47+
@SuppressWarnings({"unchecked", "rawtypes"})
48+
TypeAdapter<T> arrayAdapter = new ArrayTypeAdapter(
4849
gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
50+
return arrayAdapter;
4951
}
5052
};
5153

@@ -89,7 +91,6 @@ public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class
8991
}
9092
}
9193

92-
@SuppressWarnings("unchecked")
9394
@Override public void write(JsonWriter out, Object array) throws IOException {
9495
if (array == null) {
9596
out.nullValue();
@@ -98,6 +99,7 @@ public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class
9899

99100
out.beginArray();
100101
for (int i = 0, length = Array.getLength(array); i < length; i++) {
102+
@SuppressWarnings("unchecked")
101103
E value = (E) Array.get(array, i);
102104
componentTypeAdapter.write(out, value);
103105
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public JsonAdapterAnnotationTypeAdapterFactory(ConstructorConstructor constructo
3838
this.constructorConstructor = constructorConstructor;
3939
}
4040

41-
@SuppressWarnings("unchecked")
41+
@SuppressWarnings("unchecked") // this is not safe; requires that user has specified correct adapter class for @JsonAdapter
4242
@Override
4343
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
4444
Class<? super T> rawType = targetType.getRawType();
@@ -49,7 +49,6 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
4949
return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
5050
}
5151

52-
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
5352
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
5453
TypeToken<?> type, JsonAdapter annotation) {
5554
Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();
@@ -62,12 +61,16 @@ TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gso
6261
typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
6362
} else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
6463
JsonSerializer<?> serializer = instance instanceof JsonSerializer
65-
? (JsonSerializer) instance
64+
? (JsonSerializer<?>) instance
6665
: null;
6766
JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
68-
? (JsonDeserializer) instance
67+
? (JsonDeserializer<?>) instance
6968
: null;
70-
typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null, nullSafe);
69+
70+
@SuppressWarnings({ "unchecked", "rawtypes" })
71+
TypeAdapter<?> tempAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null, nullSafe);
72+
typeAdapter = tempAdapter;
73+
7174
nullSafe = false;
7275
} else {
7376
throw new IllegalArgumentException("Invalid attempt to bind an instance of "

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ private Object readTerminal(JsonReader in, JsonToken peeked) throws IOException
166166
}
167167
}
168168

169-
@SuppressWarnings("unchecked")
170169
@Override public void write(JsonWriter out, Object value) throws IOException {
171170
if (value == null) {
172171
out.nullValue();
173172
return;
174173
}
175174

175+
@SuppressWarnings("unchecked")
176176
TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
177177
if (typeAdapter instanceof ObjectTypeAdapter) {
178178
out.beginObject();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,19 @@ private ReflectiveTypeAdapterFactory.BoundField createBoundField(
126126
final TypeToken<?> fieldType, boolean serialize, boolean deserialize,
127127
final boolean blockInaccessible) {
128128
final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());
129-
// special casing primitives here saves ~5% on Android...
130129
JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
131130
TypeAdapter<?> mapped = null;
132131
if (annotation != null) {
132+
// This is not safe; requires that user has specified correct adapter class for @JsonAdapter
133133
mapped = jsonAdapterFactory.getTypeAdapter(
134134
constructorConstructor, context, fieldType, annotation);
135135
}
136136
final boolean jsonAdapterPresent = mapped != null;
137137
if (mapped == null) mapped = context.getAdapter(fieldType);
138138

139-
final TypeAdapter<?> typeAdapter = mapped;
139+
@SuppressWarnings("unchecked")
140+
final TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) mapped;
140141
return new ReflectiveTypeAdapterFactory.BoundField(name, serialize, deserialize) {
141-
@SuppressWarnings({"unchecked", "rawtypes"}) // the type adapter and field type always agree
142142
@Override void write(JsonWriter writer, Object value)
143143
throws IOException, IllegalAccessException {
144144
if (!serialized) return;
@@ -152,8 +152,8 @@ private ReflectiveTypeAdapterFactory.BoundField createBoundField(
152152
return;
153153
}
154154
writer.name(name);
155-
TypeAdapter t = jsonAdapterPresent ? typeAdapter
156-
: new TypeAdapterRuntimeTypeWrapper(context, typeAdapter, fieldType.getType());
155+
TypeAdapter<Object> t = jsonAdapterPresent ? typeAdapter
156+
: new TypeAdapterRuntimeTypeWrapper<>(context, typeAdapter, fieldType.getType());
157157
t.write(writer, fieldValue);
158158
}
159159
@Override void read(JsonReader reader, Object value)

0 commit comments

Comments
 (0)