public final class AssertionUtil
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static void |
enableAssertionsFor(java.lang.Class<?> clasS)
Enables assertions for the given
clasS and the local and anonymous classes that it encloses, regardless
of the "enable assertions" command line options that the JVM was started with. |
static void |
enableAssertionsForThisClass()
Enables assertions for the class that invokes this method, regardless of the "enable assertions" command line
options that the JVM was started with.
|
static java.lang.Object |
fail()
Identical with '
throw new AssertionError()', but returns an Object so it can be used in an
expression. |
static <T> T |
fail(java.lang.String message)
Identical with '
throw new AssertionError(message)', but returns an Object so it can be used in
an expression. |
static java.lang.Object |
fail(java.lang.String message,
java.lang.Throwable cause)
Identical with '
throw new AssertionError(message, cause)', but returns an Object so it can be
used in an expression. |
static java.lang.Object |
fail(java.lang.Throwable cause)
Identical with '
throw new AssertionError(cause)', but returns an Object so it can be used in
an expression. |
static <T> T |
notNull(T subject)
Verifies that the
subject is not null. |
static <T> T |
notNull(T subject,
java.lang.String message)
Verifies that the
subject is not null. |
public static void enableAssertionsFor(java.lang.Class<?> clasS)
clasS and the local and anonymous classes that it encloses, regardless
of the "enable assertions" command line options that the JVM was started with.
Enabling assertions programmatically makes them much more useful, because unfortunately assertions are disabled by default and hardly any runtime environment provides a simple way to enable them.
Notice that the assertion status of a top-level type or member type is shared with the local and anonymous class that its methods declare, but not with the member types it declares. In other words: This method should be called for local classes and anonymous classes.
enableAssertionsForThisClass() is an even more elegant way to enable assertions for classes as they
are loaded and initialized.
public static void enableAssertionsForThisClass()
public class MyClass {
static { AssertionUtil.enableAssertionsForThisClass(); }
// ...
}
Adding such static initializers to all classes makes assertions much more helpful, because unfortunately assertions are disabled by default and hardly any runtime environment provides a simple way to enable them.
Must only be called from the static initializer of a class.
Should not be called from local and anonymous classes, because these share the assertion state with the enclosing (non-local, non-anonymous) type.
public static <T> T notNull(@Nullable T subject)
subject is not null.subjectjava.lang.NullPointerExceptionpublic static <T> T notNull(@Nullable T subject, java.lang.String message)
subject is not null.subjectjava.lang.NullPointerException - with the given message iff the subject is nullpublic static java.lang.Object fail()
throw new AssertionError()', but returns an Object so it can be used in an
expression.public static <T> T fail(java.lang.String message)
throw new AssertionError(message)', but returns an Object so it can be used in
an expression.public static java.lang.Object fail(java.lang.Throwable cause)
throw new AssertionError(cause)', but returns an Object so it can be used in
an expression.public static java.lang.Object fail(java.lang.String message,
java.lang.Throwable cause)
throw new AssertionError(message, cause)', but returns an Object so it can be
used in an expression.