Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail Maven build on compiler warnings; remove some warning suppressions #2183

Merged
merged 6 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
* Writes a graph of objects as a list of named nodes.
*/
// TODO: proper documentation
@SuppressWarnings("rawtypes")
public final class GraphAdapterBuilder {
private final Map<Type, InstanceCreator<?>> instanceCreators;
private final ConstructorConstructor constructorConstructor;
Expand Down Expand Up @@ -78,7 +77,7 @@ public void registerOn(GsonBuilder gsonBuilder) {
}
}

static class Factory implements TypeAdapterFactory, InstanceCreator {
static class Factory implements TypeAdapterFactory, InstanceCreator<Object> {
private final Map<Type, InstanceCreator<?>> instanceCreators;
private final ThreadLocal<Graph> graphThreadLocal = new ThreadLocal<>();

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

/**
* The instance currently being deserialized. Used as a backdoor between
* the graph traversal (which needs to know instances) and instance creators
* which create them.
*/
private Element nextCreate;
private Element<Object> nextCreate;

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

@SuppressWarnings("unchecked")
void read(Graph graph) throws IOException {
if (graph.nextCreate != null) {
throw new IllegalStateException("Unexpected recursive call to read() for " + id);
}
graph.nextCreate = this;
graph.nextCreate = (Element<Object>) this;
value = typeAdapter.fromJsonTree(element);
if (value == null) {
throw new IllegalStateException("non-null value deserialized to null: " + element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@

package com.google.gson.typeadapters;

import javax.annotation.PostConstruct;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import junit.framework.TestCase;

public class PostConstructAdapterFactoryTest extends TestCase {
public void test() throws Exception {
Expand Down Expand Up @@ -55,6 +52,7 @@ public void testList() {
assertEquals(sandwiches, sandwichesFromJson);
}

@SuppressWarnings("overrides") // for missing hashCode() override
static class Sandwich {
public String bread;
public String cheese;
Expand Down Expand Up @@ -89,6 +87,7 @@ public boolean equals(Object o) {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Inderjeet Singh
*/
@SuppressWarnings("serial") // ignore warning about missing serialVersionUID
public final class LazilyParsedNumber extends Number {
private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
* <p>This implementation was derived from Android 4.1's TreeMap class.
*/
@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>() {
Expand Down Expand Up @@ -504,10 +505,9 @@ static final class Node<K, V> implements Entry<K, V> {
return oldValue;
}

@SuppressWarnings("rawtypes")
@Override public boolean equals(Object o) {
if (o instanceof Entry) {
Entry other = (Entry) o;
Entry<?, ?> other = (Entry<?, ?>) o;
return (key == null ? other.getKey() == null : key.equals(other.getKey()))
&& (value == null ? other.getValue() == null : value.equals(other.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gso
typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
} else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
JsonSerializer<?> serializer = instance instanceof JsonSerializer
? (JsonSerializer) instance
? (JsonSerializer<?>) instance
: null;
JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
? (JsonDeserializer) instance
? (JsonDeserializer<?>) instance
: null;
typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null, nullSafe);
nullSafe = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.google.gson.internal.bind;

import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

final class TypeAdapterRuntimeTypeWrapper<T> extends TypeAdapter<T> {
private final Gson context;
Expand All @@ -41,7 +40,6 @@ public T read(JsonReader in) throws IOException {
return delegate.read(in);
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, T value) throws IOException {
// Order of preference for choosing type adapters
Expand All @@ -50,10 +48,11 @@ public void write(JsonWriter out, T value) throws IOException {
// Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type)
// Fourth preference: reflective type adapter for the declared type

TypeAdapter chosen = delegate;
TypeAdapter<T> chosen = delegate;
Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value);
if (runtimeType != type) {
TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
@SuppressWarnings("unchecked")
TypeAdapter<T> runtimeTypeAdapter = (TypeAdapter<T>) context.getAdapter(TypeToken.get(runtimeType));
if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
// The user registered a type adapter for the runtime type, so we will use that
chosen = runtimeTypeAdapter;
Expand Down
4 changes: 4 additions & 0 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ public JsonToken peek() throws IOException {
}
}

@SuppressWarnings("fallthrough")
int doPeek() throws IOException {
int peekStack = stack[stackSize - 1];
if (peekStack == JsonScope.EMPTY_ARRAY) {
Expand Down Expand Up @@ -749,6 +750,7 @@ private int peekNumber() throws IOException {
}
}

@SuppressWarnings("fallthrough")
private boolean isLiteral(char c) throws IOException {
switch (c) {
case '/':
Expand Down Expand Up @@ -1129,6 +1131,7 @@ private void skipQuotedValue(char quote) throws IOException {
throw syntaxError("Unterminated string");
}

@SuppressWarnings("fallthrough")
private void skipUnquotedValue() throws IOException {
do {
int i = 0;
Expand Down Expand Up @@ -1539,6 +1542,7 @@ public String getPath() {
* @throws NumberFormatException if any unicode escape sequences are
* malformed.
*/
@SuppressWarnings("fallthrough")
private char readEscapeCharacter() throws IOException {
if (pos == limit && !fillBuffer(1)) {
throw syntaxError("Unterminated escape sequence");
Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/com/google/gson/stream/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class JsonWriter implements Closeable, Flushable {
static {
REPLACEMENT_CHARS = new String[128];
for (int i = 0; i <= 0x1f; i++) {
REPLACEMENT_CHARS[i] = String.format("\\u%04x", (int) i);
REPLACEMENT_CHARS[i] = String.format("\\u%04x", i);
}
REPLACEMENT_CHARS['"'] = "\\\"";
REPLACEMENT_CHARS['\\'] = "\\\\";
Expand Down
16 changes: 8 additions & 8 deletions gson/src/test/java/com/google/gson/common/TestTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.google.gson.common;

import java.lang.reflect.Type;
import java.util.Collection;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -28,6 +25,8 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Type;
import java.util.Collection;

/**
* Types used for testing JSON serialization and deserialization
Expand All @@ -36,7 +35,7 @@
* @author Joel Leitch
*/
public class TestTypes {

public static class Base {
public static final String BASE_NAME = Base.class.getSimpleName();
public static final String BASE_FIELD_KEY = "baseName";
Expand Down Expand Up @@ -76,7 +75,7 @@ public ClassWithBaseCollectionField(Collection<Base> base) {
}

public static class BaseSerializer implements JsonSerializer<Base> {
public static final String NAME = BaseSerializer.class.getSimpleName();
public static final String NAME = BaseSerializer.class.getSimpleName();
@Override
public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
Expand All @@ -85,13 +84,13 @@ public JsonElement serialize(Base src, Type typeOfSrc, JsonSerializationContext
}
}
public static class SubSerializer implements JsonSerializer<Sub> {
public static final String NAME = SubSerializer.class.getSimpleName();
public static final String NAME = SubSerializer.class.getSimpleName();
@Override
public JsonElement serialize(Sub src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty(Base.SERIALIZER_KEY, NAME);
return obj;
}
}
}

public static class StringWrapper {
Expand Down Expand Up @@ -228,6 +227,7 @@ public String getExpectedJson() {
}
}

@SuppressWarnings("overrides") // for missing hashCode() override
public static class ClassWithNoFields {
// Nothing here..
@Override
Expand Down Expand Up @@ -271,7 +271,7 @@ public void appendFields(StringBuilder sb) {
}

public static class ClassWithTransientFields<T> {
public transient T transientT;
public transient T transientT;
public final transient long transientLongValue;
private final long[] longValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public void testSingleStringArrayDeserialization() throws Exception {
assertEquals("hello", arrayType[0]);
}

@SuppressWarnings("unchecked")
public void testArrayOfCollectionSerialization() throws Exception {
StringBuilder sb = new StringBuilder("[");
int arraySize = 3;

Type typeToSerialize = new TypeToken<Collection<Integer>[]>() {}.getType();
@SuppressWarnings({"rawtypes", "unchecked"})
Collection<Integer>[] arrayOfCollection = new ArrayList[arraySize];
for (int i = 0; i < arraySize; ++i) {
int startValue = (3 * i) + 1;
Expand Down
42 changes: 19 additions & 23 deletions gson/src/test/java/com/google/gson/functional/CollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

package com.google.gson.functional;

import static org.junit.Assert.assertArrayEquals;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -30,18 +40,7 @@
import java.util.Set;
import java.util.Stack;
import java.util.Vector;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.reflect.TypeToken;

import junit.framework.TestCase;
import static org.junit.Assert.assertArrayEquals;

/**
* Functional tests for Json serialization and deserialization of collections.
Expand Down Expand Up @@ -241,35 +240,33 @@ public void testRawCollectionOfIntegersSerialization() {
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
}

@SuppressWarnings("rawtypes")
public void testRawCollectionSerialization() {
public void testObjectCollectionSerialization() {
BagOfPrimitives bag1 = new BagOfPrimitives();
Collection target = Arrays.asList(bag1, bag1);
Collection<?> target = Arrays.asList(bag1, bag1, "test");
String json = gson.toJson(target);
assertTrue(json.contains(bag1.getExpectedJson()));
}

@SuppressWarnings("rawtypes")
public void testRawCollectionDeserializationNotAlllowed() {
String json = "[0,1,2,3,4,5,6,7,8,9]";
Collection integers = gson.fromJson(json, Collection.class);
Collection<?> integers = gson.fromJson(json, Collection.class);
// JsonReader converts numbers to double by default so we need a floating point comparison
assertEquals(Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0), integers);

json = "[\"Hello\", \"World\"]";
Collection strings = gson.fromJson(json, Collection.class);
Collection<?> strings = gson.fromJson(json, Collection.class);
assertTrue(strings.contains("Hello"));
assertTrue(strings.contains("World"));
}

@SuppressWarnings({"rawtypes", "unchecked"})
public void testRawCollectionOfBagOfPrimitivesNotAllowed() {
BagOfPrimitives bag = new BagOfPrimitives(10, 20, false, "stringValue");
String json = '[' + bag.getExpectedJson() + ',' + bag.getExpectedJson() + ']';
Collection target = gson.fromJson(json, Collection.class);
Collection<?> target = gson.fromJson(json, Collection.class);
assertEquals(2, target.size());
for (Object bag1 : target) {
// Gson 2.0 converts raw objects into maps
@SuppressWarnings("unchecked")
Map<String, Object> values = (Map<String, Object>) bag1;
assertTrue(values.containsValue(10.0));
assertTrue(values.containsValue(20.0));
Expand Down Expand Up @@ -324,7 +321,7 @@ public void testFieldIsArrayList() {
HasArrayListField copy = gson.fromJson("{\"longs\":[1,3]}", HasArrayListField.class);
assertEquals(Arrays.asList(1L, 3L), copy.longs);
}

public void testUserCollectionTypeAdapter() {
Type listOfString = new TypeToken<List<String>>() {}.getType();
Object stringListSerializer = new JsonSerializer<List<String>>() {
Expand All @@ -343,11 +340,10 @@ static class HasArrayListField {
ArrayList<Long> longs = new ArrayList<>();
}

@SuppressWarnings("rawtypes")
private static int[] toIntArray(Collection collection) {
private static int[] toIntArray(Collection<?> collection) {
int[] ints = new int[collection.size()];
int i = 0;
for (Iterator iterator = collection.iterator(); iterator.hasNext(); ++i) {
for (Iterator<?> iterator = collection.iterator(); iterator.hasNext(); ++i) {
Object obj = iterator.next();
if (obj instanceof Integer) {
ints[i] = ((Integer)obj).intValue();
Expand Down
Loading