1 22 package org.jboss.aspects.dbc.condition; 23 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 27 import javassist.Modifier; 28 29 import org.jboss.aop.annotation.AnnotationElement; 30 import org.jboss.aspects.dbc.DesignByContractAspect; 31 import org.jboss.aspects.dbc.PostCond; 32 import org.jboss.aspects.dbc.PreCond; 33 34 39 public class MethodConditionManager extends ConditionManager 40 { 41 public static synchronized ExecutableCondition[] getPreConditions(Method method) 42 { 43 ExecutableCondition[] pre = (ExecutableCondition[])preConditions.get(method); 44 if (pre != null) 45 { 46 return pre; 47 } 48 49 initialise(method); 50 return (ExecutableCondition[])preConditions.get(method); 51 } 52 53 public static synchronized ExecutableCondition[] getPostConditions(Method method) 54 { 55 ExecutableCondition[] post = (ExecutableCondition[])postConditions.get(method); 56 if (post != null) 57 { 58 return post; 59 } 60 61 initialise(method); 62 return (ExecutableCondition[])postConditions.get(method); 63 } 64 65 public static synchronized InvariantCondition[] getInvariants(Method method) 66 { 67 return getInvariants(method.getDeclaringClass()); 68 } 69 70 private static void initialise(Method method) 71 { 72 if (DesignByContractAspect.verbose) System.out.println("[dbc] ===== Intitalising method: " + method); 73 ArrayList preConds = new ArrayList (); 74 ArrayList postConds = new ArrayList (); 75 76 77 boolean first = true; 81 82 Class clazz = method.getDeclaringClass(); 83 Class curClazz = clazz; 84 Method superMethod = method; 85 86 while (curClazz != null) 87 { 88 if (first) 89 { 90 first = false; 91 } 92 else 93 { 94 superMethod = findMethodInClass(curClazz, method); 95 } 96 97 if (superMethod != null) 98 { 99 addMethodConditions(method, superMethod, preConds, postConds); 100 } 101 102 addMethodConditionsForInterfaces(preConds, postConds, curClazz, method); 103 104 curClazz = curClazz.getSuperclass(); 105 } 106 107 ExecutableCondition[] pre = (ExecutableCondition[])preConds.toArray(new ExecutableCondition[preConds.size()]); 108 preConditions.put(method, pre); 109 110 ExecutableCondition[] post = (ExecutableCondition[])postConds.toArray(new ExecutableCondition[postConds.size()]); 111 postConditions.put(method, post); 112 } 113 114 private static void addMethodConditionsForInterfaces(ArrayList preConds, ArrayList postConds, Class clazz, Method method) 115 { 116 Class [] interfaces = clazz.getInterfaces(); 117 for (int i = 0 ; i < interfaces.length ; i++) 118 { 119 Method foundMethod = findMethodInClass(interfaces[i], method); 121 122 if (foundMethod != null) 123 { 124 addMethodConditions(method, foundMethod, preConds, postConds); 126 } 127 } 128 129 } 130 private static void addMethodConditions(Method realMethod, Method currentMethod, ArrayList preConds, ArrayList postConds) 131 { 132 PreCond pre = (PreCond)AnnotationElement.getAnyAnnotation(currentMethod, PreCond.class); 133 if (pre != null) 134 { 135 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found preconditions in method: " + currentMethod); 136 addMethodConditions(realMethod, preConds, pre.value()); 137 } 138 139 PostCond post = (PostCond)AnnotationElement.getAnyAnnotation(currentMethod, PostCond.class); 140 if (post != null) 141 { 142 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found postconditions in method: " + currentMethod); 143 addMethodConditions(realMethod, postConds, post.value()); 144 } 145 } 146 147 148 private static ArrayList addMethodConditions(Method realMethod, ArrayList conditions, String [] exprs) 149 { 150 if (exprs == null) 151 { 152 return conditions; 153 } 154 155 boolean staticCall = Modifier.isStatic(realMethod.getModifiers()); 156 157 for (int i = 0 ; i < exprs.length ; i++) 158 { 159 conditions.add(new MethodCondition(realMethod, exprs[i], staticCall)); 160 } 161 162 return conditions; 163 } 164 165 private static Method findMethodInClass(Class clazz, Method method) 166 { 167 String name = method.getName(); 168 Method [] methods = clazz.getDeclaredMethods(); 169 for (int i = 0 ; i < methods.length ; i++) 170 { 171 if (methods[i].getName().equals(name)) 172 { 173 Class [] soughtParams = method.getParameterTypes(); 174 Class [] foundParams = methods[i].getParameterTypes(); 175 176 if (soughtParams.length == foundParams.length) 177 { 178 for (int j = 0 ; j < soughtParams.length ; j++) 179 { 180 if (soughtParams[j] != foundParams[j]) 181 { 182 break; 183 } 184 } 185 186 return methods[i]; 187 } 188 } 189 } 190 return null; 191 } 192 193 194 195 196 } 197 | Popular Tags |