Class NullPointerTester
NullPointerException or UnsupportedOperationException whenever null is passed to a
parameter whose declaration or type isn't annotated with an annotation with the simple name
Nullable, CheckForNull, NullableType, or NullableDecl.
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.
NullPointerTester uses 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 by
NullPointerTester, the caller can provide a custom non-null default value for the parameter type
via setDefault(java.lang.Class<T>, T).
- Since:
- 10.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumStrategy for exception type matching used byNullPointerTester.private static final classstatic enumVisibility of any method or constructor. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ClassToInstanceMap<Object>private static final Function<TypeVariable<?>,AnnotatedType[]> private static final ImmutableSet<String> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate 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(Type type) (package private) ImmutableList<Method>getInstanceMethodsToTest(Class<?> c, NullPointerTester.Visibility minimalVisibility) ignore(Constructor<?> constructor) Ignoreconstructorin the tests that follow.Ignoremethodin the tests that follow.private static Function<TypeVariable<?>,AnnotatedType[]> private static Invokable<?,?> private static booleanReturns true if the given member is a method that overridesObject.equals(Object).private boolean(package private) static booleanisNullable(Invokable<?, ?> invokable) (package private) static booleanisNullable(Parameter param) private static booleanisNullable(Annotation[] annotations) private static booleanisNullableTypeVariable(Type type) (package private) static booleanisPrimitiveOrNullable(Parameter param) private <T> TnewDefaultReturningProxy(TypeToken<T> type) setDefault(Class<T> type, T value) Sets a default value that can be used for any parameter of typetype.voidRunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc.voidtestAllPublicInstanceMethods(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.voidRunstestMethod(java.lang.Object, java.lang.reflect.Method)on every public static method of classc, including those "inherited" from superclasses of the same package.voidtestConstructor(Constructor<?> ctor) Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.voidtestConstructorParameter(Constructor<?> ctor, int paramIndex) Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestConstructors(Class<?> c, NullPointerTester.Visibility minimalVisibility) RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility.voidtestInstanceMethods(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(Object instance, Method method) Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.voidtestMethodParameter(Object instance, Method method, int paramIndex) Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.private voidtestParameter(Object instance, Invokable<?, ?> invokable, int paramIndex, Class<?> testedClass) Verifies thatinvokableproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null.voidtestStaticMethods(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 Details
-
defaults
-
ignoredMembers
-
policy
-
NULLABLE_ANNOTATION_SIMPLE_NAMES
-
GET_ANNOTATED_BOUNDS
-
-
Constructor Details
-
NullPointerTester
public NullPointerTester()
-
-
Method Details
-
setDefault
Sets a default value that can be used for any parameter of typetype. Returns this object. -
ignore
Ignoremethodin the tests that follow. Returns this object.- Since:
- 13.0
-
ignore
Ignoreconstructorin the tests that follow. Returns this object.- Since:
- 22.0
-
testConstructors
RunstestConstructor(java.lang.reflect.Constructor<?>)on every constructor in classcthat has at leastminimalVisibility. -
testAllPublicConstructors
RunstestConstructor(java.lang.reflect.Constructor<?>)on every public constructor in classc. -
testStaticMethods
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
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
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<Method> getInstanceMethodsToTest(Class<?> c, NullPointerTester.Visibility minimalVisibility) -
testAllPublicInstanceMethods
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
Verifies thatmethodproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null.- Parameters:
instance- the instance to invokemethodon, or null ifmethodis static
-
testConstructor
Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhenever any of its non-nullable parameters are null. -
testMethodParameter
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
Verifies thatctorproduces aNullPointerExceptionorUnsupportedOperationExceptionwhen the parameter in positionparamIndexis null. If this parameter is marked nullable, this method does nothing. -
testParameter
private void testParameter(Object instance, Invokable<?, ?> invokable, int paramIndex, 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
-
getDefaultValue
-
defaultConverter
-
getFirstTypeParameter
-
newDefaultReturningProxy
-
invokable
-
isPrimitiveOrNullable
-
isNullable
-
isNullable
-
isNullableTypeVariable
-
isNullable
-
initGetAnnotatedBounds
-
isIgnored
-
isEquals
Returns true if 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.
-