public final class NullPointerTester
extends java.lang.Object
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).
| Modifier and Type | Class and Description |
|---|---|
private static class |
NullPointerTester.ExceptionTypePolicy
Strategy for exception type matching used by
NullPointerTester. |
private static class |
NullPointerTester.Signature |
static class |
NullPointerTester.Visibility
Visibility of any method or constructor.
|
| Modifier and Type | Field and Description |
|---|---|
private ClassToInstanceMap<java.lang.Object> |
defaults |
private static java.util.function.Function<java.lang.reflect.TypeVariable<?>,java.lang.reflect.AnnotatedType[]> |
GET_ANNOTATED_BOUNDS |
private java.util.List<java.lang.reflect.Member> |
ignoredMembers |
private static ImmutableSet<java.lang.String> |
NULLABLE_ANNOTATION_SIMPLE_NAMES |
private NullPointerTester.ExceptionTypePolicy |
policy |
| Constructor and Description |
|---|
NullPointerTester() |
| Modifier and Type | Method and 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> T |
getDefaultValue(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) |
NullPointerTester |
ignore(java.lang.reflect.Constructor<?> constructor)
Ignore
constructor in the tests that follow. |
NullPointerTester |
ignore(java.lang.reflect.Method method)
Ignore
method in the tests that follow. |
private static java.util.function.Function<java.lang.reflect.TypeVariable<?>,java.lang.reflect.AnnotatedType[]> |
initGetAnnotatedBounds() |
private static Invokable<?,?> |
invokable(java.lang.Object instance,
java.lang.reflect.Method method) |
private static boolean |
isEquals(java.lang.reflect.Member member)
Returns true if the given member is a method that overrides
Object.equals(Object). |
private boolean |
isIgnored(java.lang.reflect.Member member) |
private static boolean |
isNullable(java.lang.annotation.Annotation[] annotations) |
(package private) static boolean |
isNullable(Invokable<?,?> invokable) |
(package private) static boolean |
isNullable(Parameter param) |
private static boolean |
isNullableTypeVariable(java.lang.reflect.Type type) |
(package private) static boolean |
isPrimitiveOrNullable(Parameter param) |
private <T> T |
newDefaultReturningProxy(TypeToken<T> type) |
<T> NullPointerTester |
setDefault(java.lang.Class<T> type,
T value)
Sets a default value that can be used for any parameter of type
type. |
void |
testAllPublicConstructors(java.lang.Class<?> c)
Runs
testConstructor(java.lang.reflect.Constructor<?>) on every public constructor in class c. |
void |
testAllPublicInstanceMethods(java.lang.Object instance)
Runs
testMethod(java.lang.Object, java.lang.reflect.Method) on every public instance method of the class of instance,
including those inherited from superclasses of the same package. |
void |
testAllPublicStaticMethods(java.lang.Class<?> c)
Runs
testMethod(java.lang.Object, java.lang.reflect.Method) on every public static method of class c, including those
"inherited" from superclasses of the same package. |
void |
testConstructor(java.lang.reflect.Constructor<?> ctor)
Verifies that
ctor produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null. |
void |
testConstructorParameter(java.lang.reflect.Constructor<?> ctor,
int paramIndex)
Verifies that
ctor produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. |
void |
testConstructors(java.lang.Class<?> c,
NullPointerTester.Visibility minimalVisibility)
Runs
testConstructor(java.lang.reflect.Constructor<?>) on every constructor in class c that has at least minimalVisibility. |
void |
testInstanceMethods(java.lang.Object instance,
NullPointerTester.Visibility minimalVisibility)
Runs
testMethod(java.lang.Object, java.lang.reflect.Method) on every instance method of the class of instance with at
least minimalVisibility, including those inherited from superclasses of the same
package. |
void |
testMethod(java.lang.Object instance,
java.lang.reflect.Method method)
Verifies that
method produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null. |
void |
testMethodParameter(java.lang.Object instance,
java.lang.reflect.Method method,
int paramIndex)
Verifies that
method produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. |
private void |
testParameter(java.lang.Object instance,
Invokable<?,?> invokable,
int paramIndex,
java.lang.Class<?> testedClass)
Verifies that
invokable produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. |
void |
testStaticMethods(java.lang.Class<?> c,
NullPointerTester.Visibility minimalVisibility)
Runs
testMethod(java.lang.Object, java.lang.reflect.Method) on every static method of class c that has at least minimalVisibility, including those "inherited" from superclasses of the same package. |
private final ClassToInstanceMap<java.lang.Object> defaults
private final java.util.List<java.lang.reflect.Member> ignoredMembers
private NullPointerTester.ExceptionTypePolicy policy
private static final ImmutableSet<java.lang.String> NULLABLE_ANNOTATION_SIMPLE_NAMES
private static final java.util.function.Function<java.lang.reflect.TypeVariable<?>,java.lang.reflect.AnnotatedType[]> GET_ANNOTATED_BOUNDS
public <T> NullPointerTester setDefault(java.lang.Class<T> type, T value)
type. Returns this
object.public NullPointerTester ignore(java.lang.reflect.Method method)
method in the tests that follow. Returns this object.public NullPointerTester ignore(java.lang.reflect.Constructor<?> constructor)
constructor in the tests that follow. Returns this object.public void testConstructors(java.lang.Class<?> c,
NullPointerTester.Visibility minimalVisibility)
testConstructor(java.lang.reflect.Constructor<?>) on every constructor in class c that has at least minimalVisibility.public void testAllPublicConstructors(java.lang.Class<?> c)
testConstructor(java.lang.reflect.Constructor<?>) on every public constructor in class c.public void testStaticMethods(java.lang.Class<?> c,
NullPointerTester.Visibility minimalVisibility)
testMethod(java.lang.Object, java.lang.reflect.Method) on every static method of class c that has at least minimalVisibility, including those "inherited" from superclasses of the same package.public void testAllPublicStaticMethods(java.lang.Class<?> c)
testMethod(java.lang.Object, java.lang.reflect.Method) on every public static method of class c, including those
"inherited" from superclasses of the same package.public void testInstanceMethods(java.lang.Object instance,
NullPointerTester.Visibility minimalVisibility)
testMethod(java.lang.Object, java.lang.reflect.Method) on every instance method of the class of instance with at
least minimalVisibility, including those inherited from superclasses of the same
package.ImmutableList<java.lang.reflect.Method> getInstanceMethodsToTest(java.lang.Class<?> c, NullPointerTester.Visibility minimalVisibility)
public void testAllPublicInstanceMethods(java.lang.Object instance)
testMethod(java.lang.Object, java.lang.reflect.Method) on every public instance method of the class of instance,
including those inherited from superclasses of the same package.public void testMethod(java.lang.Object instance,
java.lang.reflect.Method method)
method produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null.instance - the instance to invoke method on, or null if method is staticpublic void testConstructor(java.lang.reflect.Constructor<?> ctor)
ctor produces a NullPointerException or UnsupportedOperationException whenever any of its non-nullable parameters are null.public void testMethodParameter(java.lang.Object instance,
java.lang.reflect.Method method,
int paramIndex)
method produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If
this parameter is marked nullable, this method does nothing.instance - the instance to invoke method on, or null if method is staticpublic void testConstructorParameter(java.lang.reflect.Constructor<?> ctor,
int paramIndex)
ctor produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If
this parameter is marked nullable, this method does nothing.private void testParameter(java.lang.Object instance,
Invokable<?,?> invokable,
int paramIndex,
java.lang.Class<?> testedClass)
invokable produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If
this parameter is marked nullable, this method does nothing.instance - the instance to invoke invokable on, or null if invokable is
staticprivate java.lang.Object[] buildParamList(Invokable<?,?> invokable, int indexOfParamToSetToNull)
private <T> T getDefaultValue(TypeToken<T> type)
private <F,T> Converter<F,T> defaultConverter(TypeToken<F> convertFromType, TypeToken<T> convertToType)
private static TypeToken<?> getFirstTypeParameter(java.lang.reflect.Type type)
private <T> T newDefaultReturningProxy(TypeToken<T> type)
private static Invokable<?,?> invokable(java.lang.Object instance, java.lang.reflect.Method method)
static boolean isPrimitiveOrNullable(Parameter param)
static boolean isNullable(Invokable<?,?> invokable)
static boolean isNullable(Parameter param)
private static boolean isNullableTypeVariable(java.lang.reflect.Type type)
private static boolean isNullable(java.lang.annotation.Annotation[] annotations)
private static java.util.function.Function<java.lang.reflect.TypeVariable<?>,java.lang.reflect.AnnotatedType[]> initGetAnnotatedBounds()
private boolean isIgnored(java.lang.reflect.Member member)
private static boolean isEquals(java.lang.reflect.Member member)
Object.equals(Object).
The documentation for Object.equals(java.lang.Object) says it should accept null, so don't require an
explicit @Nullable 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.