Skip to content

Commit 7f77ad4

Browse files
authored
Replace $Gson$Preconditions.checkNotNull with Objects.requireNonNull (#2180)
* Replace $Gson$Preconditions.checkNotNull with Objects.requireNonNull * Add back checkNotNull
1 parent b0b6834 commit 7f77ad4

14 files changed

Lines changed: 98 additions & 105 deletions

File tree

gson/src/main/java/com/google/gson/FieldAttributes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.google.gson;
1818

19-
import com.google.gson.internal.$Gson$Preconditions;
2019
import java.lang.annotation.Annotation;
2120
import java.lang.reflect.Field;
2221
import java.lang.reflect.Type;
2322
import java.util.Arrays;
2423
import java.util.Collection;
24+
import java.util.Objects;
2525

2626
/**
2727
* A data object that stores attributes of a field.
@@ -42,8 +42,7 @@ public final class FieldAttributes {
4242
* @param f the field to pull attributes from
4343
*/
4444
public FieldAttributes(Field f) {
45-
$Gson$Preconditions.checkNotNull(f);
46-
this.field = f;
45+
this.field = Objects.requireNonNull(f);
4746
}
4847

4948
/**

gson/src/main/java/com/google/gson/Gson.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.HashMap;
5555
import java.util.List;
5656
import java.util.Map;
57+
import java.util.Objects;
5758
import java.util.concurrent.ConcurrentHashMap;
5859
import java.util.concurrent.ConcurrentMap;
5960
import java.util.concurrent.atomic.AtomicLong;
@@ -504,9 +505,7 @@ private static TypeAdapter<AtomicLongArray> atomicLongArrayAdapter(final TypeAda
504505
*/
505506
@SuppressWarnings("unchecked")
506507
public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
507-
if (type == null) {
508-
throw new NullPointerException("type must not be null");
509-
}
508+
Objects.requireNonNull(type, "type must not be null");
510509
TypeAdapter<?> cached = typeTokenCache.get(type);
511510
if (cached != null) {
512511
return (TypeAdapter<T>) cached;

gson/src/main/java/com/google/gson/GsonBuilder.java

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,6 @@
1616

1717
package com.google.gson;
1818

19-
import java.lang.reflect.Type;
20-
import java.text.DateFormat;
21-
import java.util.ArrayList;
22-
import java.util.Collections;
23-
import java.util.Date;
24-
import java.util.HashMap;
25-
import java.util.LinkedList;
26-
import java.util.List;
27-
import java.util.Map;
28-
29-
import com.google.gson.internal.$Gson$Preconditions;
30-
import com.google.gson.internal.Excluder;
31-
import com.google.gson.internal.bind.DefaultDateTypeAdapter;
32-
import com.google.gson.internal.bind.TreeTypeAdapter;
33-
import com.google.gson.internal.bind.TypeAdapters;
34-
import com.google.gson.internal.sql.SqlTypesSupport;
35-
import com.google.gson.reflect.TypeToken;
36-
import com.google.gson.stream.JsonReader;
37-
import com.google.gson.stream.JsonWriter;
38-
3919
import static com.google.gson.Gson.DEFAULT_COMPLEX_MAP_KEYS;
4020
import static com.google.gson.Gson.DEFAULT_DATE_PATTERN;
4121
import static com.google.gson.Gson.DEFAULT_ESCAPE_HTML;
@@ -48,6 +28,26 @@
4828
import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
4929
import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
5030

31+
import com.google.gson.internal.$Gson$Preconditions;
32+
import com.google.gson.internal.Excluder;
33+
import com.google.gson.internal.bind.DefaultDateTypeAdapter;
34+
import com.google.gson.internal.bind.TreeTypeAdapter;
35+
import com.google.gson.internal.bind.TypeAdapters;
36+
import com.google.gson.internal.sql.SqlTypesSupport;
37+
import com.google.gson.reflect.TypeToken;
38+
import com.google.gson.stream.JsonReader;
39+
import com.google.gson.stream.JsonWriter;
40+
import java.lang.reflect.Type;
41+
import java.text.DateFormat;
42+
import java.util.ArrayList;
43+
import java.util.Collections;
44+
import java.util.Date;
45+
import java.util.HashMap;
46+
import java.util.LinkedList;
47+
import java.util.List;
48+
import java.util.Map;
49+
import java.util.Objects;
50+
5151
/**
5252
* <p>Use this builder to construct a {@link Gson} instance when you need to set configuration
5353
* options other than the default. For {@link Gson} with default configuration, it is simpler to
@@ -166,7 +166,7 @@ public GsonBuilder setVersion(double ignoreVersionsAfter) {
166166
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
167167
*/
168168
public GsonBuilder excludeFieldsWithModifiers(int... modifiers) {
169-
$Gson$Preconditions.checkNotNull(modifiers);
169+
Objects.requireNonNull(modifiers);
170170
excluder = excluder.withModifiers(modifiers);
171171
return this;
172172
}
@@ -310,8 +310,7 @@ public GsonBuilder disableInnerClassSerialization() {
310310
* @since 1.3
311311
*/
312312
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
313-
$Gson$Preconditions.checkNotNull(serializationPolicy);
314-
this.longSerializationPolicy = serializationPolicy;
313+
this.longSerializationPolicy = Objects.requireNonNull(serializationPolicy);
315314
return this;
316315
}
317316

@@ -334,8 +333,7 @@ public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
334333
* @since 1.3
335334
*/
336335
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
337-
$Gson$Preconditions.checkNotNull(fieldNamingStrategy);
338-
this.fieldNamingPolicy = fieldNamingStrategy;
336+
this.fieldNamingPolicy = Objects.requireNonNull(fieldNamingStrategy);
339337
return this;
340338
}
341339

@@ -347,8 +345,7 @@ public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrateg
347345
* @see ToNumberPolicy#DOUBLE The default object-to-number strategy
348346
*/
349347
public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) {
350-
$Gson$Preconditions.checkNotNull(objectToNumberStrategy);
351-
this.objectToNumberStrategy = objectToNumberStrategy;
348+
this.objectToNumberStrategy = Objects.requireNonNull(objectToNumberStrategy);
352349
return this;
353350
}
354351

@@ -360,8 +357,7 @@ public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStra
360357
* @see ToNumberPolicy#LAZILY_PARSED_NUMBER The default number-to-number strategy
361358
*/
362359
public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStrategy) {
363-
$Gson$Preconditions.checkNotNull(numberToNumberStrategy);
364-
this.numberToNumberStrategy = numberToNumberStrategy;
360+
this.numberToNumberStrategy = Objects.requireNonNull(numberToNumberStrategy);
365361
return this;
366362
}
367363

@@ -378,7 +374,7 @@ public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStra
378374
* @since 1.4
379375
*/
380376
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
381-
$Gson$Preconditions.checkNotNull(strategies);
377+
Objects.requireNonNull(strategies);
382378
for (ExclusionStrategy strategy : strategies) {
383379
excluder = excluder.withExclusionStrategy(strategy, true, true);
384380
}
@@ -398,7 +394,7 @@ public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
398394
* @since 1.7
399395
*/
400396
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
401-
$Gson$Preconditions.checkNotNull(strategy);
397+
Objects.requireNonNull(strategy);
402398
excluder = excluder.withExclusionStrategy(strategy, true, false);
403399
return this;
404400
}
@@ -416,7 +412,7 @@ public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy)
416412
* @since 1.7
417413
*/
418414
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
419-
$Gson$Preconditions.checkNotNull(strategy);
415+
Objects.requireNonNull(strategy);
420416
excluder = excluder.withExclusionStrategy(strategy, false, true);
421417
return this;
422418
}
@@ -547,7 +543,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
547543
*/
548544
@SuppressWarnings({"unchecked", "rawtypes"})
549545
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
550-
$Gson$Preconditions.checkNotNull(type);
546+
Objects.requireNonNull(type);
551547
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
552548
|| typeAdapter instanceof JsonDeserializer<?>
553549
|| typeAdapter instanceof InstanceCreator<?>
@@ -574,7 +570,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
574570
* @since 2.1
575571
*/
576572
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
577-
$Gson$Preconditions.checkNotNull(factory);
573+
Objects.requireNonNull(factory);
578574
factories.add(factory);
579575
return this;
580576
}
@@ -595,7 +591,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
595591
*/
596592
@SuppressWarnings({"unchecked", "rawtypes"})
597593
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
598-
$Gson$Preconditions.checkNotNull(baseType);
594+
Objects.requireNonNull(baseType);
599595
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
600596
|| typeAdapter instanceof JsonDeserializer<?>
601597
|| typeAdapter instanceof TypeAdapter<?>);
@@ -669,7 +665,7 @@ public GsonBuilder disableJdkUnsafe() {
669665
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
670666
*/
671667
public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
672-
$Gson$Preconditions.checkNotNull(filter);
668+
Objects.requireNonNull(filter);
673669
reflectionFilters.addFirst(filter);
674670
return this;
675671
}

gson/src/main/java/com/google/gson/JsonPrimitive.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.google.gson;
1818

19-
import com.google.gson.internal.$Gson$Preconditions;
2019
import com.google.gson.internal.LazilyParsedNumber;
2120
import java.math.BigDecimal;
2221
import java.math.BigInteger;
22+
import java.util.Objects;
2323

2424
/**
2525
* A class representing a JSON primitive value. A primitive value
@@ -40,7 +40,7 @@ public final class JsonPrimitive extends JsonElement {
4040
*/
4141
@SuppressWarnings("deprecation") // superclass constructor
4242
public JsonPrimitive(Boolean bool) {
43-
value = $Gson$Preconditions.checkNotNull(bool);
43+
value = Objects.requireNonNull(bool);
4444
}
4545

4646
/**
@@ -50,7 +50,7 @@ public JsonPrimitive(Boolean bool) {
5050
*/
5151
@SuppressWarnings("deprecation") // superclass constructor
5252
public JsonPrimitive(Number number) {
53-
value = $Gson$Preconditions.checkNotNull(number);
53+
value = Objects.requireNonNull(number);
5454
}
5555

5656
/**
@@ -60,7 +60,7 @@ public JsonPrimitive(Number number) {
6060
*/
6161
@SuppressWarnings("deprecation") // superclass constructor
6262
public JsonPrimitive(String string) {
63-
value = $Gson$Preconditions.checkNotNull(string);
63+
value = Objects.requireNonNull(string);
6464
}
6565

6666
/**
@@ -73,7 +73,7 @@ public JsonPrimitive(String string) {
7373
public JsonPrimitive(Character c) {
7474
// convert characters to strings since in JSON, characters are represented as a single
7575
// character string
76-
value = $Gson$Preconditions.checkNotNull(c).toString();
76+
value = Objects.requireNonNull(c).toString();
7777
}
7878

7979
/**

gson/src/main/java/com/google/gson/internal/$Gson$Preconditions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gson.internal;
1818

19+
import java.util.Objects;
20+
1921
/**
2022
* A simple utility class used to check method Preconditions.
2123
*
@@ -34,6 +36,12 @@ public final class $Gson$Preconditions {
3436
throw new UnsupportedOperationException();
3537
}
3638

39+
/**
40+
* @deprecated
41+
* This is an internal Gson method. Use {@link Objects#requireNonNull(Object)} instead.
42+
*/
43+
// Only deprecated for now because external projects might be using this by accident
44+
@Deprecated
3745
public static <T> T checkNotNull(T obj) {
3846
if (obj == null) {
3947
throw new NullPointerException();

gson/src/main/java/com/google/gson/internal/$Gson$Types.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.gson.internal;
1818

1919
import static com.google.gson.internal.$Gson$Preconditions.checkArgument;
20-
import static com.google.gson.internal.$Gson$Preconditions.checkNotNull;
20+
import static java.util.Objects.requireNonNull;
2121

2222
import java.io.Serializable;
2323
import java.lang.reflect.Array;
@@ -486,7 +486,7 @@ private static final class ParameterizedTypeImpl implements ParameterizedType, S
486486
private final Type[] typeArguments;
487487

488488
public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
489-
checkNotNull(rawType);
489+
requireNonNull(rawType);
490490
// require an owner type if the raw type needs it
491491
if (rawType instanceof Class<?>) {
492492
Class<?> rawTypeAsClass = (Class<?>) rawType;
@@ -499,7 +499,7 @@ public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments
499499
this.rawType = canonicalize(rawType);
500500
this.typeArguments = typeArguments.clone();
501501
for (int t = 0, length = this.typeArguments.length; t < length; t++) {
502-
checkNotNull(this.typeArguments[t]);
502+
requireNonNull(this.typeArguments[t]);
503503
checkNotPrimitive(this.typeArguments[t]);
504504
this.typeArguments[t] = canonicalize(this.typeArguments[t]);
505505
}
@@ -553,7 +553,7 @@ private static final class GenericArrayTypeImpl implements GenericArrayType, Ser
553553
private final Type componentType;
554554

555555
public GenericArrayTypeImpl(Type componentType) {
556-
checkNotNull(componentType);
556+
requireNonNull(componentType);
557557
this.componentType = canonicalize(componentType);
558558
}
559559

@@ -592,14 +592,14 @@ public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
592592
checkArgument(upperBounds.length == 1);
593593

594594
if (lowerBounds.length == 1) {
595-
checkNotNull(lowerBounds[0]);
595+
requireNonNull(lowerBounds[0]);
596596
checkNotPrimitive(lowerBounds[0]);
597597
checkArgument(upperBounds[0] == Object.class);
598598
this.lowerBound = canonicalize(lowerBounds[0]);
599599
this.upperBound = Object.class;
600600

601601
} else {
602-
checkNotNull(upperBounds[0]);
602+
requireNonNull(upperBounds[0]);
603603
checkNotPrimitive(upperBounds[0]);
604604
this.lowerBound = null;
605605
this.upperBound = canonicalize(upperBounds[0]);

gson/src/main/java/com/google/gson/internal/Streams.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.EOFException;
2929
import java.io.IOException;
3030
import java.io.Writer;
31+
import java.util.Objects;
3132

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

106107
@Override public void write(String str, int off, int len) throws IOException {
107108
// Appendable.append turns null -> "null", which is not desired here
108-
$Gson$Preconditions.checkNotNull(str);
109+
Objects.requireNonNull(str);
109110
appendable.append(str, off, off + len);
110111
}
111112

gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616

1717
package com.google.gson.internal.bind;
1818

19-
import java.io.IOException;
20-
import java.lang.reflect.Array;
21-
import java.lang.reflect.GenericArrayType;
22-
import java.lang.reflect.Type;
23-
import java.util.ArrayList;
24-
import java.util.List;
25-
2619
import com.google.gson.Gson;
2720
import com.google.gson.TypeAdapter;
2821
import com.google.gson.TypeAdapterFactory;
@@ -31,6 +24,11 @@
3124
import com.google.gson.stream.JsonReader;
3225
import com.google.gson.stream.JsonToken;
3326
import com.google.gson.stream.JsonWriter;
27+
import java.io.IOException;
28+
import java.lang.reflect.Array;
29+
import java.lang.reflect.GenericArrayType;
30+
import java.lang.reflect.Type;
31+
import java.util.ArrayList;
3432

3533
/**
3634
* Adapt an array of objects.

0 commit comments

Comments
 (0)