public final class AssertionUtil extends Object
Modifier and Type | Method and Description |
---|---|
static void |
enableAssertionsFor(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, and for all all the local and anonymous classes that
its methods declare, regardless of the "enable assertions" command line options that the JVM was started with.
|
static Object |
fail()
Identical with '
throw new AssertionError() ', but returns an Object so it can be used in an
expression. |
static <T> T |
fail(String message)
Identical with '
throw new AssertionError(message) ', but returns an Object so it can be used in
an expression. |
static Object |
fail(String message,
Throwable cause)
Identical with '
throw new AssertionError(message, cause) ', but returns an Object so it can be
used in an expression. |
static Object |
fail(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,
String message)
Verifies that the subject is not
null . |
public static void enableAssertionsFor(Class<?> clasS)
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 class or member class is shared with the local and anonymous class that its methods declare, but not with the member classes it declares. In other words: This method should not only be called for top-level class declarations, but also for each and every member class declaration.
(Notice that assertions cannot be used in interfaces, because interfaces cannot declare non-abstract methods, nor initializers.)
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(); } // ... }
enableAssertionsFor(Class)
public static <T> T notNull(@Nullable T subject)
null
.NullPointerException
public static <T> T notNull(@Nullable T subject, String message)
null
.NullPointerException
- with the given message iff the subject is null
public static Object fail()
throw new AssertionError()
', but returns an Object
so it can be used in an
expression.public static <T> T fail(String message)
throw new AssertionError(message)
', but returns an Object
so it can be used in
an expression.public static Object fail(Throwable cause)
throw new AssertionError(cause)
', but returns an Object
so it can be used in
an expression.Copyright © 2018 Arno Unkrig. All rights reserved.