Skip to content

Incorrect example in javadocs of Assert.notNull(object, messageSupplier) #25774

@xak2000

Description

@xak2000

Current javadoc of Assert.notNull(object, messageSupplier) methods contains an example:

Assert.notNull(clazz, () -> "The class '" + clazz.getName() + "' must not be null");

/**
* Assert that an object is not {@code null}.
* <pre class="code">
* Assert.notNull(clazz, () -&gt; "The class '" + clazz.getName() + "' must not be null");
* </pre>
* @param object the object to check
* @param messageSupplier a supplier for the exception message to use if the
* assertion fails
* @throws IllegalArgumentException if the object is {@code null}
* @since 5.0
*/
public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
if (object == null) {
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
}
}

This example is incorrect because it will lead to NPE in the messageSupplier.get(), that will be called if clazz is null.

I can create a PR to fix this, but I'm unsure what simple and descriptive example can show the intention of this variant of the method.

Whatever comes to my mind contains another object, that will be used in the message (not the object under assertion).
Like:

Object someObj = getSomeObj();
Assert.notNull(clazz, () -> "Some message that involves '" + someObj.someMethod());

If there is not someObj in the equation, then why we need a Supplier in the first place?

But this is obviously not a good example. Too broad.

But javadoc need to be fixed anyway. It is just confusing now.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation task

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions