Skip to content

Commit

Permalink
Add null checks for GsonBuilder methods (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 authored Aug 20, 2022
1 parent 18b9ba4 commit b84b221
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +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);
excluder = excluder.withModifiers(modifiers);
return this;
}
Expand Down Expand Up @@ -309,32 +310,31 @@ public GsonBuilder disableInnerClassSerialization() {
* @since 1.3
*/
public GsonBuilder setLongSerializationPolicy(LongSerializationPolicy serializationPolicy) {
$Gson$Preconditions.checkNotNull(serializationPolicy);
this.longSerializationPolicy = serializationPolicy;
return this;
}

/**
* Configures Gson to apply a specific naming policy to an object's field during serialization
* Configures Gson to apply a specific naming policy to an object's fields during serialization
* and deserialization.
*
* @param namingConvention the JSON field naming convention to use for serialization and
* deserialization.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* <p>This method just delegates to {@link #setFieldNamingStrategy(FieldNamingStrategy)}.
*/
public GsonBuilder setFieldNamingPolicy(FieldNamingPolicy namingConvention) {
this.fieldNamingPolicy = namingConvention;
return this;
return setFieldNamingStrategy(namingConvention);
}

/**
* Configures Gson to apply a specific naming policy strategy to an object's field during
* Configures Gson to apply a specific naming strategy to an object's fields during
* serialization and deserialization.
*
* @param fieldNamingStrategy the actual naming strategy to apply to the fields
* @param fieldNamingStrategy the naming strategy to apply to the fields
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3
*/
public GsonBuilder setFieldNamingStrategy(FieldNamingStrategy fieldNamingStrategy) {
$Gson$Preconditions.checkNotNull(fieldNamingStrategy);
this.fieldNamingPolicy = fieldNamingStrategy;
return this;
}
Expand All @@ -347,6 +347,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;
return this;
}
Expand All @@ -359,6 +360,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;
return this;
}
Expand All @@ -376,6 +378,7 @@ public GsonBuilder setNumberToNumberStrategy(ToNumberStrategy numberToNumberStra
* @since 1.4
*/
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
$Gson$Preconditions.checkNotNull(strategies);
for (ExclusionStrategy strategy : strategies) {
excluder = excluder.withExclusionStrategy(strategy, true, true);
}
Expand All @@ -395,6 +398,7 @@ public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies) {
* @since 1.7
*/
public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy) {
$Gson$Preconditions.checkNotNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, true, false);
return this;
}
Expand All @@ -412,6 +416,7 @@ public GsonBuilder addSerializationExclusionStrategy(ExclusionStrategy strategy)
* @since 1.7
*/
public GsonBuilder addDeserializationExclusionStrategy(ExclusionStrategy strategy) {
$Gson$Preconditions.checkNotNull(strategy);
excluder = excluder.withExclusionStrategy(strategy, false, true);
return this;
}
Expand Down Expand Up @@ -536,6 +541,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
$Gson$Preconditions.checkNotNull(type);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof InstanceCreator<?>
Expand All @@ -562,6 +568,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
* @since 2.1
*/
public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
$Gson$Preconditions.checkNotNull(factory);
factories.add(factory);
return this;
}
Expand All @@ -582,6 +589,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
$Gson$Preconditions.checkNotNull(baseType);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof TypeAdapter<?>);
Expand Down Expand Up @@ -655,8 +663,7 @@ public GsonBuilder disableJdkUnsafe() {
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
*/
public GsonBuilder addReflectionAccessFilter(ReflectionAccessFilter filter) {
if (filter == null) throw new NullPointerException();

$Gson$Preconditions.checkNotNull(filter);
reflectionFilters.addFirst(filter);
return this;
}
Expand Down

0 comments on commit b84b221

Please sign in to comment.