1 22 package org.jboss.aspects.dbc.condition; 23 24 import java.lang.reflect.Constructor ; 25 import java.util.ArrayList ; 26 27 import org.jboss.aop.annotation.AnnotationElement; 28 import org.jboss.aspects.dbc.DesignByContractAspect; 29 import org.jboss.aspects.dbc.PostCond; 30 import org.jboss.aspects.dbc.PreCond; 31 32 33 38 public class ConstructorConditionManager extends ConditionManager 39 { 40 public static synchronized ExecutableCondition[] getPreConditions(Constructor constructor) 41 { 42 ExecutableCondition[] pre = (ExecutableCondition[])preConditions.get(constructor); 43 if (pre != null) 44 { 45 return pre; 46 } 47 48 initialise(constructor); 49 return (ExecutableCondition[])preConditions.get(constructor); 50 } 51 52 public static synchronized ExecutableCondition[] getPostConditions(Constructor constructor) 53 { 54 ExecutableCondition[] post = (ExecutableCondition[])postConditions.get(constructor); 55 if (post != null) 56 { 57 return post; 58 } 59 60 initialise(constructor); 61 return (ExecutableCondition[])postConditions.get(constructor); 62 } 63 64 public static synchronized InvariantCondition[] getInvariants(Constructor constructor) 65 { 66 return getInvariants(constructor.getDeclaringClass()); 67 } 68 69 private static void initialise(Constructor constructor) 70 { 71 if (DesignByContractAspect.verbose) System.out.println("[dbc] ===== Intitalising constructor: " + constructor); 72 ArrayList preConds = new ArrayList (); 73 ArrayList postConds = new ArrayList (); 74 75 76 boolean first = true; 79 80 Class clazz = constructor.getDeclaringClass(); 81 Class curClazz = clazz; 82 Constructor superConstructor = constructor; 83 84 while (curClazz != null) 85 { 86 if (first) 87 { 88 first = false; 89 } 90 else 91 { 92 superConstructor = findConstructorInClass(curClazz, constructor); 93 } 94 95 if (superConstructor != null) 96 { 97 addConstructorConditions(constructor, superConstructor, preConds, postConds); 98 } 99 100 curClazz = curClazz.getSuperclass(); 101 } 102 103 ExecutableCondition[] pre = (ExecutableCondition[])preConds.toArray(new ExecutableCondition[preConds.size()]); 104 preConditions.put(constructor, pre); 105 106 ExecutableCondition[] post = (ExecutableCondition[])postConds.toArray(new ExecutableCondition[postConds.size()]); 107 postConditions.put(constructor, post); 108 } 109 110 private static void addConstructorConditions(Constructor realConstructor, Constructor currentConstructor, ArrayList preConds, ArrayList postConds) 111 { 112 PreCond pre = (PreCond)AnnotationElement.getAnyAnnotation(currentConstructor, PreCond.class); 113 if (pre != null) 114 { 115 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found preconditions in method: " + currentConstructor); 116 addConstructorConditions(realConstructor, preConds, pre.value()); 117 } 118 119 PostCond post = (PostCond)AnnotationElement.getAnyAnnotation(currentConstructor, PostCond.class); 120 if (post != null) 121 { 122 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found postconditions in method: " + currentConstructor); 123 addConstructorConditions(realConstructor, postConds, post.value()); 124 } 125 } 126 127 128 private static ArrayList addConstructorConditions(Constructor realConstructor, ArrayList conditions, String [] exprs) 129 { 130 if (exprs == null) 131 { 132 return conditions; 133 } 134 135 for (int i = 0 ; i < exprs.length ; i++) 136 { 137 conditions.add(new ConstructorCondition(realConstructor, exprs[i])); 138 } 139 140 return conditions; 141 } 142 143 private static Constructor findConstructorInClass(Class clazz, Constructor constructor) 144 { 145 String name = constructor.getName(); 146 Constructor [] constructors = clazz.getDeclaredConstructors(); 147 for (int i = 0 ; i < constructors.length ; i++) 148 { 149 if (constructors[i].getName().equals(name)) 150 { 151 Class [] soughtParams = constructor.getParameterTypes(); 152 Class [] foundParams = constructors[i].getParameterTypes(); 153 154 if (soughtParams.length == foundParams.length) 155 { 156 for (int j = 0 ; j < soughtParams.length ; j++) 157 { 158 if (soughtParams[j] != foundParams[j]) 159 { 160 break; 161 } 162 } 163 164 return constructors[i]; 165 } 166 } 167 } 168 return null; 169 } 170 171 } 172 | Popular Tags |