Update Precondition checks and have it throw a NPE instead.

This commit is contained in:
Joel Leitch 2011-03-16 07:23:44 +00:00
parent 7b223a8983
commit 279c0e87ed
22 changed files with 32 additions and 51 deletions

View File

@ -48,8 +48,7 @@ class DelegatingJsonElementVisitor implements JsonElementVisitor {
private final JsonElementVisitor delegate;
protected DelegatingJsonElementVisitor(JsonElementVisitor delegate) {
Preconditions.checkNotNull(delegate);
this.delegate = delegate;
this.delegate = Preconditions.checkNotNull(delegate);
}
public void endArray(JsonArray array) throws IOException {

View File

@ -28,8 +28,7 @@ final class DisjunctionExclusionStrategy implements ExclusionStrategy {
private final Collection<ExclusionStrategy> strategies;
DisjunctionExclusionStrategy(Collection<ExclusionStrategy> strategies) {
Preconditions.checkNotNull(strategies);
this.strategies = strategies;
this.strategies = Preconditions.checkNotNull(strategies);
}
public boolean shouldSkipField(FieldAttributes f) {

View File

@ -57,13 +57,12 @@ public final class FieldAttributes {
* @param f the field to pull attributes from
*/
FieldAttributes(final Class<?> declaringClazz, final Field f) {
Preconditions.checkNotNull(declaringClazz);
this.declaringClazz = declaringClazz;
name = f.getName();
declaredType = f.getType();
isSynthetic = f.isSynthetic();
modifiers = f.getModifiers();
field = f;
this.declaringClazz = Preconditions.checkNotNull(declaringClazz);
this.name = f.getName();
this.declaredType = f.getType();
this.isSynthetic = f.isSynthetic();
this.modifiers = f.getModifiers();
this.field = f;
}
private static int getMaxCacheSize() {

View File

@ -28,8 +28,7 @@ final class FieldNamingStrategy2Adapter implements FieldNamingStrategy2 {
private final FieldNamingStrategy adaptee;
public FieldNamingStrategy2Adapter(FieldNamingStrategy adaptee) {
Preconditions.checkNotNull(adaptee);
this.adaptee = adaptee;
this.adaptee = Preconditions.checkNotNull(adaptee);
}
public String translateName(FieldAttributes f) {

View File

@ -364,8 +364,8 @@ public final class GsonBuilder {
* @since 1.7
*/
public GsonBuilder setExclusionStrategies(Mode mode, ExclusionStrategy... strategies) {
Preconditions.checkNotNull(mode);
Set<ExclusionStrategy> strategySet = (mode == Mode.SERIALIZE)
Set<ExclusionStrategy> strategySet =
(Preconditions.checkNotNull(mode) == Mode.SERIALIZE)
? serializeExclusionStrategies : deserializeExclusionStrategies;
strategySet.addAll(Arrays.asList(strategies));
return this;

View File

@ -41,12 +41,11 @@ abstract class JsonDeserializationVisitor<T> implements ObjectNavigator.Visitor
ObjectNavigatorFactory factory, ObjectConstructor objectConstructor,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
JsonDeserializationContext context) {
Preconditions.checkNotNull(json);
this.targetType = targetType;
this.factory = factory;
this.objectConstructor = objectConstructor;
this.deserializers = deserializers;
this.json = json;
this.json = Preconditions.checkNotNull(json);
this.context = context;
this.constructed = false;
}

View File

@ -40,8 +40,7 @@ class JsonDeserializerExceptionWrapper<T> implements JsonDeserializer<T> {
* @throws IllegalArgumentException if {@code delegate} is {@code null}.
*/
JsonDeserializerExceptionWrapper(JsonDeserializer<T> delegate) {
Preconditions.checkNotNull(delegate);
this.delegate = delegate;
this.delegate = Preconditions.checkNotNull(delegate);
}
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)

View File

@ -52,11 +52,10 @@ public final class JsonObject extends JsonElement {
* @param value the member object.
*/
public void add(String property, JsonElement value) {
Preconditions.checkNotNull(property);
if (value == null) {
value = JsonNull.createJsonNull();
}
members.put(property, value);
members.put(Preconditions.checkNotNull(property), value);
}
/**

View File

@ -211,8 +211,7 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
}
private void assignToRoot(JsonElement newRoot) {
Preconditions.checkNotNull(newRoot);
root = newRoot;
root = Preconditions.checkNotNull(newRoot);
}
private boolean isFieldNull(FieldAttributes f, Object obj) {

View File

@ -36,7 +36,6 @@ final class MemoryRefStack {
*/
public ObjectTypePair push(ObjectTypePair obj) {
Preconditions.checkNotNull(obj);
return stack.push(obj);
}

View File

@ -63,8 +63,7 @@ final class ModifyFirstLetterNamingPolicy extends RecursiveFieldNamingPolicy {
* @throws IllegalArgumentException if {@code modifier} is null
*/
ModifyFirstLetterNamingPolicy(LetterModifier modifier) {
Preconditions.checkNotNull(modifier);
this.letterModifier = modifier;
this.letterModifier = Preconditions.checkNotNull(modifier);
}
@Override

View File

@ -88,10 +88,8 @@ final class ObjectNavigator {
* object.
*/
ObjectNavigator(ObjectTypePair objTypePair, ExclusionStrategy exclusionStrategy) {
Preconditions.checkNotNull(exclusionStrategy);
this.objTypePair = objTypePair;
this.exclusionStrategy = exclusionStrategy;
this.exclusionStrategy = Preconditions.checkNotNull(exclusionStrategy);
}
/**

View File

@ -37,9 +37,8 @@ final class ObjectNavigatorFactory {
* names
*/
public ObjectNavigatorFactory(ExclusionStrategy strategy, FieldNamingStrategy2 fieldNamingPolicy) {
Preconditions.checkNotNull(fieldNamingPolicy);
this.strategy = (strategy == null ? new NullExclusionStrategy() : strategy);
this.fieldNamingPolicy = fieldNamingPolicy;
this.fieldNamingPolicy = Preconditions.checkNotNull(fieldNamingPolicy);
}
/**

View File

@ -31,7 +31,9 @@ package com.google.gson;
*/
final class Preconditions {
public static <T> T checkNotNull(T obj) {
checkArgument(obj != null);
if (obj == null) {
throw new NullPointerException();
}
return obj;
}

View File

@ -76,12 +76,8 @@ final class Primitives {
* @see Class#isPrimitive
*/
public static boolean isWrapperType(Class<?> type) {
return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(checkNotNull(type));
}
private static Class<?> checkNotNull(Class<?> type) {
Preconditions.checkNotNull(type);
return type;
return WRAPPER_TO_PRIMITIVE_TYPE.containsKey(
Preconditions.checkNotNull(type));
}
/**
@ -94,11 +90,10 @@ final class Primitives {
* </pre>
*/
public static <T> Class<T> wrap(Class<T> type) {
checkNotNull(type);
// cast is safe: long.class and Long.class are both of type Class<Long>
@SuppressWarnings("unchecked")
Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(type);
Class<T> wrapped = (Class<T>) PRIMITIVE_TO_WRAPPER_TYPE.get(
Preconditions.checkNotNull(type));
return (wrapped == null) ? type : wrapped;
}
@ -112,11 +107,10 @@ final class Primitives {
* </pre>
*/
public static <T> Class<T> unwrap(Class<T> type) {
checkNotNull(type);
// cast is safe: long.class and Long.class are both of type Class<Long>
@SuppressWarnings("unchecked")
Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(type);
Class<T> unwrapped = (Class<T>) WRAPPER_TO_PRIMITIVE_TYPE.get(
Preconditions.checkNotNull(type));
return (unwrapped == null) ? type : unwrapped;
}
}

View File

@ -30,7 +30,6 @@ import java.util.Collection;
abstract class RecursiveFieldNamingPolicy implements FieldNamingStrategy2 {
public final String translateName(FieldAttributes f) {
Preconditions.checkNotNull(f);
return translateName(f.getName(), f.getDeclaredType(), f.getAnnotations());
}

View File

@ -40,7 +40,6 @@ final class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNam
}
public String translateName(FieldAttributes f) {
Preconditions.checkNotNull(f);
SerializedName serializedName = f.getAnnotation(SerializedName.class);
return serializedName == null ? delegate.translateName(f)
: fieldNameValidator.validate(serializedName.value());

View File

@ -33,7 +33,7 @@ public class CamelCaseSeparatorNamingPolicyTest extends TestCase {
try {
new CamelCaseSeparatorNamingPolicy(null);
fail("Null separator string is not supported");
} catch (IllegalArgumentException expected) { }
} catch (NullPointerException expected) { }
try {
new CamelCaseSeparatorNamingPolicy("");

View File

@ -41,7 +41,7 @@ public class DisjunctionExclusionStrategyTest extends TestCase {
List<ExclusionStrategy> constructorParam = null;
new DisjunctionExclusionStrategy(constructorParam);
fail("Should throw an exception");
} catch (IllegalArgumentException expected) { }
} catch (NullPointerException expected) { }
}
public void testSkipFieldsWithMixedTrueAndFalse() throws Exception {

View File

@ -42,6 +42,6 @@ public class JavaFieldNamingPolicyTest extends TestCase {
try {
namingPolicy.translateName((FieldAttributes) null);
fail("Should have thrown an exception");
} catch (IllegalArgumentException expected) { }
} catch (NullPointerException expected) { }
}
}

View File

@ -58,7 +58,7 @@ public class JsonObjectTest extends TestCase {
try {
jsonObj.add(null, JsonNull.createJsonNull());
fail("Should not allow null property names.");
} catch (IllegalArgumentException expected) { }
} catch (NullPointerException expected) { }
jsonObj.add("", JsonNull.createJsonNull());
jsonObj.add(" \t", JsonNull.createJsonNull());

View File

@ -29,7 +29,7 @@ public class ModifyFirstLetterNamingPolicyTest extends TestCase {
try {
new ModifyFirstLetterNamingPolicy(null);
fail("Null values are not allowed as a constructor parameters");
} catch (IllegalArgumentException expected) { }
} catch (NullPointerException expected) { }
}
public void testLowerCaseFirstLetter() throws Exception {