Add @CanIgnoreReturnValue as appropriate to Gson methods. (#2369)

This annotation indicates that return value of the annotated method does
not need to be used. If it is _not_ present on a non-void method, and if
Error Prone's `CheckReturnValue` is active, then calling the method
without using the result is an error. However, we are not enabling
`CheckReturnValue` by default here.

Also update some code that does ignore return values, so that the
returned value is used, if only by assigning it to an unused variable.
This commit is contained in:
Éamonn McManus 2023-04-10 10:50:25 -07:00 committed by GitHub
parent b43ccee889
commit c1da2d7070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 257 additions and 164 deletions

View File

@ -16,6 +16,7 @@
package com.google.gson.typeadapters; package com.google.gson.typeadapters;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -179,6 +180,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* Ensures that this factory will handle not just the given {@code baseType}, but any subtype * Ensures that this factory will handle not just the given {@code baseType}, but any subtype
* of that type. * of that type.
*/ */
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> recognizeSubtypes() { public RuntimeTypeAdapterFactory<T> recognizeSubtypes() {
this.recognizeSubtypes = true; this.recognizeSubtypes = true;
return this; return this;
@ -191,6 +193,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* @throws IllegalArgumentException if either {@code type} or {@code label} * @throws IllegalArgumentException if either {@code type} or {@code label}
* have already been registered on this type adapter. * have already been registered on this type adapter.
*/ */
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) { public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
if (type == null || label == null) { if (type == null || label == null) {
throw new NullPointerException(); throw new NullPointerException();
@ -210,6 +213,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* @throws IllegalArgumentException if either {@code type} or its simple name * @throws IllegalArgumentException if either {@code type} or its simple name
* have already been registered on this type adapter. * have already been registered on this type adapter.
*/ */
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) { public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName()); return registerSubtype(type, type.getSimpleName());
} }

View File

@ -129,8 +129,8 @@ public final class GraphAdapterBuilderTest {
@Test @Test
public void testSerializationWithMultipleTypes() { public void testSerializationWithMultipleTypes() {
Company google = new Company("Google"); Company google = new Company("Google");
new Employee("Jesse", google); Employee unused1 = new Employee("Jesse", google);
new Employee("Joel", google); Employee unused2 = new Employee("Joel", google);
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
new GraphAdapterBuilder() new GraphAdapterBuilder()

View File

@ -110,9 +110,9 @@ public final class InterceptorTest {
@Override public User read(JsonReader in) throws IOException { @Override public User read(JsonReader in) throws IOException {
in.beginObject(); in.beginObject();
in.nextName(); String unused1 = in.nextName();
String name = in.nextString(); String name = in.nextString();
in.nextName(); String unused2 = in.nextName();
String password = in.nextString(); String password = in.nextString();
in.endObject(); in.endObject();
return new User(name, password); return new User(name, password);

View File

@ -64,7 +64,7 @@ public final class UtcDateTypeAdapterTest {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter()) .registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
.create(); .create();
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class); Date unused = gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
} }
@Test @Test

View File

@ -1223,7 +1223,7 @@ public final class Gson {
boolean oldLenient = reader.isLenient(); boolean oldLenient = reader.isLenient();
reader.setLenient(true); reader.setLenient(true);
try { try {
reader.peek(); JsonToken unused = reader.peek();
isEmpty = false; isEmpty = false;
TypeAdapter<T> typeAdapter = getAdapter(typeOfT); TypeAdapter<T> typeAdapter = getAdapter(typeOfT);
return typeAdapter.read(reader); return typeAdapter.read(reader);

View File

@ -28,6 +28,7 @@ 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_SPECIALIZE_FLOAT_VALUES;
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE; import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.annotations.Since; import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until; import com.google.gson.annotations.Until;
import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.$Gson$Preconditions;
@ -159,6 +160,7 @@ public final class GsonBuilder {
* @see Since * @see Since
* @see Until * @see Until
*/ */
@CanIgnoreReturnValue
public GsonBuilder setVersion(double version) { public GsonBuilder setVersion(double version) {
if (Double.isNaN(version) || version < 0.0) { if (Double.isNaN(version) || version < 0.0) {
throw new IllegalArgumentException("Invalid version: " + version); throw new IllegalArgumentException("Invalid version: " + version);
@ -181,6 +183,7 @@ public final class GsonBuilder {
* {@link java.lang.reflect.Modifier#STATIC}. * {@link java.lang.reflect.Modifier#STATIC}.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder excludeFieldsWithModifiers(int... modifiers) { public GsonBuilder excludeFieldsWithModifiers(int... modifiers) {
Objects.requireNonNull(modifiers); Objects.requireNonNull(modifiers);
excluder = excluder.withModifiers(modifiers); excluder = excluder.withModifiers(modifiers);
@ -196,6 +199,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder generateNonExecutableJson() { public GsonBuilder generateNonExecutableJson() {
this.generateNonExecutableJson = true; this.generateNonExecutableJson = true;
return this; return this;
@ -210,6 +214,7 @@ public final class GsonBuilder {
* *
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder excludeFieldsWithoutExposeAnnotation() { public GsonBuilder excludeFieldsWithoutExposeAnnotation() {
excluder = excluder.excludeFieldsWithoutExposeAnnotation(); excluder = excluder.excludeFieldsWithoutExposeAnnotation();
return this; return this;
@ -222,6 +227,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder serializeNulls() { public GsonBuilder serializeNulls() {
this.serializeNulls = true; this.serializeNulls = true;
return this; return this;
@ -306,6 +312,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder enableComplexMapKeySerialization() { public GsonBuilder enableComplexMapKeySerialization() {
complexMapKeySerialization = true; complexMapKeySerialization = true;
return this; return this;
@ -330,6 +337,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableInnerClassSerialization() { public GsonBuilder disableInnerClassSerialization() {
excluder = excluder.disableInnerClassSerialization(); excluder = excluder.disableInnerClassSerialization();
return this; return this;
@ -343,6 +351,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) { public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy); this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy);
return this; return this;
@ -354,6 +363,7 @@ public final class GsonBuilder {
* *
* <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}. * <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}.
*/ */
@CanIgnoreReturnValue
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) { public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
return setFieldNamingStrategy(namingConvention); return setFieldNamingStrategy(namingConvention);
} }
@ -370,6 +380,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) { public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy); this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy);
return this; return this;
@ -383,6 +394,7 @@ public final class GsonBuilder {
* @see ToNumberPolicy#DOUBLE The default object-to-number strategy * @see ToNumberPolicy#DOUBLE The default object-to-number strategy
* @since 2.8.9 * @since 2.8.9
*/ */
@CanIgnoreReturnValue
public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) { public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) {
this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy); this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy);
return this; return this;
@ -396,6 +408,7 @@ public final class GsonBuilder {
* @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy * @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy
* @since 2.8.9 * @since 2.8.9
*/ */
@CanIgnoreReturnValue
public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) { public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) {
this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy); this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy);
return this; return this;
@ -427,6 +440,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.4 * @since 1.4
*/ */
@CanIgnoreReturnValue
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) { public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
Objects.requireNonNull(strategies); Objects.requireNonNull(strategies);
for (ExclusionStrategy strategy : strategies) { for (ExclusionStrategy strategy : strategies) {
@ -450,6 +464,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) { public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
Objects.requireNonNull(strategy); Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, true, false); excluder = excluder.withExclusionStrategy(strategy, true, false);
@ -471,6 +486,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) { public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
Objects.requireNonNull(strategy); Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, false, true); excluder = excluder.withExclusionStrategy(strategy, false, true);
@ -486,6 +502,7 @@ public final class GsonBuilder {
* *
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder setPrettyPrinting() { public GsonBuilder setPrettyPrinting() {
return setPrettyPrinting(FormattingStyle.DEFAULT); return setPrettyPrinting(FormattingStyle.DEFAULT);
} }
@ -500,6 +517,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since $next-version$ * @since $next-version$
*/ */
@CanIgnoreReturnValue
public GsonBuilder setPrettyPrinting(FormattingStyle formattingStyle) { public GsonBuilder setPrettyPrinting(FormattingStyle formattingStyle) {
this.formattingStyle = formattingStyle; this.formattingStyle = formattingStyle;
return this; return this;
@ -515,6 +533,7 @@ public final class GsonBuilder {
* @see JsonReader#setLenient(boolean) * @see JsonReader#setLenient(boolean)
* @see JsonWriter#setLenient(boolean) * @see JsonWriter#setLenient(boolean)
*/ */
@CanIgnoreReturnValue
public GsonBuilder setLenient() { public GsonBuilder setLenient() {
lenient = true; lenient = true;
return this; return this;
@ -527,6 +546,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableHtmlEscaping() { public GsonBuilder disableHtmlEscaping() {
this.escapeHtmlChars = false; this.escapeHtmlChars = false;
return this; return this;
@ -548,6 +568,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(String pattern) { public GsonBuilder setDateFormat(String pattern) {
// TODO(Joel): Make this fail fast if it is an invalid date format // TODO(Joel): Make this fail fast if it is an invalid date format
this.datePattern = pattern; this.datePattern = pattern;
@ -568,6 +589,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(int style) { public GsonBuilder setDateFormat(int style) {
this.dateStyle = style; this.dateStyle = style;
this.datePattern = null; this.datePattern = null;
@ -589,6 +611,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
this.dateStyle = dateStyle; this.dateStyle = dateStyle;
this.timeStyle = timeStyle; this.timeStyle = timeStyle;
@ -618,6 +641,7 @@ public final class GsonBuilder {
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Objects.requireNonNull(type); Objects.requireNonNull(type);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?> $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@ -651,6 +675,7 @@ public final class GsonBuilder {
* *
* @since 2.1 * @since 2.1
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
Objects.requireNonNull(factory); Objects.requireNonNull(factory);
factories.add(factory); factories.add(factory);
@ -671,6 +696,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.7 * @since 1.7
*/ */
@CanIgnoreReturnValue
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) { public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
Objects.requireNonNull(baseType); Objects.requireNonNull(baseType);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?> $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
@ -707,6 +733,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public GsonBuilder serializeSpecialFloatingPointValues() { public GsonBuilder serializeSpecialFloatingPointValues() {
this.serializeSpecialFloatingPointValues = true; this.serializeSpecialFloatingPointValues = true;
return this; return this;
@ -728,6 +755,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 2.9.0 * @since 2.9.0
*/ */
@CanIgnoreReturnValue
public GsonBuilder disableJdkUnsafe() { public GsonBuilder disableJdkUnsafe() {
this.useJdkUnsafe = false; this.useJdkUnsafe = false;
return this; return this;
@ -753,6 +781,7 @@ public final class GsonBuilder {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 2.9.1 * @since 2.9.1
*/ */
@CanIgnoreReturnValue
public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) { public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
reflectionFilters.addFirst(filter); reflectionFilters.addFirst(filter);

View File

@ -16,6 +16,7 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.NonNullElementWrapperList; import com.google.gson.internal.NonNullElementWrapperList;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
@ -145,6 +146,7 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @return the element previously at the specified position * @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/ */
@CanIgnoreReturnValue
public JsonElement set(int index, JsonElement element) { public JsonElement set(int index, JsonElement element) {
return elements.set(index, element == null ? JsonNull.INSTANCE : element); return elements.set(index, element == null ? JsonNull.INSTANCE : element);
} }
@ -157,6 +159,7 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @return true if this array contained the specified element, false otherwise * @return true if this array contained the specified element, false otherwise
* @since 2.3 * @since 2.3
*/ */
@CanIgnoreReturnValue
public boolean remove(JsonElement element) { public boolean remove(JsonElement element) {
return elements.remove(element); return elements.remove(element);
} }
@ -171,6 +174,7 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds * @throws IndexOutOfBoundsException if the specified index is outside the array bounds
* @since 2.3 * @since 2.3
*/ */
@CanIgnoreReturnValue
public JsonElement remove(int index) { public JsonElement remove(int index) {
return elements.remove(index); return elements.remove(index);
} }

View File

@ -16,6 +16,7 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.Streams; import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import java.io.IOException; import java.io.IOException;
@ -143,6 +144,7 @@ public abstract class JsonElement {
* @throws IllegalStateException if this element is of another type. * @throws IllegalStateException if this element is of another type.
* @since 1.2 * @since 1.2
*/ */
@CanIgnoreReturnValue
public JsonNull getAsJsonNull() { public JsonNull getAsJsonNull() {
if (isJsonNull()) { if (isJsonNull()) {
return (JsonNull) this; return (JsonNull) this;

View File

@ -16,6 +16,7 @@
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.LinkedTreeMap; import com.google.gson.internal.LinkedTreeMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -77,6 +78,7 @@ public final class JsonObject extends JsonElement {
* member with this name exists. * member with this name exists.
* @since 1.3 * @since 1.3
*/ */
@CanIgnoreReturnValue
public JsonElement remove(String property) { public JsonElement remove(String property) {
return members.remove(property); return members.remove(property);
} }

View File

@ -15,6 +15,7 @@
*/ */
package com.google.gson; package com.google.gson;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.internal.Streams; import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonToken;

View File

@ -17,6 +17,7 @@
package com.google.gson.internal; package com.google.gson.internal;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException; import java.io.IOException;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
@ -107,6 +108,7 @@ public final class LinkedTreeMap<K, V> extends AbstractMap<K, V> implements Seri
return findByObject(key) != null; return findByObject(key) != null;
} }
@CanIgnoreReturnValue
@Override public V put(K key, V value) { @Override public V put(K key, V value) {
if (key == null) { if (key == null) {
throw new NullPointerException("key == null"); throw new NullPointerException("key == null");

View File

@ -23,6 +23,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.bind.TypeAdapters; import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.MalformedJsonException; import com.google.gson.stream.MalformedJsonException;
import java.io.EOFException; import java.io.EOFException;
@ -44,7 +45,7 @@ public final class Streams {
public static JsonElement parse(JsonReader reader) throws JsonParseException { public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true; boolean isEmpty = true;
try { try {
reader.peek(); JsonToken unused = reader.peek();
isEmpty = false; isEmpty = false;
return TypeAdapters.JSON_ELEMENT.read(reader); return TypeAdapters.JSON_ELEMENT.read(reader);
} catch (EOFException e) { } catch (EOFException e) {

View File

@ -16,6 +16,7 @@
package com.google.gson.internal.bind; package com.google.gson.internal.bind;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
@ -153,6 +154,7 @@ public final class JsonTreeReader extends JsonReader {
return stack[stackSize - 1]; return stack[stackSize - 1];
} }
@CanIgnoreReturnValue
private Object popStack() { private Object popStack() {
Object result = stack[--stackSize]; Object result = stack[--stackSize];
stack[stackSize] = null; stack[stackSize] = null;

View File

@ -16,6 +16,7 @@
package com.google.gson.internal.bind; package com.google.gson.internal.bind;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
@ -92,6 +93,7 @@ public final class JsonTreeWriter extends JsonWriter {
} }
} }
@CanIgnoreReturnValue
@Override public JsonWriter beginArray() throws IOException { @Override public JsonWriter beginArray() throws IOException {
JsonArray array = new JsonArray(); JsonArray array = new JsonArray();
put(array); put(array);
@ -99,6 +101,7 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter endArray() throws IOException { @Override public JsonWriter endArray() throws IOException {
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
throw new IllegalStateException(); throw new IllegalStateException();
@ -111,6 +114,7 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter beginObject() throws IOException { @Override public JsonWriter beginObject() throws IOException {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
put(object); put(object);
@ -118,6 +122,7 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter endObject() throws IOException { @Override public JsonWriter endObject() throws IOException {
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
throw new IllegalStateException(); throw new IllegalStateException();
@ -130,6 +135,7 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter name(String name) throws IOException { @Override public JsonWriter name(String name) throws IOException {
Objects.requireNonNull(name, "name == null"); Objects.requireNonNull(name, "name == null");
if (stack.isEmpty() || pendingName != null) { if (stack.isEmpty() || pendingName != null) {
@ -143,6 +149,7 @@ public final class JsonTreeWriter extends JsonWriter {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(String value) throws IOException { @Override public JsonWriter value(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -155,16 +162,19 @@ public final class JsonTreeWriter extends JsonWriter {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@CanIgnoreReturnValue
@Override public JsonWriter nullValue() throws IOException { @Override public JsonWriter nullValue() throws IOException {
put(JsonNull.INSTANCE); put(JsonNull.INSTANCE);
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(boolean value) throws IOException { @Override public JsonWriter value(boolean value) throws IOException {
put(new JsonPrimitive(value)); put(new JsonPrimitive(value));
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(Boolean value) throws IOException { @Override public JsonWriter value(Boolean value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -173,6 +183,7 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(float value) throws IOException { @Override public JsonWriter value(float value) throws IOException {
if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) { if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
@ -181,6 +192,7 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(double value) throws IOException { @Override public JsonWriter value(double value) throws IOException {
if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) { if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value); throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
@ -189,11 +201,13 @@ public final class JsonTreeWriter extends JsonWriter {
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(long value) throws IOException { @Override public JsonWriter value(long value) throws IOException {
put(new JsonPrimitive(value)); put(new JsonPrimitive(value));
return this; return this;
} }
@CanIgnoreReturnValue
@Override public JsonWriter value(Number value) throws IOException { @Override public JsonWriter value(Number value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();

View File

@ -1119,7 +1119,7 @@ public class JsonReader implements Closeable {
return; return;
} else if (c == '\\') { } else if (c == '\\') {
pos = p; pos = p;
readEscapeCharacter(); char unused = readEscapeCharacter();
p = pos; p = pos;
l = limit; l = limit;
} else if (c == '\n') { } else if (c == '\n') {
@ -1664,7 +1664,7 @@ public class JsonReader implements Closeable {
*/ */
private void consumeNonExecutePrefix() throws IOException { private void consumeNonExecutePrefix() throws IOException {
// fast forward through the leading whitespace // fast forward through the leading whitespace
nextNonWhitespace(true); int unused = nextNonWhitespace(true);
pos--; pos--;
if (pos + 5 > limit && !fillBuffer(5)) { if (pos + 5 > limit && !fillBuffer(5)) {

View File

@ -24,6 +24,7 @@ import static com.google.gson.stream.JsonScope.NONEMPTY_ARRAY;
import static com.google.gson.stream.JsonScope.NONEMPTY_DOCUMENT; import static com.google.gson.stream.JsonScope.NONEMPTY_DOCUMENT;
import static com.google.gson.stream.JsonScope.NONEMPTY_OBJECT; import static com.google.gson.stream.JsonScope.NONEMPTY_OBJECT;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Closeable; import java.io.Closeable;
import java.io.Flushable; import java.io.Flushable;
import java.io.IOException; import java.io.IOException;
@ -318,6 +319,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter beginArray() throws IOException { public JsonWriter beginArray() throws IOException {
writeDeferredName(); writeDeferredName();
return open(EMPTY_ARRAY, '['); return open(EMPTY_ARRAY, '[');
@ -328,6 +330,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter endArray() throws IOException { public JsonWriter endArray() throws IOException {
return close(EMPTY_ARRAY, NONEMPTY_ARRAY, ']'); return close(EMPTY_ARRAY, NONEMPTY_ARRAY, ']');
} }
@ -338,6 +341,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter beginObject() throws IOException { public JsonWriter beginObject() throws IOException {
writeDeferredName(); writeDeferredName();
return open(EMPTY_OBJECT, '{'); return open(EMPTY_OBJECT, '{');
@ -348,6 +352,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter endObject() throws IOException { public JsonWriter endObject() throws IOException {
return close(EMPTY_OBJECT, NONEMPTY_OBJECT, '}'); return close(EMPTY_OBJECT, NONEMPTY_OBJECT, '}');
} }
@ -356,6 +361,7 @@ public class JsonWriter implements Closeable, Flushable {
* Enters a new scope by appending any necessary whitespace and the given * Enters a new scope by appending any necessary whitespace and the given
* bracket. * bracket.
*/ */
@CanIgnoreReturnValue
private JsonWriter open(int empty, char openBracket) throws IOException { private JsonWriter open(int empty, char openBracket) throws IOException {
beforeValue(); beforeValue();
push(empty); push(empty);
@ -367,6 +373,7 @@ public class JsonWriter implements Closeable, Flushable {
* Closes the current scope by appending any necessary whitespace and the * Closes the current scope by appending any necessary whitespace and the
* given bracket. * given bracket.
*/ */
@CanIgnoreReturnValue
private JsonWriter close(int empty, int nonempty, char closeBracket) private JsonWriter close(int empty, int nonempty, char closeBracket)
throws IOException { throws IOException {
int context = peek(); int context = peek();
@ -415,6 +422,7 @@ public class JsonWriter implements Closeable, Flushable {
* @param name the name of the forthcoming value. May not be null. * @param name the name of the forthcoming value. May not be null.
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter name(String name) throws IOException { public JsonWriter name(String name) throws IOException {
Objects.requireNonNull(name, "name == null"); Objects.requireNonNull(name, "name == null");
if (deferredName != null) { if (deferredName != null) {
@ -441,6 +449,7 @@ public class JsonWriter implements Closeable, Flushable {
* @param value the literal string value, or null to encode a null literal. * @param value the literal string value, or null to encode a null literal.
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(String value) throws IOException { public JsonWriter value(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -462,6 +471,7 @@ public class JsonWriter implements Closeable, Flushable {
* writing raw JSON values. * writing raw JSON values.
* @since 2.4 * @since 2.4
*/ */
@CanIgnoreReturnValue
public JsonWriter jsonValue(String value) throws IOException { public JsonWriter jsonValue(String value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -477,6 +487,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter nullValue() throws IOException { public JsonWriter nullValue() throws IOException {
if (deferredName != null) { if (deferredName != null) {
if (serializeNulls) { if (serializeNulls) {
@ -496,6 +507,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(boolean value) throws IOException { public JsonWriter value(boolean value) throws IOException {
writeDeferredName(); writeDeferredName();
beforeValue(); beforeValue();
@ -509,6 +521,7 @@ public class JsonWriter implements Closeable, Flushable {
* @return this writer. * @return this writer.
* @since 2.7 * @since 2.7
*/ */
@CanIgnoreReturnValue
public JsonWriter value(Boolean value) throws IOException { public JsonWriter value(Boolean value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();
@ -530,6 +543,7 @@ public class JsonWriter implements Closeable, Flushable {
* #setLenient(boolean) lenient}. * #setLenient(boolean) lenient}.
* @since 2.9.1 * @since 2.9.1
*/ */
@CanIgnoreReturnValue
public JsonWriter value(float value) throws IOException { public JsonWriter value(float value) throws IOException {
writeDeferredName(); writeDeferredName();
if (!lenient && (Float.isNaN(value) || Float.isInfinite(value))) { if (!lenient && (Float.isNaN(value) || Float.isInfinite(value))) {
@ -549,6 +563,7 @@ public class JsonWriter implements Closeable, Flushable {
* @throws IllegalArgumentException if the value is NaN or Infinity and this writer is * @throws IllegalArgumentException if the value is NaN or Infinity and this writer is
* not {@link #setLenient(boolean) lenient}. * not {@link #setLenient(boolean) lenient}.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(double value) throws IOException { public JsonWriter value(double value) throws IOException {
writeDeferredName(); writeDeferredName();
if (!lenient && (Double.isNaN(value) || Double.isInfinite(value))) { if (!lenient && (Double.isNaN(value) || Double.isInfinite(value))) {
@ -564,6 +579,7 @@ public class JsonWriter implements Closeable, Flushable {
* *
* @return this writer. * @return this writer.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(long value) throws IOException { public JsonWriter value(long value) throws IOException {
writeDeferredName(); writeDeferredName();
beforeValue(); beforeValue();
@ -593,6 +609,7 @@ public class JsonWriter implements Closeable, Flushable {
* not {@link #setLenient(boolean) lenient}; or if the {@code toString()} result is not a * not {@link #setLenient(boolean) lenient}; or if the {@code toString()} result is not a
* valid JSON number. * valid JSON number.
*/ */
@CanIgnoreReturnValue
public JsonWriter value(Number value) throws IOException { public JsonWriter value(Number value) throws IOException {
if (value == null) { if (value == null) {
return nullValue(); return nullValue();

View File

@ -70,8 +70,8 @@ public class JsonStreamParserTest {
@Test @Test
public void testCallingNextBeyondAvailableInput() { public void testCallingNextBeyondAvailableInput() {
parser.next(); JsonElement unused1 = parser.next();
parser.next(); JsonElement unused2 = parser.next();
try { try {
parser.next(); parser.next();
fail("Parser should not go beyond available input"); fail("Parser should not go beyond available input");

View File

@ -88,11 +88,11 @@ public final class MixedStreamTest {
jsonReader.beginArray(); jsonReader.beginArray();
jsonReader.setLenient(false); jsonReader.setLenient(false);
gson.fromJson(jsonReader, Car.class); Car unused1 = gson.fromJson(jsonReader, Car.class);
assertThat(jsonReader.isLenient()).isFalse(); assertThat(jsonReader.isLenient()).isFalse();
jsonReader.setLenient(true); jsonReader.setLenient(true);
gson.fromJson(jsonReader, Car.class); Car unused2 = gson.fromJson(jsonReader, Car.class);
assertThat(jsonReader.isLenient()).isTrue(); assertThat(jsonReader.isLenient()).isTrue();
} }

View File

@ -47,7 +47,7 @@ public class ConcurrencyTest {
public void testSingleThreadSerialization() { public void testSingleThreadSerialization() {
MyObject myObj = new MyObject(); MyObject myObj = new MyObject();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
gson.toJson(myObj); String unused = gson.toJson(myObj);
} }
} }
@ -58,7 +58,7 @@ public class ConcurrencyTest {
@Test @Test
public void testSingleThreadDeserialization() { public void testSingleThreadDeserialization() {
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); MyObject unused = gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class);
} }
} }
@ -79,7 +79,7 @@ public class ConcurrencyTest {
try { try {
startLatch.await(); startLatch.await();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
gson.toJson(myObj); String unused = gson.toJson(myObj);
} }
} catch (Throwable t) { } catch (Throwable t) {
failed.set(true); failed.set(true);
@ -110,7 +110,7 @@ public class ConcurrencyTest {
try { try {
startLatch.await(); startLatch.await();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class); MyObject unused = gson.fromJson("{'a':'hello','b':'world','i':1}", MyObject.class);
} }
} catch (Throwable t) { } catch (Throwable t) {
failed.set(true); failed.set(true);

View File

@ -127,7 +127,7 @@ public class DefaultTypeAdaptersTest {
URL target = gson.fromJson(json, URL.class); URL target = gson.fromJson(json, URL.class);
assertThat(target.toExternalForm()).isEqualTo(urlValue); assertThat(target.toExternalForm()).isEqualTo(urlValue);
gson.fromJson('"' + urlValue + '"', URL.class); URL unused = gson.fromJson('"' + urlValue + '"', URL.class);
assertThat(target.toExternalForm()).isEqualTo(urlValue); assertThat(target.toExternalForm()).isEqualTo(urlValue);
} }

View File

@ -17,6 +17,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -267,6 +268,7 @@ public class InheritanceTest {
return sb.toString(); return sb.toString();
} }
@CanIgnoreReturnValue
private StringBuilder append(StringBuilder sb, Collection<?> c) { private StringBuilder append(StringBuilder sb, Collection<?> c) {
sb.append("["); sb.append("[");
boolean first = true; boolean first = true;

View File

@ -118,7 +118,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
out.value("PartJsonFieldAnnotationAdapter"); out.value("PartJsonFieldAnnotationAdapter");
} }
@Override public Part read(JsonReader in) throws IOException { @Override public Part read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return new Part("PartJsonFieldAnnotationAdapter"); return new Part("PartJsonFieldAnnotationAdapter");
} }
} }
@ -131,7 +131,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override public T read(JsonReader in) throws IOException { @Override public T read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return (T) new Part("GizmoPartTypeAdapterFactory"); return (T) new Part("GizmoPartTypeAdapterFactory");
} }
}; };
@ -158,7 +158,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
out.value("UserClassAnnotationAdapter"); out.value("UserClassAnnotationAdapter");
} }
@Override public User read(JsonReader in) throws IOException { @Override public User read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return new User("UserClassAnnotationAdapter"); return new User("UserClassAnnotationAdapter");
} }
} }
@ -177,7 +177,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
out.value("UserFieldAnnotationAdapter"); out.value("UserFieldAnnotationAdapter");
} }
@Override public User read(JsonReader in) throws IOException { @Override public User read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return new User("UserFieldAnnotationAdapter"); return new User("UserFieldAnnotationAdapter");
} }
} }
@ -187,7 +187,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
out.value("RegisteredUserAdapter"); out.value("RegisteredUserAdapter");
} }
@Override public User read(JsonReader in) throws IOException { @Override public User read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return new User("RegisteredUserAdapter"); return new User("RegisteredUserAdapter");
} }
} }
@ -307,7 +307,7 @@ public final class JsonAdapterAnnotationOnFieldsTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override public T read(JsonReader in) throws IOException { @Override public T read(JsonReader in) throws IOException {
in.nextString(); String unused = in.nextString();
return (T) Arrays.asList(new Part("GizmoPartTypeAdapterFactory")); return (T) Arrays.asList(new Part("GizmoPartTypeAdapterFactory"));
} }
}; };

View File

@ -539,17 +539,17 @@ public class ObjectTest {
Gson gson = new Gson(); Gson gson = new Gson();
Product product = new Product(); Product product = new Product();
assertThat(gson.toJson(product)).isEqualTo("{\"attributes\":[],\"departments\":[]}"); assertThat(gson.toJson(product)).isEqualTo("{\"attributes\":[],\"departments\":[]}");
gson.fromJson(gson.toJson(product), Product.class); Product unused1 = gson.fromJson(gson.toJson(product), Product.class);
product.departments.add(new Department()); product.departments.add(new Department());
assertThat(gson.toJson(product)) assertThat(gson.toJson(product))
.isEqualTo("{\"attributes\":[],\"departments\":[{\"name\":\"abc\",\"code\":\"123\"}]}"); .isEqualTo("{\"attributes\":[],\"departments\":[{\"name\":\"abc\",\"code\":\"123\"}]}");
gson.fromJson(gson.toJson(product), Product.class); Product unused2 = gson.fromJson(gson.toJson(product), Product.class);
product.attributes.add("456"); product.attributes.add("456");
assertThat(gson.toJson(product)) assertThat(gson.toJson(product))
.isEqualTo("{\"attributes\":[\"456\"],\"departments\":[{\"name\":\"abc\",\"code\":\"123\"}]}"); .isEqualTo("{\"attributes\":[\"456\"],\"departments\":[{\"name\":\"abc\",\"code\":\"123\"}]}");
gson.fromJson(gson.toJson(product), Product.class); Product unused3 = gson.fromJson(gson.toJson(product), Product.class);
} }
static final class Department { static final class Department {
@ -611,7 +611,7 @@ public class ObjectTest {
@Test @Test
public void testStaticFieldDeserialization() { public void testStaticFieldDeserialization() {
// By default Gson should ignore static fields // By default Gson should ignore static fields
gson.fromJson("{\"s\":\"custom\"}", ClassWithStaticField.class); ClassWithStaticField unused = gson.fromJson("{\"s\":\"custom\"}", ClassWithStaticField.class);
assertThat(ClassWithStaticField.s).isEqualTo("initial"); assertThat(ClassWithStaticField.s).isEqualTo("initial");
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()

View File

@ -18,6 +18,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonStreamParser; import com.google.gson.JsonStreamParser;
@ -155,12 +156,14 @@ public class ReadersWritersTest {
final StringBuilder stringBuilder = new StringBuilder(); final StringBuilder stringBuilder = new StringBuilder();
int toStringCallCount = 0; int toStringCallCount = 0;
@CanIgnoreReturnValue
@Override @Override
public Appendable append(char c) throws IOException { public Appendable append(char c) throws IOException {
stringBuilder.append(c); stringBuilder.append(c);
return this; return this;
} }
@CanIgnoreReturnValue
@Override @Override
public Appendable append(CharSequence csq) throws IOException { public Appendable append(CharSequence csq) throws IOException {
if (csq == null) { if (csq == null) {
@ -170,6 +173,7 @@ public class ReadersWritersTest {
return this; return this;
} }
@CanIgnoreReturnValue
@Override @Override
public Appendable append(CharSequence csq, int start, int end) throws IOException { public Appendable append(CharSequence csq, int start, int end) throws IOException {
if (csq == null) { if (csq == null) {

View File

@ -17,6 +17,7 @@ package com.google.gson.functional;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -124,6 +125,7 @@ public final class RuntimeTypeAdapterFactoryFunctionalTest {
* @throws IllegalArgumentException if either {@code type} or {@code label} * @throws IllegalArgumentException if either {@code type} or {@code label}
* have already been registered on this type adapter. * have already been registered on this type adapter.
*/ */
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) { public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
if (type == null || label == null) { if (type == null || label == null) {
throw new NullPointerException(); throw new NullPointerException();
@ -143,6 +145,7 @@ public final class RuntimeTypeAdapterFactoryFunctionalTest {
* @throws IllegalArgumentException if either {@code type} or its simple name * @throws IllegalArgumentException if either {@code type} or its simple name
* have already been registered on this type adapter. * have already been registered on this type adapter.
*/ */
@CanIgnoreReturnValue
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) { public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName()); return registerSubtype(type, type.getSimpleName());
} }

View File

@ -136,7 +136,7 @@ public final class TypeHierarchyAdapterTest {
/** This behaviour changed in Gson 2.1; it used to throw. */ /** This behaviour changed in Gson 2.1; it used to throw. */
@Test @Test
public void testRegisterSubTypeFirstAllowed() { public void testRegisterSubTypeFirstAllowed() {
new GsonBuilder() Gson unused = new GsonBuilder()
.registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter()) .registerTypeHierarchyAdapter(Manager.class, new ManagerAdapter())
.registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter()) .registerTypeHierarchyAdapter(Employee.class, new EmployeeAdapter())
.create(); .create();

View File

@ -332,9 +332,9 @@ public final class JsonElementReaderTest {
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
reader.nextName(); String unused1 = reader.nextName();
assertThat(new JsonPrimitive(1)).isEqualTo(reader.nextJsonElement()); assertThat(new JsonPrimitive(1)).isEqualTo(reader.nextJsonElement());
reader.nextName(); String unused2 = reader.nextName();
reader.beginObject(); reader.beginObject();
try { try {
reader.nextJsonElement(); reader.nextJsonElement();
@ -342,7 +342,7 @@ public final class JsonElementReaderTest {
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
reader.endObject(); reader.endObject();
reader.nextName(); String unused3 = reader.nextName();
reader.beginArray(); reader.beginArray();
try { try {
reader.nextJsonElement(); reader.nextJsonElement();

View File

@ -123,7 +123,7 @@ public class PerformanceTest {
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
list.add(new CollectionEntry("name"+i,"value"+i)); list.add(new CollectionEntry("name"+i,"value"+i));
} }
gson.toJson(list); String unused = gson.toJson(list);
} }
/** /**
@ -163,7 +163,7 @@ public class PerformanceTest {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
ba[i] = 0x05; ba[i] = 0x05;
} }
gson.toJson(ba); String unused = gson.toJson(ba);
System.out.printf("Gson could serialize a byte array of size: %d\n", size); System.out.printf("Gson could serialize a byte array of size: %d\n", size);
} }
} }
@ -248,7 +248,7 @@ public class PerformanceTest {
System.out.printf("Large object serialized in: %d ms\n", (t2 - t1)); System.out.printf("Large object serialized in: %d ms\n", (t2 - t1));
t1 = System.currentTimeMillis(); t1 = System.currentTimeMillis();
gson.fromJson(json, new TypeToken<Map<String, Long>>() {}.getType()); Map<String, Long> unused = gson.fromJson(json, new TypeToken<Map<String, Long>>() {}.getType());
t2 = System.currentTimeMillis(); t2 = System.currentTimeMillis();
System.out.printf("Large object deserialized in: %d ms\n", (t2 - t1)); System.out.printf("Large object deserialized in: %d ms\n", (t2 - t1));
@ -297,7 +297,7 @@ public class PerformanceTest {
Gson gson = new Gson(); Gson gson = new Gson();
String json = gson.toJson(original); String json = gson.toJson(original);
Type longToLong = new TypeToken<Map<Long, Long>>(){}.getType(); Type longToLong = new TypeToken<Map<Long, Long>>(){}.getType();
gson.fromJson(json, longToLong); Map<Long, Long> unused = gson.fromJson(json, longToLong);
} }
private String buildJsonForClassWithList() { private String buildJsonForClassWithList() {

View File

@ -30,7 +30,7 @@ public class JsonAdapterNullSafeTest {
@Test @Test
public void testNullSafeBugSerialize() { public void testNullSafeBugSerialize() {
Device device = new Device("ec57803e"); Device device = new Device("ec57803e");
gson.toJson(device); String unused = gson.toJson(device);
} }
@Test @Test

View File

@ -51,34 +51,34 @@ public class JsonReaderPathTest {
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$."); assertThat(reader.getPreviousPath()).isEqualTo("$.");
assertThat(reader.getPath()).isEqualTo("$."); assertThat(reader.getPath()).isEqualTo("$.");
reader.nextName(); String unused1 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.beginArray(); reader.beginArray();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[0]");
assertThat(reader.getPath()).isEqualTo("$.a[0]"); assertThat(reader.getPath()).isEqualTo("$.a[0]");
reader.nextInt(); int unused2 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[0]");
assertThat(reader.getPath()).isEqualTo("$.a[1]"); assertThat(reader.getPath()).isEqualTo("$.a[1]");
reader.nextBoolean(); boolean unused3 = reader.nextBoolean();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[1]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[1]");
assertThat(reader.getPath()).isEqualTo("$.a[2]"); assertThat(reader.getPath()).isEqualTo("$.a[2]");
reader.nextBoolean(); boolean unused4 = reader.nextBoolean();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[2]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[2]");
assertThat(reader.getPath()).isEqualTo("$.a[3]"); assertThat(reader.getPath()).isEqualTo("$.a[3]");
reader.nextNull(); reader.nextNull();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[3]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[3]");
assertThat(reader.getPath()).isEqualTo("$.a[4]"); assertThat(reader.getPath()).isEqualTo("$.a[4]");
reader.nextString(); String unused5 = reader.nextString();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[4]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[4]");
assertThat(reader.getPath()).isEqualTo("$.a[5]"); assertThat(reader.getPath()).isEqualTo("$.a[5]");
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[5]."); assertThat(reader.getPreviousPath()).isEqualTo("$.a[5].");
assertThat(reader.getPath()).isEqualTo("$.a[5]."); assertThat(reader.getPath()).isEqualTo("$.a[5].");
reader.nextName(); String unused6 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[5].c"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[5].c");
assertThat(reader.getPath()).isEqualTo("$.a[5].c"); assertThat(reader.getPath()).isEqualTo("$.a[5].c");
reader.nextString(); String unused7 = reader.nextString();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[5].c"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[5].c");
assertThat(reader.getPath()).isEqualTo("$.a[5].c"); assertThat(reader.getPath()).isEqualTo("$.a[5].c");
reader.endObject(); reader.endObject();
@ -87,7 +87,7 @@ public class JsonReaderPathTest {
reader.beginArray(); reader.beginArray();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[6][0]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[6][0]");
assertThat(reader.getPath()).isEqualTo("$.a[6][0]"); assertThat(reader.getPath()).isEqualTo("$.a[6][0]");
reader.nextInt(); int unused8 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.a[6][0]"); assertThat(reader.getPreviousPath()).isEqualTo("$.a[6][0]");
assertThat(reader.getPath()).isEqualTo("$.a[6][1]"); assertThat(reader.getPath()).isEqualTo("$.a[6][1]");
reader.endArray(); reader.endArray();
@ -106,49 +106,49 @@ public class JsonReaderPathTest {
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.peek(); JsonToken unused1 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$."); assertThat(reader.getPreviousPath()).isEqualTo("$.");
assertThat(reader.getPath()).isEqualTo("$."); assertThat(reader.getPath()).isEqualTo("$.");
reader.peek(); JsonToken unused2 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$."); assertThat(reader.getPreviousPath()).isEqualTo("$.");
assertThat(reader.getPath()).isEqualTo("$."); assertThat(reader.getPath()).isEqualTo("$.");
reader.nextName(); String unused3 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.peek(); JsonToken unused4 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.nextInt(); int unused5 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.peek(); JsonToken unused6 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.nextName(); String unused7 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
reader.peek(); JsonToken unused8 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
reader.nextInt(); int unused9 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
reader.peek(); JsonToken unused10 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
reader.endObject(); reader.endObject();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.peek(); JsonToken unused11 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.close(); reader.close();
@ -161,35 +161,35 @@ public class JsonReaderPathTest {
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.peek(); JsonToken unused1 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.beginArray(); reader.beginArray();
assertThat(reader.getPreviousPath()).isEqualTo("$[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$[0]");
assertThat(reader.getPath()).isEqualTo("$[0]"); assertThat(reader.getPath()).isEqualTo("$[0]");
reader.peek(); JsonToken unused2 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$[0]");
assertThat(reader.getPath()).isEqualTo("$[0]"); assertThat(reader.getPath()).isEqualTo("$[0]");
reader.nextInt(); int unused3 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$[0]");
assertThat(reader.getPath()).isEqualTo("$[1]"); assertThat(reader.getPath()).isEqualTo("$[1]");
reader.peek(); JsonToken unused4 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$[0]"); assertThat(reader.getPreviousPath()).isEqualTo("$[0]");
assertThat(reader.getPath()).isEqualTo("$[1]"); assertThat(reader.getPath()).isEqualTo("$[1]");
reader.nextInt(); int unused5 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$[1]"); assertThat(reader.getPreviousPath()).isEqualTo("$[1]");
assertThat(reader.getPath()).isEqualTo("$[2]"); assertThat(reader.getPath()).isEqualTo("$[2]");
reader.peek(); JsonToken unused6 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$[1]"); assertThat(reader.getPreviousPath()).isEqualTo("$[1]");
assertThat(reader.getPath()).isEqualTo("$[2]"); assertThat(reader.getPath()).isEqualTo("$[2]");
reader.endArray(); reader.endArray();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.peek(); JsonToken unused7 = reader.peek();
assertThat(reader.getPreviousPath()).isEqualTo("$"); assertThat(reader.getPreviousPath()).isEqualTo("$");
assertThat(reader.getPath()).isEqualTo("$"); assertThat(reader.getPath()).isEqualTo("$");
reader.close(); reader.close();
@ -249,11 +249,11 @@ public class JsonReaderPathTest {
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$."); assertThat(reader.getPreviousPath()).isEqualTo("$.");
assertThat(reader.getPath()).isEqualTo("$."); assertThat(reader.getPath()).isEqualTo("$.");
reader.nextName(); String unused1 = reader.nextName();
reader.skipValue(); reader.skipValue();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.nextName(); String unused2 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
} }
@ -261,7 +261,7 @@ public class JsonReaderPathTest {
@Test public void skipObjectEnd() throws IOException { @Test public void skipObjectEnd() throws IOException {
JsonReader reader = factory.create("{\"a\":{},\"b\":2}"); JsonReader reader = factory.create("{\"a\":{},\"b\":2}");
reader.beginObject(); reader.beginObject();
reader.nextName(); String unused = reader.nextName();
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$.a."); assertThat(reader.getPreviousPath()).isEqualTo("$.a.");
assertThat(reader.getPath()).isEqualTo("$.a."); assertThat(reader.getPath()).isEqualTo("$.a.");
@ -353,37 +353,37 @@ public class JsonReaderPathTest {
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$."); assertThat(reader.getPreviousPath()).isEqualTo("$.");
assertThat(reader.getPath()).isEqualTo("$."); assertThat(reader.getPath()).isEqualTo("$.");
reader.nextName(); String unused1 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$.a."); assertThat(reader.getPreviousPath()).isEqualTo("$.a.");
assertThat(reader.getPath()).isEqualTo("$.a."); assertThat(reader.getPath()).isEqualTo("$.a.");
reader.nextName(); String unused2 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a.a1"); assertThat(reader.getPreviousPath()).isEqualTo("$.a.a1");
assertThat(reader.getPath()).isEqualTo("$.a.a1"); assertThat(reader.getPath()).isEqualTo("$.a.a1");
reader.nextInt(); int unused3 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.a.a1"); assertThat(reader.getPreviousPath()).isEqualTo("$.a.a1");
assertThat(reader.getPath()).isEqualTo("$.a.a1"); assertThat(reader.getPath()).isEqualTo("$.a.a1");
reader.nextName(); String unused4 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.a.a2"); assertThat(reader.getPreviousPath()).isEqualTo("$.a.a2");
assertThat(reader.getPath()).isEqualTo("$.a.a2"); assertThat(reader.getPath()).isEqualTo("$.a.a2");
reader.nextInt(); int unused5 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.a.a2"); assertThat(reader.getPreviousPath()).isEqualTo("$.a.a2");
assertThat(reader.getPath()).isEqualTo("$.a.a2"); assertThat(reader.getPath()).isEqualTo("$.a.a2");
reader.endObject(); reader.endObject();
assertThat(reader.getPreviousPath()).isEqualTo("$.a"); assertThat(reader.getPreviousPath()).isEqualTo("$.a");
assertThat(reader.getPath()).isEqualTo("$.a"); assertThat(reader.getPath()).isEqualTo("$.a");
reader.nextName(); String unused6 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.b"); assertThat(reader.getPreviousPath()).isEqualTo("$.b");
assertThat(reader.getPath()).isEqualTo("$.b"); assertThat(reader.getPath()).isEqualTo("$.b");
reader.beginObject(); reader.beginObject();
assertThat(reader.getPreviousPath()).isEqualTo("$.b."); assertThat(reader.getPreviousPath()).isEqualTo("$.b.");
assertThat(reader.getPath()).isEqualTo("$.b."); assertThat(reader.getPath()).isEqualTo("$.b.");
reader.nextName(); String unused7 = reader.nextName();
assertThat(reader.getPreviousPath()).isEqualTo("$.b.b1"); assertThat(reader.getPreviousPath()).isEqualTo("$.b.b1");
assertThat(reader.getPath()).isEqualTo("$.b.b1"); assertThat(reader.getPath()).isEqualTo("$.b.b1");
reader.nextInt(); int unused8 = reader.nextInt();
assertThat(reader.getPreviousPath()).isEqualTo("$.b.b1"); assertThat(reader.getPreviousPath()).isEqualTo("$.b.b1");
assertThat(reader.getPath()).isEqualTo("$.b.b1"); assertThat(reader.getPath()).isEqualTo("$.b.b1");
reader.endObject(); reader.endObject();

View File

@ -33,6 +33,7 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.Arrays; import java.util.Arrays;
import com.google.gson.stream.JsonToken;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -855,8 +856,8 @@ public final class JsonReaderTest {
try { try {
JsonReader reader = new JsonReader(reader("{\"a\":true}")); JsonReader reader = new JsonReader(reader("{\"a\":true}"));
reader.beginObject(); reader.beginObject();
reader.nextName(); String unused1 = reader.nextName();
reader.peek(); JsonToken unused2 = reader.peek();
reader.close(); reader.close();
reader.nextBoolean(); reader.nextBoolean();
fail(); fail();
@ -869,13 +870,13 @@ public final class JsonReaderTest {
JsonReader reader = new JsonReader(reader("{\"a\":true}")); JsonReader reader = new JsonReader(reader("{\"a\":true}"));
reader.beginObject(); reader.beginObject();
try { try {
reader.nextString(); String unused = reader.nextString();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
assertThat(reader.nextName()).isEqualTo("a"); assertThat(reader.nextName()).isEqualTo("a");
try { try {
reader.nextName(); String unused = reader.nextName();
fail(); fail();
} catch (IllegalStateException expected) { } catch (IllegalStateException expected) {
} }
@ -1234,8 +1235,8 @@ public final class JsonReaderTest {
JsonReader reader = new JsonReader(reader("[true;true]")); JsonReader reader = new JsonReader(reader("[true;true]"));
reader.beginArray(); reader.beginArray();
try { try {
reader.nextBoolean(); boolean unused1 = reader.nextBoolean();
reader.nextBoolean(); boolean unused2 = reader.nextBoolean();
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
} }
@ -1268,8 +1269,8 @@ public final class JsonReaderTest {
reader.beginObject(); reader.beginObject();
assertThat(reader.nextName()).isEqualTo("a"); assertThat(reader.nextName()).isEqualTo("a");
try { try {
reader.nextBoolean(); boolean unused1 = reader.nextBoolean();
reader.nextName(); String unused2 = reader.nextName();
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
} }
@ -1601,9 +1602,9 @@ public final class JsonReaderTest {
JsonReader reader1 = new JsonReader(reader(json)); JsonReader reader1 = new JsonReader(reader(json));
reader1.setLenient(true); reader1.setLenient(true);
reader1.beginArray(); reader1.beginArray();
reader1.nextString(); String unused1 = reader1.nextString();
try { try {
reader1.peek(); JsonToken unused2 = reader1.peek();
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo(message); assertThat(expected.getMessage()).isEqualTo(message);
@ -1615,7 +1616,7 @@ public final class JsonReaderTest {
reader2.beginArray(); reader2.beginArray();
reader2.skipValue(); reader2.skipValue();
try { try {
reader2.peek(); JsonToken unused3 = reader2.peek();
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo(message); assertThat(expected.getMessage()).isEqualTo(message);
@ -1626,14 +1627,14 @@ public final class JsonReaderTest {
public void testFailWithPositionDeepPath() throws IOException { public void testFailWithPositionDeepPath() throws IOException {
JsonReader reader = new JsonReader(reader("[1,{\"a\":[2,3,}")); JsonReader reader = new JsonReader(reader("[1,{\"a\":[2,3,}"));
reader.beginArray(); reader.beginArray();
reader.nextInt(); int unused1 = reader.nextInt();
reader.beginObject(); reader.beginObject();
reader.nextName(); String unused2 = reader.nextName();
reader.beginArray(); reader.beginArray();
reader.nextInt(); int unused3 = reader.nextInt();
reader.nextInt(); int unused4 = reader.nextInt();
try { try {
reader.peek(); JsonToken unused5 = reader.peek();
fail(); fail();
} catch (IOException expected) { } catch (IOException expected) {
assertThat(expected.getMessage()).isEqualTo("Expected value at line 1 column 14 path $[1].a[2]"); assertThat(expected.getMessage()).isEqualTo("Expected value at line 1 column 14 path $[1].a[2]");

View File

@ -20,6 +20,7 @@ import static java.util.Objects.requireNonNull;
import com.google.common.base.CaseFormat; import com.google.common.base.CaseFormat;
import com.google.common.collect.MapMaker; import com.google.common.collect.MapMaker;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer; import com.google.gson.JsonDeserializer;
@ -102,6 +103,7 @@ public class ProtoTypeAdapter
setFieldNameSerializationFormat(fromFieldNameFormat, toFieldNameFormat); setFieldNameSerializationFormat(fromFieldNameFormat, toFieldNameFormat);
} }
@CanIgnoreReturnValue
public Builder setEnumSerialization(EnumSerialization enumSerialization) { public Builder setEnumSerialization(EnumSerialization enumSerialization) {
this.enumSerialization = requireNonNull(enumSerialization); this.enumSerialization = requireNonNull(enumSerialization);
return this; return this;
@ -122,6 +124,7 @@ public class ProtoTypeAdapter
* n__id_ct nIdCt * n__id_ct nIdCt
* }</pre> * }</pre>
*/ */
@CanIgnoreReturnValue
public Builder setFieldNameSerializationFormat(CaseFormat fromFieldNameFormat, public Builder setFieldNameSerializationFormat(CaseFormat fromFieldNameFormat,
CaseFormat toFieldNameFormat) { CaseFormat toFieldNameFormat) {
this.protoFormat = fromFieldNameFormat; this.protoFormat = fromFieldNameFormat;
@ -141,6 +144,7 @@ public class ProtoTypeAdapter
* ...the adapter will serialize the field using '{@code appId}' instead of the default ' * ...the adapter will serialize the field using '{@code appId}' instead of the default '
* {@code clientAppId}'. This lets you customize the name serialization of any proto field. * {@code clientAppId}'. This lets you customize the name serialization of any proto field.
*/ */
@CanIgnoreReturnValue
public Builder addSerializedNameExtension( public Builder addSerializedNameExtension(
Extension<FieldOptions, String> serializedNameExtension) { Extension<FieldOptions, String> serializedNameExtension) {
serializedNameExtensions.add(requireNonNull(serializedNameExtension)); serializedNameExtensions.add(requireNonNull(serializedNameExtension));
@ -166,6 +170,7 @@ public class ProtoTypeAdapter
* Note that you need to set the enum serialization of this adapter to * Note that you need to set the enum serialization of this adapter to
* {@link EnumSerialization#NAME}, otherwise these annotations will be ignored. * {@link EnumSerialization#NAME}, otherwise these annotations will be ignored.
*/ */
@CanIgnoreReturnValue
public Builder addSerializedEnumValueExtension( public Builder addSerializedEnumValueExtension(
Extension<EnumValueOptions, String> serializedEnumValueExtension) { Extension<EnumValueOptions, String> serializedEnumValueExtension) {
serializedEnumValueExtensions.add(requireNonNull(serializedEnumValueExtension)); serializedEnumValueExtensions.add(requireNonNull(serializedEnumValueExtension));