1 22 package org.jboss.aop.pointcut; 23 24 import java.io.StringReader ; 25 import java.lang.reflect.AccessibleObject ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.Field ; 28 import java.lang.reflect.Method ; 29 import org.jboss.aop.Advisor; 30 import org.jboss.aop.AspectManager; 31 import org.jboss.aop.pointcut.ast.ASTStart; 32 import org.jboss.aop.pointcut.ast.ParseException; 33 import org.jboss.aop.pointcut.ast.PointcutExpressionParser; 34 import javassist.CtConstructor; 35 import javassist.CtField; 36 import javassist.CtMethod; 37 import javassist.NotFoundException; 38 import javassist.expr.MethodCall; 39 import javassist.expr.NewExpr; 40 41 47 public class PointcutExpression implements Pointcut 48 { 49 protected String name; 50 protected String expr; 51 protected ASTStart ast; 52 53 protected PointcutStats stats; 54 55 public PointcutExpression(String name, String expr) throws ParseException 56 { 57 this.name = name; 58 this.expr = expr; 59 60 61 ast = new PointcutExpressionParser(new StringReader (expr)).Start(); 62 } 63 64 public void setManager(AspectManager manager) 65 { 66 if (stats == null) 67 { 68 stats = new PointcutStats(ast, manager); 69 stats.matches(); 70 } 71 } 72 73 public PointcutStats getStats() 74 { 75 return stats; 76 } 77 78 public String getName() 79 { 80 return name; 81 } 82 83 public String getExpr() 84 { 85 return expr; 86 } 87 88 public boolean softMatch(Advisor advisor) 89 { 90 SoftClassMatcher matcher = new SoftClassMatcher(advisor, advisor.getName(), ast); 91 return matcher.matches(); 92 } 93 94 public boolean matchesCall(Advisor callingAdvisor, MethodCall methodCall) throws NotFoundException 95 { 96 if (stats == null || stats.isWithin() || stats.isWithincode() || stats.isCall()) 97 { 98 MethodCallMatcher matcher = new MethodCallMatcher(callingAdvisor, methodCall, ast); 99 return matcher.matches(); 100 } 101 return false; 102 } 103 104 public boolean matchesCall(Advisor callingAdvisor, NewExpr methodCall) throws NotFoundException 105 { 106 if (stats == null || stats.isWithin() || stats.isWithincode() || stats.isCall()) 107 { 108 NewExprMatcher matcher = new NewExprMatcher(callingAdvisor, methodCall, ast); 109 return matcher.matches(); 110 } 111 return false; 112 } 113 114 public PointcutMethodMatch matchesExecution(Advisor advisor, Method m) 115 { 116 if (stats == null || stats.isExecution()) 117 { 118 ExecutionMethodMatcher matcher = new ExecutionMethodMatcher(advisor, m, ast); 119 boolean match = matcher.matches(); 120 121 if (match) 122 { 123 return new PointcutMethodMatch(match, matcher.getMatchedClass(), matcher.getMatchLevel(), matcher.isInstanceOf()); 124 } 125 } 126 return null; 127 } 128 129 public boolean matchesExecution(Advisor advisor, Constructor c) 130 { 131 if (stats == null || stats.isExecution()) 132 { 133 ExecutionConstructorMatcher matcher = new ExecutionConstructorMatcher(advisor, c, ast); 134 return matcher.matches(); 135 } 136 return false; 137 } 138 139 public boolean matchesConstruction(Advisor advisor, Constructor c) 140 { 141 if (stats == null || stats.isConstruction()) 142 { 143 ConstructionMatcher matcher = new ConstructionMatcher(advisor, c, ast); 144 return matcher.matches(); 145 } 146 return false; 147 } 148 149 public boolean matchesGet(Advisor advisor, Field f) 150 { 151 if (stats == null || stats.isGet()) 152 { 153 FieldMatcher matcher = new FieldGetMatcher(advisor, f, ast); 154 return matcher.matches(); 155 } 156 return false; 157 } 158 159 public boolean matchesSet(Advisor advisor, Field f) 160 { 161 if (stats == null || stats.isSet()) 162 { 163 FieldMatcher matcher = new FieldSetMatcher(advisor, f, ast); 164 return matcher.matches(); 165 } 166 return false; 167 } 168 169 public boolean matchesExecution(Advisor advisor, CtMethod m) throws NotFoundException 170 { 171 try 172 { 173 if (stats == null || stats.isExecution()) 174 { 175 ExecutionMethodMatcher matcher = new ExecutionMethodMatcher(advisor, m, ast); 176 boolean match = matcher.matches(); 177 return match; 178 } 179 return false; 180 } 181 catch (Exception e) 182 { 183 throw new RuntimeException ("Error parsing " + expr, e); 184 } 185 } 186 187 public boolean matchesExecution(Advisor advisor, CtConstructor c) throws NotFoundException 188 { 189 if (stats == null || stats.isExecution()) 190 { 191 ExecutionConstructorMatcher matcher = new ExecutionConstructorMatcher(advisor, c, ast); 192 return matcher.matches(); 193 } 194 return false; 195 } 196 197 public boolean matchesConstruction(Advisor advisor, CtConstructor c) throws NotFoundException 198 { 199 if (stats == null || stats.isConstruction()) 200 { 201 ConstructionMatcher matcher = new ConstructionMatcher(advisor, c, ast); 202 return matcher.matches(); 203 } 204 return false; 205 } 206 207 public boolean matchesGet(Advisor advisor, CtField f) throws NotFoundException 208 { 209 if (stats == null || stats.isGet()) 210 { 211 FieldMatcher matcher = new FieldGetMatcher(advisor, f, ast); 212 return matcher.matches(); 213 } 214 return false; 215 } 216 217 public boolean matchesSet(Advisor advisor, CtField f) throws NotFoundException 218 { 219 if (stats == null || stats.isSet()) 220 { 221 FieldMatcher matcher = new FieldSetMatcher(advisor, f, ast); 222 return matcher.matches(); 223 } 224 return false; 225 } 226 227 public boolean matchesCall(Advisor advisor, AccessibleObject within, Class calledClass, Method calledMethod) 228 { 229 if (stats == null || stats.isWithin() || stats.isWithincode() || stats.isCall()) 230 { 231 CallMatcher matcher = new CallMatcher(advisor, within, calledClass, calledMethod, ast); 232 return matcher.matches(); 233 } 234 return false; 235 } 236 237 238 public boolean matchesCall(Advisor advisor, AccessibleObject within, Class calledClass, Constructor calledCon) 239 { 240 if (stats == null || stats.isWithin() || stats.isWithincode() || stats.isCall()) 241 { 242 ConstructorCallMatcher matcher = new ConstructorCallMatcher(advisor, within, calledClass, calledCon, ast); 243 return matcher.matches(); 244 } 245 return false; 246 } 247 248 public String toString() 249 { 250 return "Pointcut[name=" + name + "; expr=" + expr + "]"; 251 } 252 } 253 | Popular Tags |