1 22 package org.jboss.aspects.dbc.condition; 23 24 import java.util.HashMap ; 25 26 import org.jboss.aop.joinpoint.Invocation; 27 import org.jboss.aspects.dbc.DesignByContractAspect; 28 import org.jboss.aspects.dbc.condition.parser.BeanshellGenerator; 29 import org.jboss.aspects.dbc.condition.parser.Expression; 30 import org.jboss.aspects.dbc.condition.parser.ExpressionParser; 31 32 import bsh.Capabilities; 33 34 39 public abstract class Condition 40 { 41 public final static String TARGET = "tgt"; 42 public final static String RETURN = "rtn"; 43 44 static 45 { 46 Capabilities.setAccessibility(true); 48 } 49 50 protected HashMap parameterLookup = new HashMap (); 53 protected String originalExpr; 54 protected String condExpr; 55 protected Class clazz; 56 57 protected boolean isStatic; 59 60 public Condition(String condExpr, Class clazz, boolean isStatic) 61 { 62 this.isStatic = isStatic; 63 64 if (DesignByContractAspect.verbose) System.out.println("[dbc] Initialising condition: " + condExpr); 65 originalExpr = condExpr; 66 condExpr += " "; 67 68 StringBuffer newcond = new StringBuffer (); 69 StringBuffer param = null; 70 71 for (int i = 0 ; i < condExpr.length() ; i++) 72 { 73 char c = condExpr.charAt(i); 74 if (c == '$') 75 { 76 param = new StringBuffer (); 77 continue; 78 } 79 else if (param != null && (c == '.' || c == ' ' || c == '=' || c == '>' || c == '<' 80 || c == ')' || c == '}' || c == ';' || c == '[' || c == ']')) 81 { 82 String prm = param.toString(); 84 85 if (prm.equals(TARGET) && isStatic) 86 { 87 newcond.append(clazz.getName()); 89 } 90 else 91 { 92 String bsparam = "p" + i; 94 95 newcond.append(bsparam); 96 parameterLookup.put(bsparam, prm); 97 } 98 99 param = null; 100 } 101 102 if (param == null) 103 { 104 newcond.append(c); 105 } 106 else 107 { 108 param.append(c); 109 } 110 } 111 112 Expression expr = ExpressionParser.parseExpression(newcond.toString()); 113 BeanshellGenerator gen = new BeanshellGenerator(expr); 114 this.condExpr = gen.createBeanshellCode(); 115 116 if (DesignByContractAspect.verbose) System.out.println("[dbc] Expanded to Java code:\n" + this.condExpr); 117 118 this.clazz = clazz; 119 } 120 121 public String toString() 122 { 123 return originalExpr; 124 } 125 126 public boolean equals(Object o){ 127 if (o instanceof Condition) 128 { 129 Condition c = (Condition)o; 130 return c.clazz.equals(clazz) && originalExpr.equals(c.originalExpr); 131 } 132 133 return false; 134 } 135 136 public int hashCode() 137 { 138 return originalExpr.hashCode(); 140 } 141 142 protected Object getTarget(Invocation invocation, boolean isStatic) 143 { 144 if (!isStatic) 145 { 146 return invocation.getTargetObject(); 147 } 148 else 149 { 150 return clazz; 151 } 152 } 153 } 154 | Popular Tags |