Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null checks for GsonBuilder methods #2179

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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