nonNull and similar methods [was Re: First round of java.util.Objects for code review (bug 6797535)]

Stephen Colebourne scolebourne at joda.org
Wed Oct 14 10:38:26 UTC 2009


All,
While I agree that the nonNull methods below are useful and should be in 
the JDK, I question whether they should be on j.u.Objects.

I believe that there is a whole category of methods to pre-validate the 
arguments of a method, such as Commons Lang Validate, or Google Prevalidate.

http://commons.apache.org/lang/apidocs/org/apache/commons/lang/Validate.html
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/base/Preconditions.html

Adding such a set of validation methods would be a useful way to allow 
developers to strengthen method input checking.

Stephen


Joshua Bloch wrote:
> Joe,
> 
> Hi.  I've attached a file containing the methods and a JTReg "basic 
> test" for inclusion in your BasicObjectTests.  I adhered to your style, 
> for easy integration. If you could take it from here, I'd be ever so 
> grateful.
> 
>     Thanks,
> 
>     Josh
> 
> On Thu, Oct 8, 2009 at 6:21 PM, Joe Darcy <Joe.Darcy at sun.com 
> <mailto:Joe.Darcy at sun.com>> wrote:
> 
> 
>         I strongly suggest that you do add these two methods:
> 
>            /**
>             * Checks that the specified object reference is not {@code
>         null}. This
>             * method is designed primarily for doing parameter
>         validation in methods
>             * and constructors, as demonstrated below:
>             * <pre>
>             * public Foo(Bar bar) {
>             *     this.bar = Objects.nonNull(bar);
>             * }
>             * </pre>
>             *
>             * @param obj the object reference to check for nullity
>             * @return {@code obj} if not {@code null}
>             * @throws NullPointerException if {@code obj} is {@code null}
>             */
>            public static <T> T nonNull(T obj) {
>                if (obj == null)
>                    throw new NullPointerException();
>                return obj;
>            }
> 
>            /**
>             * Checks that the specified object reference is not {@code
>         null} and
>             * throws a customized {@Link NullPointerException} if it is.
>         This method
>             * is designed primarily for doing parameter validation in
>         methods and
>             * constructors with multiple parameters, as demonstrated below:
>             * <pre>
>             * public Foo(Bar bar, Baz baz) {
>             *     this.bar = Objects.nonNull(bar, "bar must not be null");
>             *     this.baz = Objects.nonNull(baz, "baz must not be null");
>             * }
>             * </pre>
>             *
>             * @param obj     the object reference to check for nullity
>             * @param message detail message to be used in the event that
>         a {@code
>             *                NullPointerException} is thrown
>             * @return {@code obj} if not {@code null}
>             * @throws NullPointerException if {@code obj} is {@code null}
>             */
>            public static <T> T nonNull(T obj, String message) {
>                if (obj == null)
>                    throw new NullPointerException(message);
>                return obj;
>            }
> 
>         They do a great job reducing the verbiage in validity-checking
>         of arguments that must not be null.
> 
> 
>     I've filed bug 6889858 "Add nonNull methods to java.util.Objects" for
>     these last two methods.  If you want to finish off the engineering
>     need for a changeset, some light tests, etc., I'll file the
>     Sun-internal paperwork for these.
> 



More information about the core-libs-dev mailing list