Replace $Gson$Preconditions.checkNotNull with Objects.requireNonNull (#2180)

* Replace $Gson$Preconditions.checkNotNull with Objects.requireNonNull

* Add back checkNotNull
This commit is contained in:
Marcono1234 2022-08-22 16:22:32 +02:00 committed by GitHub
parent b0b6834157
commit 7f77ad4ff6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 101 additions and 108 deletions

View File

@ -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);
}
/**

View File

@ -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 <T> TypeAdapter<T> getAdapter(TypeToken<T> 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<T>) cached;

View File

@ -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;
/**
* <p>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;
}

View File

@ -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();
}
/**

View File

@ -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> T checkNotNull(T obj) {
if (obj == null) {
throw new NullPointerException();

View File

@ -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]);

View File

@ -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);
}

View File

@ -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.

View File

@ -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<T extends Date> extends TypeAdapter<T>
private final List<DateFormat> dateFormats = new ArrayList<>();
private DefaultDateTypeAdapter(DateType<T> 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<T extends Date> extends TypeAdapter<T>
}
private DefaultDateTypeAdapter(DateType<T> 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<T extends Date> extends TypeAdapter<T>
}
private DefaultDateTypeAdapter(DateType<T> 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));

View File

@ -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();
}

View File

@ -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<T> {
*/
@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<? super T>) $Gson$Types.getRawType(this.type);
this.hashCode = this.type.hashCode();
}
@ -325,8 +325,8 @@ public class TypeToken<T> {
* 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

View File

@ -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 (<a href="http://www.ietf.org/rfc/rfc7159.txt">RFC 7159</a>)
@ -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");
}
/**

View File

@ -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 (<a href="http://www.ietf.org/rfc/rfc7159.txt">RFC 7159</a>)
* 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();
}

View File

@ -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<FieldOptions, String> serializedNameExtension) {
serializedNameExtensions.add(checkNotNull(serializedNameExtension));
serializedNameExtensions.add(requireNonNull(serializedNameExtension));
return this;
}
@ -169,7 +169,7 @@ public class ProtoTypeAdapter
*/
public Builder addSerializedEnumValueExtension(
Extension<EnumValueOptions, String> serializedEnumValueExtension) {
serializedEnumValueExtensions.add(checkNotNull(serializedEnumValueExtension));
serializedEnumValueExtensions.add(requireNonNull(serializedEnumValueExtension));
return this;
}