Class NullPointerTester


  • @Beta
    @GwtIncompatible
    public final class NullPointerTester
    extends java.lang.Object
    A test utility that verifies that your methods and constructors throw NullPointerException or UnsupportedOperationException whenever null is passed to a parameter that isn't annotated with Nullable.

    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
    • Constructor Detail

      • NullPointerTester

        public NullPointerTester()
    • 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 type type. Returns this object.
      • ignore

        public NullPointerTester ignore​(java.lang.reflect.Method method)
        Ignore method in the tests that follow. Returns this object.
        Since:
        13.0
      • testAllPublicInstanceMethods

        public 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.
      • testMethod

        public void testMethod​(@Nullable
                               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.
        Parameters:
        instance - the instance to invoke method on, or null if method is static
      • testConstructor

        public void testConstructor​(java.lang.reflect.Constructor<?> ctor)
        Verifies that ctor produces a NullPointerException or UnsupportedOperationException whenever any of its non-Nullable parameters are null.
      • testMethodParameter

        public void testMethodParameter​(@Nullable
                                        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. If this parameter is marked Nullable, this method does nothing.
        Parameters:
        instance - the instance to invoke method on, or null if method is static
      • testConstructorParameter

        public 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. 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 that invokable produces a NullPointerException or UnsupportedOperationException when the parameter in position paramIndex is null. If this parameter is marked Nullable, this method does nothing.
        Parameters:
        instance - the instance to invoke invokable on, or null if invokable is 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​(@Nullable
                                                      java.lang.Object instance,
                                                      java.lang.reflect.Method method)
      • isPrimitiveOrNullable

        static boolean isPrimitiveOrNullable​(Parameter param)
      • isNullable

        private static boolean isNullable​(Parameter param)
      • 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 overrides Object.equals(Object).

        The documentation for Object.equals(java.lang.Object) says it should accept null, so don't require an explicit {@link @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.