Class Objects


  • public final class Objects
    extends Object
    Provides static utility methods that delegate to Objects and are additionally annotated with SpotBugs null annotations.
    • Method Detail

      • requireNonNull

        @NonNull
        public static <T> T requireNonNull​(@CheckForNull
                                           T obj,
                                           @NonNull
                                           String message)
        Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below: public Foo(Bar bar, Baz baz) { this.bar = Objects.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); } Wrapper for Objects.requireNonNull(Object, String) that is annotated with SpotBugs annotations to correctly handle the null case.

        Will be removed when issue https://github.com/spotbugs/spotbugs/issues/456 is fixed

        Type Parameters:
        T - the type of the reference
        Parameters:
        obj - the object reference to check for nullity
        message - detail message to be used in the event that a NullPointerException is thrown
        Returns:
        obj if not null
        Throws:
        NullPointerException - if obj is null
      • requireNonNull

        @NonNull
        public static <T> T requireNonNull​(@CheckForNull
                                           T obj,
                                           @NonNull
                                           Supplier<String> messageSupplier)
        Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

        Unlike the method requireNonNull(Object, String), this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly. Wrapper for Objects.requireNonNull(Object, Supplier) that is annotated with SpotBugs annotations to correctly handle the null case.

        Will be removed when issue https://github.com/spotbugs/spotbugs/issues/456 is fixed

        Type Parameters:
        T - the type of the reference
        Parameters:
        obj - the object reference to check for nullity
        messageSupplier - detail message supplier to be used in the event that a NullPointerException is thrown
        Returns:
        obj if not null
        Throws:
        NullPointerException - if obj is null