FileDocCategorySizeDatePackage
ConstructorConditionManager.javaAPI DocJBoss 4.2.15913Fri Jul 13 21:02:28 BST 2007org.jboss.aspects.dbc.condition

ConstructorConditionManager

public class ConstructorConditionManager extends ConditionManager
author
Kabir Khan
version
$Revision: 57186 $

Fields Summary
Constructors Summary
Methods Summary
private static voidaddConstructorConditions(java.lang.reflect.Constructor realConstructor, java.lang.reflect.Constructor currentConstructor, java.util.ArrayList preConds, java.util.ArrayList postConds)

      PreCond pre = (PreCond)AnnotationElement.getAnyAnnotation(currentConstructor, PreCond.class);
      if (pre != null)
      {
         if (DesignByContractAspect.verbose) System.out.println("[dbc] Found preconditions in method: " + currentConstructor);
         addConstructorConditions(realConstructor, preConds, pre.value());
      }
      
      PostCond post = (PostCond)AnnotationElement.getAnyAnnotation(currentConstructor, PostCond.class);
      if (post != null)
      {
         if (DesignByContractAspect.verbose) System.out.println("[dbc] Found postconditions in method: " + currentConstructor);
         addConstructorConditions(realConstructor, postConds, post.value());
      }
   
private static java.util.ArrayListaddConstructorConditions(java.lang.reflect.Constructor realConstructor, java.util.ArrayList conditions, java.lang.String[] exprs)

      if (exprs == null)
      {
         return conditions;
      }
      
      for (int i = 0 ; i < exprs.length ; i++)
      {
         conditions.add(new ConstructorCondition(realConstructor, exprs[i]));
      }
      
      return conditions;
   
private static java.lang.reflect.ConstructorfindConstructorInClass(java.lang.Class clazz, java.lang.reflect.Constructor constructor)

      String name = constructor.getName();
      Constructor[] constructors = clazz.getDeclaredConstructors();
      for (int i = 0 ; i < constructors.length ; i++)
      {
         if (constructors[i].getName().equals(name))
         {
            Class[] soughtParams = constructor.getParameterTypes();
            Class[] foundParams = constructors[i].getParameterTypes();
            
            if (soughtParams.length == foundParams.length)
            {
               for (int j = 0 ; j < soughtParams.length ; j++)
               {
                  if (soughtParams[j] != foundParams[j])
                  {
                     break;
                  }
               }
               
               return constructors[i];
            }
         }
      }
      return null;
   
public static synchronized InvariantCondition[]getInvariants(java.lang.reflect.Constructor constructor)

      return getInvariants(constructor.getDeclaringClass()); 
   
public static synchronized ExecutableCondition[]getPostConditions(java.lang.reflect.Constructor constructor)

      ExecutableCondition[] post = (ExecutableCondition[])postConditions.get(constructor); 
      if (post != null)
      {
         return post;
      }
      
      initialise(constructor);
      return (ExecutableCondition[])postConditions.get(constructor); 
   
public static synchronized ExecutableCondition[]getPreConditions(java.lang.reflect.Constructor constructor)

      ExecutableCondition[] pre = (ExecutableCondition[])preConditions.get(constructor); 
      if (pre != null)
      {
         return pre;
      }
      
      initialise(constructor);
      return (ExecutableCondition[])preConditions.get(constructor); 
   
private static voidinitialise(java.lang.reflect.Constructor constructor)

      if (DesignByContractAspect.verbose) System.out.println("[dbc] ===== Intitalising constructor: " + constructor);
      ArrayList preConds = new ArrayList();
      ArrayList postConds = new ArrayList();
      
      
      //Need @PreCond and @PostCond for this constructor, and all the super 
      //declarations of the constructor.
      boolean first = true;
      
      Class clazz = constructor.getDeclaringClass();
      Class curClazz = clazz;
      Constructor superConstructor = constructor;
      
      while (curClazz != null)
      {
         if (first)
         {
            first = false;
         }
         else
         {
            superConstructor = findConstructorInClass(curClazz, constructor);
         }
         
         if (superConstructor != null)
         {
            addConstructorConditions(constructor, superConstructor, preConds, postConds);
         }
         
         curClazz = curClazz.getSuperclass();
      }
      
      ExecutableCondition[] pre = (ExecutableCondition[])preConds.toArray(new ExecutableCondition[preConds.size()]);
      preConditions.put(constructor, pre);
      
      ExecutableCondition[] post = (ExecutableCondition[])postConds.toArray(new ExecutableCondition[postConds.size()]);
      postConditions.put(constructor, post);