diff --git a/gson/src/main/java/com/google/gson/FieldAttributes.java b/gson/src/main/java/com/google/gson/FieldAttributes.java index 9fb93f7b..4b9e16ab 100644 --- a/gson/src/main/java/com/google/gson/FieldAttributes.java +++ b/gson/src/main/java/com/google/gson/FieldAttributes.java @@ -16,12 +16,12 @@ package com.google.gson; -import com.google.gson.internal.$Gson$Preconditions; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.util.Arrays; import java.util.Collection; +import java.util.Objects; /** * A data object that stores attributes of a field. @@ -42,8 +42,7 @@ public final class FieldAttributes { * @param f the field to pull attributes from */ public FieldAttributes(Field f) { - $Gson$Preconditions.checkNotNull(f); - this.field = f; + this.field = Objects.requireNonNull(f); } /** diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 1676c1cb..f5045a1c 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -54,6 +54,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; @@ -504,9 +505,7 @@ public final class Gson { */ @SuppressWarnings("unchecked") public TypeAdapter getAdapter(TypeToken type) { - if (type == null) { - throw new NullPointerException("type must not be null"); - } + Objects.requireNonNull(type, "type must not be null"); TypeAdapter cached = typeTokenCache.get(type); if (cached != null) { return (TypeAdapter) cached; diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 81fa022d..12db7915 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -16,26 +16,6 @@ package com.google.gson; -import java.lang.reflect.Type; -import java.text.DateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import com.google.gson.internal.$Gson$Preconditions; -import com.google.gson.internal.Excluder; -import com.google.gson.internal.bind.DefaultDateTypeAdapter; -import com.google.gson.internal.bind.TreeTypeAdapter; -import com.google.gson.internal.bind.TypeAdapters; -import com.google.gson.internal.sql.SqlTypesSupport; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; - import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS; import static com.google.gson.Gson.DEFAULT_DATE_PATTERN; import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML; @@ -48,6 +28,26 @@ 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_USE_JDK_UNSAFE; +import com.google.gson.internal.$Gson$Preconditions; +import com.google.gson.internal.Excluder; +import com.google.gson.internal.bind.DefaultDateTypeAdapter; +import com.google.gson.internal.bind.TreeTypeAdapter; +import com.google.gson.internal.bind.TypeAdapters; +import com.google.gson.internal.sql.SqlTypesSupport; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; + /** *

Use this builder to construct a {@link Gson} instance when you need to set configuration * options other than the default. For {@link Gson} with default configuration, it is simpler to @@ -166,7 +166,7 @@ public final class GsonBuilder { * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder excludeFieldsWithModifiers(int... modifiers) { - $Gson$Preconditions.checkNotNull(modifiers); + Objects.requireNonNull(modifiers); excluder = excluder.withModifiers(modifiers); return this; } @@ -310,8 +310,7 @@ public final class GsonBuilder { * @since 1.3 */ public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) { - $Gson$Preconditions.checkNotNull(serializationPolicy); - this.longSerializationPolicy = serializationPolicy; + this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy); return this; } @@ -334,8 +333,7 @@ public final class GsonBuilder { * @since 1.3 */ public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) { - $Gson$Preconditions.checkNotNull(fieldNamingStrategy); - this.fieldNamingPolicy = fieldNamingStrategy; + this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy); return this; } @@ -347,8 +345,7 @@ public final class GsonBuilder { * @see ToNumberPolicy#DOUBLE The default object-to-number strategy */ public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) { - $Gson$Preconditions.checkNotNull(objectToNumberStrategy); - this.objectToNumberStrategy = objectToNumberStrategy; + this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy); return this; } @@ -360,8 +357,7 @@ public final class GsonBuilder { * @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy */ public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) { - $Gson$Preconditions.checkNotNull(numberToNumberStrategy); - this.numberToNumberStrategy = numberToNumberStrategy; + this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy); return this; } @@ -378,7 +374,7 @@ public final class GsonBuilder { * @since 1.4 */ public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) { - $Gson$Preconditions.checkNotNull(strategies); + Objects.requireNonNull(strategies); for (ExclusionStrategy strategy : strategies) { excluder = excluder.withExclusionStrategy(strategy, true, true); } @@ -398,7 +394,7 @@ public final class GsonBuilder { * @since 1.7 */ public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) { - $Gson$Preconditions.checkNotNull(strategy); + Objects.requireNonNull(strategy); excluder = excluder.withExclusionStrategy(strategy, true, false); return this; } @@ -416,7 +412,7 @@ public final class GsonBuilder { * @since 1.7 */ public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) { - $Gson$Preconditions.checkNotNull(strategy); + Objects.requireNonNull(strategy); excluder = excluder.withExclusionStrategy(strategy, false, true); return this; } @@ -547,7 +543,7 @@ public final class GsonBuilder { */ @SuppressWarnings({"unchecked", "rawtypes"}) public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { - $Gson$Preconditions.checkNotNull(type); + Objects.requireNonNull(type); $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator @@ -574,7 +570,7 @@ public final class GsonBuilder { * @since 2.1 */ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { - $Gson$Preconditions.checkNotNull(factory); + Objects.requireNonNull(factory); factories.add(factory); return this; } @@ -595,7 +591,7 @@ public final class GsonBuilder { */ @SuppressWarnings({"unchecked", "rawtypes"}) public GsonBuilder registerTypeHierarchyAdapter(Class baseType, Object typeAdapter) { - $Gson$Preconditions.checkNotNull(baseType); + Objects.requireNonNull(baseType); $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof TypeAdapter); @@ -669,7 +665,7 @@ public final class GsonBuilder { * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern */ public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) { - $Gson$Preconditions.checkNotNull(filter); + Objects.requireNonNull(filter); reflectionFilters.addFirst(filter); return this; } diff --git a/gson/src/main/java/com/google/gson/JsonPrimitive.java b/gson/src/main/java/com/google/gson/JsonPrimitive.java index 8b8570bc..f69ffd5f 100644 --- a/gson/src/main/java/com/google/gson/JsonPrimitive.java +++ b/gson/src/main/java/com/google/gson/JsonPrimitive.java @@ -16,10 +16,10 @@ package com.google.gson; -import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.LazilyParsedNumber; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Objects; /** * A class representing a JSON primitive value. A primitive value @@ -40,7 +40,7 @@ public final class JsonPrimitive extends JsonElement { */ @SuppressWarnings("deprecation") // superclass constructor public JsonPrimitive(Boolean bool) { - value = $Gson$Preconditions.checkNotNull(bool); + value = Objects.requireNonNull(bool); } /** @@ -50,7 +50,7 @@ public final class JsonPrimitive extends JsonElement { */ @SuppressWarnings("deprecation") // superclass constructor public JsonPrimitive(Number number) { - value = $Gson$Preconditions.checkNotNull(number); + value = Objects.requireNonNull(number); } /** @@ -60,7 +60,7 @@ public final class JsonPrimitive extends JsonElement { */ @SuppressWarnings("deprecation") // superclass constructor public JsonPrimitive(String string) { - value = $Gson$Preconditions.checkNotNull(string); + value = Objects.requireNonNull(string); } /** @@ -73,7 +73,7 @@ public final class JsonPrimitive extends JsonElement { public JsonPrimitive(Character c) { // convert characters to strings since in JSON, characters are represented as a single // character string - value = $Gson$Preconditions.checkNotNull(c).toString(); + value = Objects.requireNonNull(c).toString(); } /** diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java b/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java index f0e7d3fa..f1056efc 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java @@ -16,6 +16,8 @@ package com.google.gson.internal; +import java.util.Objects; + /** * A simple utility class used to check method Preconditions. * @@ -34,6 +36,12 @@ public final class $Gson$Preconditions { throw new UnsupportedOperationException(); } + /** + * @deprecated + * This is an internal Gson method. Use {@link Objects#requireNonNull(Object)} instead. + */ + // Only deprecated for now because external projects might be using this by accident + @Deprecated public static T checkNotNull(T obj) { if (obj == null) { throw new NullPointerException(); diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java index 9f34a0d4..965e010f 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java @@ -17,7 +17,7 @@ package com.google.gson.internal; import static com.google.gson.internal.$Gson$Preconditions.checkArgument; -import static com.google.gson.internal.$Gson$Preconditions.checkNotNull; +import static java.util.Objects.requireNonNull; import java.io.Serializable; import java.lang.reflect.Array; @@ -486,7 +486,7 @@ public final class $Gson$Types { private final Type[] typeArguments; public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) { - checkNotNull(rawType); + requireNonNull(rawType); // require an owner type if the raw type needs it if (rawType instanceof Class) { Class rawTypeAsClass = (Class) rawType; @@ -499,7 +499,7 @@ public final class $Gson$Types { this.rawType = canonicalize(rawType); this.typeArguments = typeArguments.clone(); for (int t = 0, length = this.typeArguments.length; t < length; t++) { - checkNotNull(this.typeArguments[t]); + requireNonNull(this.typeArguments[t]); checkNotPrimitive(this.typeArguments[t]); this.typeArguments[t] = canonicalize(this.typeArguments[t]); } @@ -553,7 +553,7 @@ public final class $Gson$Types { private final Type componentType; public GenericArrayTypeImpl(Type componentType) { - checkNotNull(componentType); + requireNonNull(componentType); this.componentType = canonicalize(componentType); } @@ -592,14 +592,14 @@ public final class $Gson$Types { checkArgument(upperBounds.length == 1); if (lowerBounds.length == 1) { - checkNotNull(lowerBounds[0]); + requireNonNull(lowerBounds[0]); checkNotPrimitive(lowerBounds[0]); checkArgument(upperBounds[0] == Object.class); this.lowerBound = canonicalize(lowerBounds[0]); this.upperBound = Object.class; } else { - checkNotNull(upperBounds[0]); + requireNonNull(upperBounds[0]); checkNotPrimitive(upperBounds[0]); this.lowerBound = null; this.upperBound = canonicalize(upperBounds[0]); diff --git a/gson/src/main/java/com/google/gson/internal/Streams.java b/gson/src/main/java/com/google/gson/internal/Streams.java index 419e4256..eafbbbec 100644 --- a/gson/src/main/java/com/google/gson/internal/Streams.java +++ b/gson/src/main/java/com/google/gson/internal/Streams.java @@ -28,6 +28,7 @@ import com.google.gson.stream.MalformedJsonException; import java.io.EOFException; import java.io.IOException; import java.io.Writer; +import java.util.Objects; /** * Reads and writes GSON parse trees over streams. @@ -105,7 +106,7 @@ public final class Streams { @Override public void write(String str, int off, int len) throws IOException { // Appendable.append turns null -> "null", which is not desired here - $Gson$Preconditions.checkNotNull(str); + Objects.requireNonNull(str); appendable.append(str, off, off + len); } diff --git a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java index 3995c631..46974dca 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java @@ -16,13 +16,6 @@ package com.google.gson.internal.bind; -import java.io.IOException; -import java.lang.reflect.Array; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - import com.google.gson.Gson; import com.google.gson.TypeAdapter; import com.google.gson.TypeAdapterFactory; @@ -31,6 +24,11 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.lang.reflect.Array; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Type; +import java.util.ArrayList; /** * Adapt an array of objects. diff --git a/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java index 0b7435f2..4719ea15 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java @@ -16,6 +16,15 @@ package com.google.gson.internal.bind; +import com.google.gson.JsonSyntaxException; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.JavaVersion; +import com.google.gson.internal.PreJava9DateFormatProvider; +import com.google.gson.internal.bind.util.ISO8601Utils; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; @@ -25,17 +34,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; - -import com.google.gson.JsonSyntaxException; -import com.google.gson.TypeAdapter; -import com.google.gson.TypeAdapterFactory; -import com.google.gson.internal.$Gson$Preconditions; -import com.google.gson.internal.JavaVersion; -import com.google.gson.internal.PreJava9DateFormatProvider; -import com.google.gson.internal.bind.util.ISO8601Utils; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; +import java.util.Objects; /** * This type adapter supports subclasses of date by defining a @@ -93,7 +92,7 @@ public final class DefaultDateTypeAdapter extends TypeAdapter private final List dateFormats = new ArrayList<>(); private DefaultDateTypeAdapter(DateType dateType, String datePattern) { - this.dateType = $Gson$Preconditions.checkNotNull(dateType); + this.dateType = Objects.requireNonNull(dateType); dateFormats.add(new SimpleDateFormat(datePattern, Locale.US)); if (!Locale.getDefault().equals(Locale.US)) { dateFormats.add(new SimpleDateFormat(datePattern)); @@ -101,7 +100,7 @@ public final class DefaultDateTypeAdapter extends TypeAdapter } private DefaultDateTypeAdapter(DateType dateType, int style) { - this.dateType = $Gson$Preconditions.checkNotNull(dateType); + this.dateType = Objects.requireNonNull(dateType); dateFormats.add(DateFormat.getDateInstance(style, Locale.US)); if (!Locale.getDefault().equals(Locale.US)) { dateFormats.add(DateFormat.getDateInstance(style)); @@ -112,7 +111,7 @@ public final class DefaultDateTypeAdapter extends TypeAdapter } private DefaultDateTypeAdapter(DateType dateType, int dateStyle, int timeStyle) { - this.dateType = $Gson$Preconditions.checkNotNull(dateType); + this.dateType = Objects.requireNonNull(dateType); dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US)); if (!Locale.getDefault().equals(Locale.US)) { dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle)); diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java index 80aebd9e..6ff1aa46 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/JsonTreeWriter.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * This writer creates a JsonElement. @@ -130,9 +131,7 @@ public final class JsonTreeWriter extends JsonWriter { } @Override public JsonWriter name(String name) throws IOException { - if (name == null) { - throw new NullPointerException("name == null"); - } + Objects.requireNonNull(name, "name == null"); if (stack.isEmpty() || pendingName != null) { throw new IllegalStateException(); } diff --git a/gson/src/main/java/com/google/gson/reflect/TypeToken.java b/gson/src/main/java/com/google/gson/reflect/TypeToken.java index 0124757d..547e5f5b 100644 --- a/gson/src/main/java/com/google/gson/reflect/TypeToken.java +++ b/gson/src/main/java/com/google/gson/reflect/TypeToken.java @@ -16,7 +16,6 @@ package com.google.gson.reflect; -import com.google.gson.internal.$Gson$Preconditions; import com.google.gson.internal.$Gson$Types; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; @@ -24,6 +23,7 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * Represents a generic type {@code T}. Java doesn't yet provide a way to @@ -72,7 +72,7 @@ public class TypeToken { */ @SuppressWarnings("unchecked") private TypeToken(Type type) { - this.type = $Gson$Types.canonicalize($Gson$Preconditions.checkNotNull(type)); + this.type = $Gson$Types.canonicalize(Objects.requireNonNull(type)); this.rawType = (Class) $Gson$Types.getRawType(this.type); this.hashCode = this.type.hashCode(); } @@ -325,8 +325,8 @@ public class TypeToken { * the raw type */ public static TypeToken getParameterized(Type rawType, Type... typeArguments) { - $Gson$Preconditions.checkNotNull(rawType); - $Gson$Preconditions.checkNotNull(typeArguments); + Objects.requireNonNull(rawType); + Objects.requireNonNull(typeArguments); // Perform basic validation here because this is the only public API where users // can create malformed parameterized types diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index 2359b303..99b09cfb 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -23,6 +23,7 @@ import java.io.EOFException; import java.io.IOException; import java.io.Reader; import java.util.Arrays; +import java.util.Objects; /** * Reads a JSON (RFC 7159) @@ -287,10 +288,7 @@ public class JsonReader implements Closeable { * Creates a new instance that reads a JSON-encoded stream from {@code in}. */ public JsonReader(Reader in) { - if (in == null) { - throw new NullPointerException("in == null"); - } - this.in = in; + this.in = Objects.requireNonNull(in, "in == null"); } /** diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 0ed7196c..0972dbab 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -16,17 +16,6 @@ package com.google.gson.stream; -import java.io.Closeable; -import java.io.Flushable; -import java.io.IOException; -import java.io.Writer; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Arrays; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import java.util.regex.Pattern; - import static com.google.gson.stream.JsonScope.DANGLING_NAME; import static com.google.gson.stream.JsonScope.EMPTY_ARRAY; import static com.google.gson.stream.JsonScope.EMPTY_DOCUMENT; @@ -35,6 +24,18 @@ 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_OBJECT; +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; +import java.io.Writer; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.regex.Pattern; + /** * Writes a JSON (RFC 7159) * encoded value to a stream, one token at a time. The stream includes both @@ -203,10 +204,7 @@ public class JsonWriter implements Closeable, Flushable { * {@link java.io.BufferedWriter BufferedWriter} if necessary. */ public JsonWriter(Writer out) { - if (out == null) { - throw new NullPointerException("out == null"); - } - this.out = out; + this.out = Objects.requireNonNull(out, "out == null"); } /** @@ -387,9 +385,7 @@ public class JsonWriter implements Closeable, Flushable { * @return this writer. */ public JsonWriter name(String name) throws IOException { - if (name == null) { - throw new NullPointerException("name == null"); - } + Objects.requireNonNull(name, "name == null"); if (deferredName != null) { throw new IllegalStateException(); } diff --git a/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java index 4b38c7c4..3136c58b 100644 --- a/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java +++ b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java @@ -16,7 +16,7 @@ package com.google.gson.protobuf; -import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.Objects.requireNonNull; import com.google.common.base.CaseFormat; import com.google.common.collect.MapMaker; @@ -104,7 +104,7 @@ public class ProtoTypeAdapter } public Builder setEnumSerialization(EnumSerialization enumSerialization) { - this.enumSerialization = checkNotNull(enumSerialization); + this.enumSerialization = requireNonNull(enumSerialization); return this; } @@ -144,7 +144,7 @@ public class ProtoTypeAdapter */ public Builder addSerializedNameExtension( Extension serializedNameExtension) { - serializedNameExtensions.add(checkNotNull(serializedNameExtension)); + serializedNameExtensions.add(requireNonNull(serializedNameExtension)); return this; } @@ -169,7 +169,7 @@ public class ProtoTypeAdapter */ public Builder addSerializedEnumValueExtension( Extension serializedEnumValueExtension) { - serializedEnumValueExtensions.add(checkNotNull(serializedEnumValueExtension)); + serializedEnumValueExtensions.add(requireNonNull(serializedEnumValueExtension)); return this; }