Skip to content

Commit

Permalink
Replace $Gson$Preconditions.checkNotNull with `Objects.requireNonNu…
Browse files Browse the repository at this point in the history
…ll` (#2180)

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

* Add back checkNotNull
  • Loading branch information
Marcono1234 committed Aug 22, 2022
1 parent b0b6834 commit 7f77ad4
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 105 deletions.
5 changes: 2 additions & 3 deletions gson/src/main/java/com/google/gson/FieldAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
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;
Expand Down Expand Up @@ -504,9 +505,7 @@ private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(final TypeAda
*/
@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;
Expand Down
68 changes: 32 additions & 36 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +28,26 @@
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
Expand Down Expand Up @@ -166,7 +166,7 @@ public GsonBuilder setVersion(double ignoreVersionsAfter) {
* @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;
}
Expand Down Expand Up @@ -310,8 +310,7 @@ public GsonBuilder disableInnerClassSerialization() {
* @since 1.3
*/
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
$Gson$Preconditions.checkNotNull(serializationPolicy);
this.longSerializationPolicy = serializationPolicy;
this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy);
return this;
}

Expand All @@ -334,8 +333,7 @@ public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
* @since 1.3
*/
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
$Gson$Preconditions.checkNotNull(fieldNamingStrategy);
this.fieldNamingPolicy = fieldNamingStrategy;
this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy);
return this;
}

Expand All @@ -347,8 +345,7 @@ public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrateg
* @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;
}

Expand All @@ -360,8 +357,7 @@ public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStra
* @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;
}

Expand All @@ -378,7 +374,7 @@ public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStra
* @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);
}
Expand All @@ -398,7 +394,7 @@ public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
* @since 1.7
*/
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
$Gson$Preconditions.checkNotNull(strategy);
Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, true, false);
return this;
}
Expand All @@ -416,7 +412,7 @@ public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy)
* @since 1.7
*/
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
$Gson$Preconditions.checkNotNull(strategy);
Objects.requireNonNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, false, true);
return this;
}
Expand Down Expand Up @@ -547,7 +543,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
*/
@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<?>
Expand All @@ -574,7 +570,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
* @since 2.1
*/
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
$Gson$Preconditions.checkNotNull(factory);
Objects.requireNonNull(factory);
factories.add(factory);
return this;
}
Expand All @@ -595,7 +591,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
*/
@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<?>);
Expand Down Expand Up @@ -669,7 +665,7 @@ public GsonBuilder disableJdkUnsafe() {
* @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;
}
Expand Down
10 changes: 5 additions & 5 deletions gson/src/main/java/com/google/gson/JsonPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -50,7 +50,7 @@ public JsonPrimitive(Boolean bool) {
*/
@SuppressWarnings("deprecation") // superclass constructor
public JsonPrimitive(Number number) {
value = $Gson$Preconditions.checkNotNull(number);
value = Objects.requireNonNull(number);
}

/**
Expand All @@ -60,7 +60,7 @@ public JsonPrimitive(Number number) {
*/
@SuppressWarnings("deprecation") // superclass constructor
public JsonPrimitive(String string) {
value = $Gson$Preconditions.checkNotNull(string);
value = Objects.requireNonNull(string);
}

/**
Expand All @@ -73,7 +73,7 @@ public JsonPrimitive(String string) {
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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.gson.internal;

import java.util.Objects;

/**
* A simple utility class used to check method Preconditions.
*
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -486,7 +486,7 @@ private static final class ParameterizedTypeImpl implements ParameterizedType, S
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;
Expand All @@ -499,7 +499,7 @@ public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments
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]);
}
Expand Down Expand Up @@ -553,7 +553,7 @@ private static final class GenericArrayTypeImpl implements GenericArrayType, Ser
private final Type componentType;

public GenericArrayTypeImpl(Type componentType) {
checkNotNull(componentType);
requireNonNull(componentType);
this.componentType = canonicalize(componentType);
}

Expand Down Expand Up @@ -592,14 +592,14 @@ public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
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]);
Expand Down
3 changes: 2 additions & 1 deletion gson/src/main/java/com/google/gson/internal/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.EOFException;
import java.io.IOException;
import java.io.Writer;
import java.util.Objects;

/**
* Reads and writes GSON parse trees over streams.
Expand Down Expand Up @@ -105,7 +106,7 @@ private static final class AppendableWriter extends Writer {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +24,11 @@
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.
Expand Down

0 comments on commit 7f77ad4

Please sign in to comment.