1 22 package org.jboss.aspects.dbc.condition; 23 24 import java.util.Iterator ; 25 26 import org.jboss.aop.joinpoint.Invocation; 27 import org.jboss.aspects.dbc.DesignByContractAspect; 28 29 import bsh.EvalError; 30 import bsh.Interpreter; 31 32 37 public class InvariantCondition extends Condition 38 { 39 public InvariantCondition(Class clazz, String condExpr, boolean isStatic) 40 { 41 super(condExpr, clazz, isStatic); 42 } 43 44 public void evaluateCondition(Invocation inv, boolean staticCall, boolean constructor, Object ret) 45 { 46 try 47 { 48 if (DesignByContractAspect.verbose) System.out.println("[dbc] Evaluate condition : '" + originalExpr + "' for class: " + clazz); 49 50 if (!isStatic && staticCall) 51 { 52 System.out.println("[dbc] Ignoring non-static invariant for static call"); 53 return; 54 } 55 56 Interpreter interpreter = new Interpreter(); 57 Object target = (constructor) ? ret : getTarget(inv, staticCall); 58 59 for (Iterator it = parameterLookup.keySet().iterator() ; it.hasNext() ; ) 60 { 61 String bsname = (String )it.next(); 62 String originalname = (String )parameterLookup.get(bsname); 63 if (originalname.equals(Condition.TARGET)) 64 { 65 interpreter.set(bsname, target); 66 if (DesignByContractAspect.verbose) System.out.println("[dbc] Setting $" + originalname + " to " + target +" (type: " + target.getClass().getName() + ")"); 67 } 68 else 69 { 70 System.out.println("INVARIANT CONDITION BROKEN: "+ originalExpr + " - " + clazz); 71 throw new RuntimeException ("Invalid marker '" + originalname + "' in expression: " + originalExpr); 72 } 73 } 74 75 Boolean eval = (Boolean )interpreter.eval(condExpr); 76 77 if (!eval.booleanValue()) 78 { 79 throw new RuntimeException ("Invariant condition " + originalExpr + " was broken " + "for class: " + clazz); 80 } 81 } 82 catch (EvalError e) 83 { 84 throw new RuntimeException ("There was an error in the expression: " + originalExpr, e); 85 } 86 } 87 88 public boolean equals(Object o) 89 { 90 if (o instanceof InvariantCondition) 91 { 92 return super.equals(o); 93 } 94 return false; 95 } 96 97 } 98 | Popular Tags |