net.mtu.eggplant.dbc
Class AssertTools

java.lang.Object
  extended bynet.mtu.eggplant.dbc.AssertTools

public final class AssertTools
extends Object

class of static helper methods for assertions.

Version:
$Revision: 1.9 $

Nested Class Summary
static class AssertTools.MethodLock
          Used for locking methods.
 
Field Summary
static String ASSERT_BEHAVIOR
          what to do if an assertion violation is raised
static boolean ENFORCE_ASSERT_CONDITION
          if assert conditions should be enforced
static boolean ENFORCE_INHERITED_CONDITIONS
          if inherited conditions should be enforced
static boolean ENFORCE_INVARIANT_CONDITION
          if invariant conditions should be enforced
static boolean ENFORCE_POST_CONDITION
          if post conditions should be enforced
static boolean ENFORCE_PRE_CONDITION
          if pre conditions should be enforced
static net.mtu.eggplant.dbc.AssertTools.NONE NO_CLASS
          Used to represent no class.
static Method NO_METHOD
          Used in caching to represent that no super method can be found.
 
Method Summary
static void assertFailed(AssertionViolation av)
          Called when an assert fails.
static Class classForName(String className)
          Get the class object for this class name.
static boolean extendsObject(AssertClass clazz)
           
static void fail(AssertionViolation av)
          Called to signal an assertion failure.
static Class[] findAssertClassInterfaces(Class[] interfaces)
          Do a depth first search of each interface heirarchy and return the class objects that implement the DBC conditions, if found.
static Method findSuperMethod(Class thisClass, String methodName, Class[] methodArgs)
          Find the superclasses method, this is my version of a superClass method, this means that the method name is ____methodName where packageName is the package the class is in with the characters '.'
static AssertionViolation getCurrentAssertionViolation()
           
static void internalError(String message)
          called whenever an erorr occurs in the code that was generated that is not caused by the user.
static void invariantFailed(AssertionViolation av)
          called when an invariant fails.
static boolean isPrimative(String type)
           
static AssertTools.MethodLock lockMethod(String signature, Object instance)
          Lock a method.
static void postConditionFailed(AssertionViolation av)
          called when a post condition fails.
static void preConditionFailed(AssertionViolation av)
          Called when a precondition fails.
static void setCurrentAssertionViolation(AssertionViolation violation)
          set the assertion violation that should be throw next.
static void setUniqueParams(AssertClass assertClass)
          Calculate the unique parameters for all constructors for class and set those values on the AssertMethod objects.
static boolean unlockMethod(AssertTools.MethodLock lock)
          Unlock a method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENFORCE_INHERITED_CONDITIONS

public static final boolean ENFORCE_INHERITED_CONDITIONS
if inherited conditions should be enforced

See Also:
net.mtu.eggplant.dbc

ENFORCE_ASSERT_CONDITION

public static final boolean ENFORCE_ASSERT_CONDITION
if assert conditions should be enforced

See Also:
net.mtu.eggplant.dbc

ENFORCE_INVARIANT_CONDITION

public static final boolean ENFORCE_INVARIANT_CONDITION
if invariant conditions should be enforced

See Also:
net.mtu.eggplant.dbc

ENFORCE_PRE_CONDITION

public static final boolean ENFORCE_PRE_CONDITION
if pre conditions should be enforced

See Also:
net.mtu.eggplant.dbc

ENFORCE_POST_CONDITION

public static final boolean ENFORCE_POST_CONDITION
if post conditions should be enforced

See Also:
net.mtu.eggplant.dbc

ASSERT_BEHAVIOR

public static final String ASSERT_BEHAVIOR
what to do if an assertion violation is raised

See Also:
net.mtu.eggplant.dbc

NO_CLASS

public static final net.mtu.eggplant.dbc.AssertTools.NONE NO_CLASS
Used to represent no class. This is used for caching. If this class is encountered it, then one can be assured that it's already been determined that a class has been checked for and not found.


NO_METHOD

public static final Method NO_METHOD
Used in caching to represent that no super method can be found.

Method Detail

findAssertClassInterfaces

public static Class[] findAssertClassInterfaces(Class[] interfaces)
Do a depth first search of each interface heirarchy and return the class objects that implement the DBC conditions, if found.

Parameters:
interfaces - the interfaces to look in
Returns:
the DBC classes, may be an empty array if no DBC classes can be found
PreConditions
(null != interfaces)
PostConditions
(null != $return)

findSuperMethod

public static Method findSuperMethod(Class thisClass,
                                     String methodName,
                                     Class[] methodArgs)
Find the superclasses method, this is my version of a superClass method, this means that the method name is ____methodName where packageName is the package the class is in with the characters '.' and '$' replaced with '_' and className is the name of the class.

Returns:
the method found, if no such method can be found null is returned
PreConditions
(thisClass != null), (methodName != null), (methodArgs != null)

setCurrentAssertionViolation

public static void setCurrentAssertionViolation(AssertionViolation violation)
set the assertion violation that should be throw next.


getCurrentAssertionViolation

public static AssertionViolation getCurrentAssertionViolation()

assertFailed

public static void assertFailed(AssertionViolation av)
Called when an assert fails.

Parameters:
av - AssertionViolation with information about the failure
See Also:
ENFORCE_ASSERT_CONDITION
PreConditions
(av != null)

invariantFailed

public static void invariantFailed(AssertionViolation av)
called when an invariant fails.

Parameters:
av - AssertionViolation with information about the failure
See Also:
ENFORCE_INVARIANT_CONDITION
PreConditions
(av != null)

postConditionFailed

public static void postConditionFailed(AssertionViolation av)
called when a post condition fails.

Parameters:
av - AssertionViolation with information about the failure
See Also:
ENFORCE_POST_CONDITION
PreConditions
(av != null)

preConditionFailed

public static void preConditionFailed(AssertionViolation av)
Called when a precondition fails.

Parameters:
av - AssertionViolation with information about the failure
See Also:
ENFORCE_PRE_CONDITION
PreConditions
(av != null)

fail

public static void fail(AssertionViolation av)
Called to signal an assertion failure.

See Also:
ASSERT_BEHAVIOR
PreConditions
(null != av)

internalError

public static void internalError(String message)
called whenever an erorr occurs in the code that was generated that is not caused by the user. Generate some useful message and then blow up.


classForName

public static Class classForName(String className)
Get the class object for this class name. Just like Class.forName(), but returns null on an exception.

Returns:
null for no such class found

lockMethod

public static AssertTools.MethodLock lockMethod(String signature,
                                                Object instance)
Lock a method. This is used at the top of the pre, post and invariant check methods so that the methods don't get called recursively.

Parameters:
signature - the signature of the method to lock
instance - the instance that should be locked. this or null (for static methods)
Returns:
the lock if it succeeded, null if it failed

unlockMethod

public static boolean unlockMethod(AssertTools.MethodLock lock)
Unlock a method.

Parameters:
lock - the lock that was recieved from lockMethod()
Returns:
true if the method was locked
See Also:
lockMethod(String, Object)

setUniqueParams

public static void setUniqueParams(AssertClass assertClass)

Calculate the unique parameters for all constructors for class and set those values on the AssertMethod objects. This is for creating a unique parameter list for checking preconditions on constructors. If in a class there exists a constructor C1 such that there is another constructor C2 that has number parameters N+1, where N is the number of parameters to C1, and the first N parameter types match exactly and the N+1 parameter is not a primative there is a possibility for ambiguity errors with the generated constructors. So boolean parameters are added to such constructors to guarentee uniqueness.

Here is an example:

 This won't work:
 public class A {
   public A(int i) {
     this(i, new AssertDummy(i));
   }
   private A(int i, AssertDummy ad) {
     //generated to handle preconditions
   }
 
   public A(int i, int j) {
    this(i, null);
    //should call following constructor, but with generated constructor is ambiguous
   }
   
   public A(int i, Component c) {
     //do something
   }
 }
 This will work:
 public class A {
   public A(int i) {
     this(i, true, new AssertDummy(i));
   }
   private A(int i, boolean dummy0, AssertDummy ad) {
     //generated to handle preconditions
   }
 
   public A(int i, int j) {
    this(i, null);
    //should call following constructor, and now will since the code is no longer ambigous
   }
   
   public A(int i, Component c) {
     //do something
   }
 }
 

PreConditions
(assertClass != null)

isPrimative

public static boolean isPrimative(String type)
Parameters:
type - String that represents a java type
Returns:
true if type represents a java primative
PreConditions
(type != null)

extendsObject

public static boolean extendsObject(AssertClass clazz)
Returns:
true if clazz extends Object and has no implemented interfaces. This signals a case where optimizations can be done by not generated assertion methods.