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 abstract class ExecutableCondition extends Condition 38 { 39 public ExecutableCondition(String condExpr, Class clazz, boolean isStatic) 40 { 41 super(condExpr, clazz, isStatic); 42 } 43 44 public void evaluateCondition(DesignByContractAspect aspect, Invocation inv, Object [] args, Object ret) 45 { 46 try 47 { 48 if (DesignByContractAspect.verbose) System.out.println("[dbc] Evaluate condition : '" + originalExpr + "' for: " + executableObject()); 49 50 Interpreter interpreter = new Interpreter(); 51 for (Iterator it = parameterLookup.keySet().iterator() ; it.hasNext() ; ) 52 { 53 String beanshellname = (String )it.next(); 54 String originalname = (String )parameterLookup.get(beanshellname); 55 Object value = getValueForToken(inv, args, ret, originalname); 56 interpreter.set(beanshellname, value); 57 if (DesignByContractAspect.verbose) System.out.println("[dbc] Setting $" + originalname + " to " + value + 58 ((value != null) ? " (type: " + value.getClass().getName() + ")" : "")) ; 59 } 60 61 Boolean eval = (Boolean )interpreter.eval(condExpr); 62 63 if (!eval.booleanValue()) 64 { 65 System.out.println("CONDITION BROKEN: "+ originalExpr + " - " + executableObject()); 66 throw new RuntimeException ("Condition " + originalExpr + " was broken " + "for: " + executableObject()); 67 } 68 } 69 catch (EvalError e) 70 { 71 throw new RuntimeException ("There was an error in the expression: " + originalExpr, e); 72 } 73 } 74 75 public String toString() 76 { 77 return executableObject() + ":" + super.toString(); 78 } 79 80 protected Object getValueForToken(Invocation invocation, Object [] args, Object rtn, String token) 81 { 82 if (isPredefinedToken(token)) 83 { 84 return getValueForPredefinedToken(invocation, rtn, token); 85 } 86 87 try 88 { 89 int i = Integer.parseInt(token); 90 return args[i]; 91 } 92 catch(NumberFormatException e) 93 { 94 throw new RuntimeException ("Invalid token '$" + token + "' in condition: " + originalExpr); 95 } 96 catch(ArrayIndexOutOfBoundsException e) 97 { 98 throw new RuntimeException ("invalid parameter number '$" + token + "' in condition: " 99 + originalExpr + ". " + executableObject() + " takes " + parameterTypes().length); 100 } 101 } 102 103 protected abstract boolean isPredefinedToken(String token); 104 protected abstract Object getValueForPredefinedToken(Invocation invocation, Object rtn, String token); 105 protected abstract Object executableObject(); 106 protected abstract Class [] parameterTypes(); 107 108 } 109 | Popular Tags |