Class NullPointerTester
- java.lang.Object
-
- com.google.common.testing.NullPointerTester
-
public final class NullPointerTester extends java.lang.ObjectA test utility that verifies that your methods and constructors throwNullPointerExceptionorUnsupportedOperationExceptionwhenever null is passed to a parameter whose declaration or type isn't annotated with an annotation with the simple nameNullable, ,NullableType, orNullableDecl.The tested methods and constructors are invoked -- each time with one parameter being null and the rest not null -- and the test fails if no expected exception is thrown.
NullPointerTesteruses best effort to pick non-null default values for many common JDK and Guava types, and also for interfaces and public classes that have public parameter-less constructors. When the non-null default value for a particular parameter type cannot be provided byNullPointerTester, the caller can provide a custom non-null default value for the parameter type viasetDefault(java.lang.Class<T>, T).- Since:
- 10.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classNullPointerTester.ExceptionTypePolicyStrategy for exception type matching used byNullPointerTester.private static classNullPointerTester.Signaturestatic classNullPointerTester.VisibilityVisibility of any method or constructor.
-
Field Summary
Fields Modifier and Type Field Description private ClassToInstanceMap<java.lang.Object>defaultsprivate java.util.List<java.lang.reflect.Member>ignoredMembersprivate static ImmutableSet<java.lang.String>NULLABLE_ANNOTATION_SIMPLE_NAMESprivate NullPointerTester.ExceptionTypePolicypolicy
-
Constructor Summary
Constructors Constructor Description NullPointerTester()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.Object[]buildParamList(Invokable<?,?> invokable, int indexOfParamToSetToNull)private <F,T>
Converter<F,T>defaultConverter(TypeToken<F> convertFromType, TypeToken<T> convertToType)private <T> TgetDefaultValue(TypeToken<T> type)private static TypeToken<?>getFirstTypeParameter(java.lang.reflect.Type type)(package private) ImmutableList<java.lang.reflect.Method>getInstanceMethodsToTest(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)NullPointerTesterignore(java.lang.reflect.Constructor<?> constructor)Ignoreconstructorin the tests that follow.NullPointerTesterignore(java.lang.reflect.Method method)Ignoremethodin the tests that follow.private static Invokable<?,?>invokable(java.lang.Object instance, java.lang.reflect.Method method)private static booleanisEquals(java.lang.reflect.Member member)Returns true if the the given member is a method that overridesObject.equals(Object).private booleanisIgnored(java.lang.reflect.Member member)(package private) static booleanisNullable(Invokable<?,?> invokable)(package private) static booleanisNullable(Parameter param)private static booleanisNullable(java.lang.annotation.Annotation[] annotations)(package private) static booleanisPrimitiveOrNullable(Parameter param)private <T> TnewDefaultReturningProxy(TypeToken<T> type)<T> NullPointerTestersetDefault(java.lang.Class<T> type, T value)Sets a default value that can be used for any parameter of typetype.voidtestAllPublicConstructors(java.lang.Class<?> c)RunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc.voidtestAllPublicInstanceMethods(java.lang.Object instance)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public instance method of the class ofinstance, including those inherited from superclasses of the same package.voidtestAllPublicStaticMethods(java.lang.Class<?> c)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public static method of classc, including those "inherited" from superclasses of the same package.voidtestConstructor(java.lang.reflect.Constructor<?> ctor)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.voidtestConstructorParameter(java.lang.reflect.Constructor<?> ctor, int paramIndex)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestConstructors(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility.voidtestInstanceMethods(java.lang.Object instance, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every instance method of the class ofinstancewith at leastminimalVisibility, including those inherited from superclasses of the same package.voidtestMethod(java.lang.Object instance, java.lang.reflect.Method method)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.voidtestMethodParameter(java.lang.Object instance, java.lang.reflect.Method method, int paramIndex)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.private voidtestParameter(java.lang.Object instance, Invokable<?,?> invokable, int paramIndex, java.lang.Class<?> testedClass)Verifies thatinvokableproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestStaticMethods(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every static method of classcthat has at leastminimalVisibility, including those "inherited" from superclasses of the same package.
-
-
-
Field Detail
-
defaults
private final ClassToInstanceMap<java.lang.Object> defaults
-
ignoredMembers
private final java.util.List<java.lang.reflect.Member> ignoredMembers
-
policy
private NullPointerTester.ExceptionTypePolicy policy
-
NULLABLE_ANNOTATION_SIMPLE_NAMES
private static final ImmutableSet<java.lang.String> NULLABLE_ANNOTATION_SIMPLE_NAMES
-
-
Method Detail
-
setDefault
public <T> NullPointerTester setDefault(java.lang.Class<T> type, T value)
Sets a default value that can be used for any parameter of typetype. Returns this object.
-
ignore
public NullPointerTester ignore(java.lang.reflect.Method method)
Ignoremethodin the tests that follow. Returns this object.- Since:
- 13.0
-
ignore
public NullPointerTester ignore(java.lang.reflect.Constructor<?> constructor)
Ignoreconstructorin the tests that follow. Returns this object.- Since:
- 22.0
-
testConstructors
public void testConstructors(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility.
-
testAllPublicConstructors
public void testAllPublicConstructors(java.lang.Class<?> c)
RunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc.
-
testStaticMethods
public void testStaticMethods(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every static method of classcthat has at leastminimalVisibility, including those "inherited" from superclasses of the same package.
-
testAllPublicStaticMethods
public void testAllPublicStaticMethods(java.lang.Class<?> c)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public static method of classc, including those "inherited" from superclasses of the same package.
-
testInstanceMethods
public void testInstanceMethods(java.lang.Object instance, NullPointerTester.Visibility minimalVisibility)RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every instance method of the class ofinstancewith at leastminimalVisibility, including those inherited from superclasses of the same package.
-
getInstanceMethodsToTest
ImmutableList<java.lang.reflect.Method> getInstanceMethodsToTest(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)
-
testAllPublicInstanceMethods
public void testAllPublicInstanceMethods(java.lang.Object instance)
RunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public instance method of the class ofinstance, including those inherited from superclasses of the same package.
-
testMethod
public void testMethod(java.lang.Object instance, java.lang.reflect.Method method)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.- Parameters:
instance- the instance to invokemethodon, or null ifmethodis static
-
testConstructor
public void testConstructor(java.lang.reflect.Constructor<?> ctor)
Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.
-
testMethodParameter
public void testMethodParameter(java.lang.Object instance, java.lang.reflect.Method method, int paramIndex)Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is marked nullable, this method does nothing.- Parameters:
instance- the instance to invokemethodon, or null ifmethodis static
-
testConstructorParameter
public void testConstructorParameter(java.lang.reflect.Constructor<?> ctor, int paramIndex)Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is marked nullable, this method does nothing.
-
testParameter
private void testParameter(java.lang.Object instance, Invokable<?,?> invokable, int paramIndex, java.lang.Class<?> testedClass)Verifies thatinvokableproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is marked nullable, this method does nothing.- Parameters:
instance- the instance to invokeinvokableon, or null ifinvokableis static
-
buildParamList
private java.lang.Object[] buildParamList(Invokable<?,?> invokable, int indexOfParamToSetToNull)
-
getDefaultValue
private <T> T getDefaultValue(TypeToken<T> type)
-
defaultConverter
private <F,T> Converter<F,T> defaultConverter(TypeToken<F> convertFromType, TypeToken<T> convertToType)
-
getFirstTypeParameter
private static TypeToken<?> getFirstTypeParameter(java.lang.reflect.Type type)
-
newDefaultReturningProxy
private <T> T newDefaultReturningProxy(TypeToken<T> type)
-
invokable
private static Invokable<?,?> invokable(java.lang.Object instance, java.lang.reflect.Method method)
-
isPrimitiveOrNullable
static boolean isPrimitiveOrNullable(Parameter param)
-
isNullable
static boolean isNullable(Invokable<?,?> invokable)
-
isNullable
static boolean isNullable(Parameter param)
-
isNullable
private static boolean isNullable(java.lang.annotation.Annotation[] annotations)
-
isIgnored
private boolean isIgnored(java.lang.reflect.Member member)
-
isEquals
private static boolean isEquals(java.lang.reflect.Member member)
Returns true if the the given member is a method that overridesObject.equals(Object).The documentation for
Object.equals(java.lang.Object)says it should accept null, so don't require an explicit annotation (see #1819).It is not necessary to consider visibility, return type, or type parameter declarations. The declaration of a method with the same name and formal parameters as
Object.equals(java.lang.Object)that is not public and boolean-returning, or that declares any type parameters, would be rejected at compile-time.
-
-