1 22 package org.jboss.aop.pointcut; 23 24 import javassist.CtClass; 25 import javassist.CtConstructor; 26 import javassist.CtField; 27 import javassist.CtMethod; 28 import javassist.NotFoundException; 29 import org.jboss.aop.Advisor; 30 import org.jboss.aop.pointcut.ast.ASTAllParameter; 31 import org.jboss.aop.pointcut.ast.ASTAnd; 32 import org.jboss.aop.pointcut.ast.ASTAttribute; 33 import org.jboss.aop.pointcut.ast.ASTBoolean; 34 import org.jboss.aop.pointcut.ast.ASTClass; 35 import org.jboss.aop.pointcut.ast.ASTComposite; 36 import org.jboss.aop.pointcut.ast.ASTConstructor; 37 import org.jboss.aop.pointcut.ast.ASTException; 38 import org.jboss.aop.pointcut.ast.ASTField; 39 import org.jboss.aop.pointcut.ast.ASTHas; 40 import org.jboss.aop.pointcut.ast.ASTHasField; 41 import org.jboss.aop.pointcut.ast.ASTMethod; 42 import org.jboss.aop.pointcut.ast.ASTNot; 43 import org.jboss.aop.pointcut.ast.ASTOr; 44 import org.jboss.aop.pointcut.ast.ASTParameter; 45 import org.jboss.aop.pointcut.ast.ASTStart; 46 import org.jboss.aop.pointcut.ast.ASTSub; 47 import org.jboss.aop.pointcut.ast.Node; 48 import org.jboss.aop.pointcut.ast.SimpleNode; 49 import org.jboss.aop.pointcut.ast.TypeExpressionParserVisitor; 50 51 import java.lang.reflect.Constructor ; 52 import java.lang.reflect.Field ; 53 import java.lang.reflect.Method ; 54 55 62 public class AnnotationMatcher implements TypeExpressionParserVisitor 63 { 64 private Advisor advisor; 65 private Object element; 66 67 public AnnotationMatcher(Advisor advisor, Object element) 68 { 69 this.advisor = advisor; 70 this.element = element; 71 } 72 73 public Object visit(ASTStart node, Object data) 74 { 75 return node.jjtGetChild(0).jjtAccept(this, data); 76 } 77 78 public Object visit(ASTBoolean node, Object data) 79 { 80 return node.jjtGetChild(0).jjtAccept(this, data); 81 } 82 83 public Object visit(ASTComposite node, Object data) 84 { 85 return node.jjtGetChild(0).jjtAccept(this, data); 86 } 87 88 public Object visit(ASTNot node, Object data) 89 { 90 91 Boolean bool = (Boolean ) node.jjtGetChild(0).jjtAccept(this, data); 92 boolean val = bool.booleanValue(); 93 return val ? Boolean.FALSE : Boolean.TRUE; 94 } 95 96 public Object visit(ASTSub node, Object data) 97 { 98 for (int i = 0; i < node.jjtGetNumChildren(); i++) 99 { 100 data = node.jjtGetChild(i).jjtAccept(this, data); 101 } 102 return data; 103 } 104 105 public Object visit(ASTAnd node, Object left) 106 { 107 Node andChild = node.jjtGetChild(0); boolean val = ((Boolean ) left).booleanValue(); 109 return new Boolean (val && ((Boolean ) andChild.jjtAccept(this, Boolean.FALSE)).booleanValue()); 110 } 111 112 public Object visit(ASTOr node, Object left) 113 { 114 Node orChild = node.jjtGetChild(0); boolean val = ((Boolean ) left).booleanValue(); 116 return new Boolean (val || ((Boolean ) orChild.jjtAccept(this, Boolean.FALSE)).booleanValue()); 117 } 118 119 120 public Object visit(SimpleNode node, Object data) 121 { 122 return null; 123 } 124 125 public CtClass getDeclaringClass() 126 { 127 if (element instanceof CtClass) return (CtClass) element; 128 if (element instanceof CtMethod) return ((CtMethod) element).getDeclaringClass(); 129 if (element instanceof CtField) return ((CtField) element).getDeclaringClass(); 130 if (element instanceof CtConstructor) return ((CtConstructor) element).getDeclaringClass(); 131 return null; 132 } 133 134 public Class getJavaDeclaringClass() 135 { 136 if (element instanceof Class ) return (Class ) element; 137 if (element instanceof Method) return ((Method) element).getDeclaringClass(); 138 if (element instanceof Field) return ((Field) element).getDeclaringClass(); 139 if (element instanceof Constructor) return ((Constructor) element).getDeclaringClass(); 140 return null; 141 } 142 143 public Object visit(ASTHas node, Object data) 144 { 145 Node n = node.jjtGetChild(0); 146 if (getDeclaringClass() != null) 147 { 148 CtClass clazz = getDeclaringClass(); 149 if (n instanceof ASTMethod) 150 { 151 return new Boolean (Util.has(clazz, (ASTMethod) n, advisor)); 152 } 153 else 154 { 155 return new Boolean (Util.has(clazz, (ASTConstructor) n, advisor)); 156 } 157 } 158 else 159 { 160 Class clazz = getJavaDeclaringClass(); 161 if (n instanceof ASTMethod) 162 { 163 return new Boolean (Util.has(clazz, (ASTMethod) n, advisor)); 164 } 165 else 166 { 167 return new Boolean (Util.has(clazz, (ASTConstructor) n, advisor)); 168 } 169 } 170 171 } 172 173 public Object visit(ASTHasField node, Object data) 174 { 175 if (getDeclaringClass() != null) 176 { 177 CtClass clazz = getDeclaringClass(); 178 ASTField f = (ASTField) node.jjtGetChild(0); 179 return new Boolean (Util.has(clazz, f, advisor)); 180 } 181 else 182 { 183 Class clazz = getJavaDeclaringClass(); 184 ASTField f = (ASTField) node.jjtGetChild(0); 185 return new Boolean (Util.has(clazz, f, advisor)); 186 } 187 } 188 189 public Object visit(ASTClass node, Object data) 190 { 191 if (!(element instanceof CtClass) && !(element instanceof Class )) return Boolean.FALSE; 192 if (element instanceof CtClass) 193 { 194 CtClass clazz = (CtClass) element; 195 return new Boolean (Util.matchesClassExpr(node.getClazz(), clazz, advisor)); 196 } 197 else 198 { 199 Class clazz = (Class ) element; 200 return new Boolean (Util.matchesClassExpr(node.getClazz(), clazz, advisor)); 201 } 202 203 } 204 205 public Object visit(ASTMethod node, Object data) 206 { 207 if (!(element instanceof CtMethod) && !(element instanceof Method)) return Boolean.FALSE; 208 if (element instanceof CtMethod) 209 { 210 MethodMatcher methodMatcher = null; 211 methodMatcher = new MethodMatcher(advisor, (CtMethod) element, null); 212 return methodMatcher.matches(node); 213 } 214 else 215 { 216 MethodMatcher methodMatcher = null; 217 methodMatcher = new MethodMatcher(advisor, (Method) element, null); 218 return methodMatcher.matches(node); 219 } 220 } 221 222 public Object visit(ASTConstructor node, Object data) 223 { 224 if (!(element instanceof CtConstructor) && !(element instanceof Constructor)) return Boolean.FALSE; 225 if (element instanceof CtConstructor) 226 { 227 ConstructorMatcher conMatcher = null; 228 try 229 { 230 conMatcher = new ConstructorMatcher(advisor, (CtConstructor) element, null); 231 } 232 catch (NotFoundException e) 233 { 234 throw new RuntimeException (e); } 236 return conMatcher.matches(node); 237 } 238 else 239 { 240 ConstructorMatcher conMatcher = null; 241 conMatcher = new ConstructorMatcher(advisor, (Constructor) element, null); 242 return conMatcher.matches(node); 243 } 244 245 } 246 247 public Object visit(ASTField node, Object data) 248 { 249 if (!(element instanceof CtField) && !(element instanceof Field)) return Boolean.FALSE; 250 if (element instanceof CtField) 251 { 252 FieldMatcher fieldMatcher = null; 253 try 254 { 255 fieldMatcher = new FieldMatcher(advisor, (CtField) element, null); 256 } 257 catch (NotFoundException e) 258 { 259 throw new RuntimeException (e); } 261 return node.jjtAccept(fieldMatcher, null); 262 } 263 else 264 { 265 FieldMatcher fieldMatcher = null; 266 fieldMatcher = new FieldMatcher(advisor, (Field) element, null); 267 return node.jjtAccept(fieldMatcher, null); 268 } 269 270 } 271 272 public Object visit(ASTAttribute node, Object data) 273 { 274 return Boolean.FALSE; 275 } 276 277 public Object visit(ASTParameter node, Object data) 278 { 279 return Boolean.FALSE; 280 } 281 282 public Object visit(ASTAllParameter node, Object data) 283 { 284 return Boolean.FALSE; 285 } 286 287 public Object visit(ASTException node, Object data) 288 { 289 return Boolean.FALSE; 290 } 291 292 } 293 | Popular Tags |