de.unkrig.commons.lang
Class AssertionUtil

java.lang.Object
  extended by de.unkrig.commons.lang.AssertionUtil

public final class AssertionUtil
extends java.lang.Object

Various assertion-related utility methods.


Method Summary
static void enableAssertionsFor(java.lang.Class<?> clasS)
          Enables assertions for the given clasS, regardless of the "enable assertions" command line options that the JVM was start 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 start 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

enableAssertionsFor

public static void enableAssertionsFor(java.lang.Class<?> clasS)
Enables assertions for the given clasS, regardless of the "enable assertions" command line options that the JVM was start with.

Enabling assertions programmatically makes them much more helpful, because unfortunately assertions are disabled by default and hardly any runtime environment provides a simple way to enable them. enableAssertionsForThisClass() is an even more elegant way to enable assertions for classes as they are loaded and initialized.


enableAssertionsForThisClass

public static void enableAssertionsForThisClass()
Enables assertions for the class that invokes this method, regardless of the "enable assertions" command line options that the JVM was start with.
     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.


notNull

public static <T> T notNull(@Nullable
                            T subject)
Verifies that the subject is not null.

Returns:
The subject
Throws:
java.lang.NullPointerException

notNull

public static <T> T notNull(@Nullable
                            T subject,
                            java.lang.String message)
Verifies that the subject is not null.

Returns:
The subject
Throws:
java.lang.NullPointerException - with the given message iff the subject is null

fail

public static java.lang.Object fail()
Identical with 'throw new AssertionError()', but returns an Object so it can be used in an expression.


fail

public 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.


fail

public 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.


fail

public 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.