1 22 package org.jboss.aspects.dbc.condition; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 27 import org.jboss.aop.annotation.AnnotationElement; 28 import org.jboss.aspects.dbc.DesignByContractAspect; 29 import org.jboss.aspects.dbc.Invariant; 30 import org.jboss.aspects.dbc.StaticInvariant; 31 32 37 public class ConditionManager 38 { 39 public static HashMap preConditions = new HashMap (); 40 public static HashMap postConditions = new HashMap (); 41 public static HashMap invariants = new HashMap (); 42 43 public static synchronized InvariantCondition[] getInvariants(Class clazz) 44 { 45 InvariantCondition[] inv = (InvariantCondition[])invariants.get(clazz); 46 if (inv != null) 47 { 48 return inv; 49 } 50 51 initialise(clazz); 52 return (InvariantCondition[])invariants.get(clazz); 53 } 54 55 56 protected static void initialise(Class clazz) 57 { 58 ArrayList invConds = new ArrayList (); 59 60 if (invariants.get(clazz) != null) 61 { 62 if (DesignByContractAspect.verbose)System.out.println("[dbc] Already have invariants for class: " + clazz); 63 } 64 65 if (DesignByContractAspect.verbose)System.out.println("[dbc] ===== Initialising invariants for class: " + clazz); 66 67 Class curClazz = clazz; 69 70 while (curClazz != null) 71 { 72 addInvariantConditions(invConds, curClazz); 73 74 Class [] interfaces = curClazz.getInterfaces(); 75 for (int i = 0; i < interfaces.length ; i++) 76 { 77 addInvariantConditions(invConds, interfaces[i]); 78 } 79 80 curClazz = curClazz.getSuperclass(); 81 } 82 83 InvariantCondition[] inv = (InvariantCondition[])invConds.toArray(new InvariantCondition[invConds.size()]); 84 invariants.put(clazz, inv); 85 } 86 87 private static void addInvariantConditions(ArrayList conditions, Class clazz) 88 { 89 Invariant inv = (Invariant)AnnotationElement.getAnyAnnotation(clazz, Invariant.class); 90 if (inv != null) 91 { 92 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found non-static invariants in class: " + clazz); 93 String [] exprs = inv.value(); 94 if (exprs != null) 95 { 96 for (int i = 0 ; i < exprs.length ; i++) 97 { 98 conditions.add(new InvariantCondition(clazz, exprs[i], false)); 99 } 100 } 101 } 102 103 StaticInvariant statinv = (StaticInvariant)AnnotationElement.getAnyAnnotation(clazz, StaticInvariant.class); 104 if (statinv != null) 105 { 106 if (DesignByContractAspect.verbose) System.out.println("[dbc] Found static invariants in class: " + clazz); 107 String [] exprs = statinv.value(); 108 if (exprs != null) 109 { 110 for (int i = 0 ; i < exprs.length ; i++) 111 { 112 conditions.add(new InvariantCondition(clazz, exprs[i], true)); 113 } 114 } 115 } 116 } 117 } 118 | Popular Tags |