1 4 package com.tc.aspectwerkz.expression; 5 6 import com.tc.backport175.bytecode.AnnotationElement; 7 8 import com.tc.aspectwerkz.expression.ast.ASTArgParameter; 9 import com.tc.aspectwerkz.expression.ast.ASTArgs; 10 import com.tc.aspectwerkz.expression.ast.ASTAttribute; 11 import com.tc.aspectwerkz.expression.ast.ASTCall; 12 import com.tc.aspectwerkz.expression.ast.ASTCflow; 13 import com.tc.aspectwerkz.expression.ast.ASTCflowBelow; 14 import com.tc.aspectwerkz.expression.ast.ASTConstructorPattern; 15 import com.tc.aspectwerkz.expression.ast.ASTExecution; 16 import com.tc.aspectwerkz.expression.ast.ASTExpression; 17 import com.tc.aspectwerkz.expression.ast.ASTFieldPattern; 18 import com.tc.aspectwerkz.expression.ast.ASTGet; 19 import com.tc.aspectwerkz.expression.ast.ASTHandler; 20 import com.tc.aspectwerkz.expression.ast.ASTMethodPattern; 21 import com.tc.aspectwerkz.expression.ast.ASTModifier; 22 import com.tc.aspectwerkz.expression.ast.ASTNot; 23 import com.tc.aspectwerkz.expression.ast.ASTParameter; 24 import com.tc.aspectwerkz.expression.ast.ASTPointcutReference; 25 import com.tc.aspectwerkz.expression.ast.ASTRoot; 26 import com.tc.aspectwerkz.expression.ast.ASTSet; 27 import com.tc.aspectwerkz.expression.ast.ASTStaticInitialization; 28 import com.tc.aspectwerkz.expression.ast.ASTTarget; 29 import com.tc.aspectwerkz.expression.ast.ASTThis; 30 import com.tc.aspectwerkz.expression.ast.ASTWithin; 31 import com.tc.aspectwerkz.expression.ast.ASTWithinCode; 32 import com.tc.aspectwerkz.expression.ast.ExpressionParserVisitor; 33 import com.tc.aspectwerkz.expression.ast.Node; 34 import com.tc.aspectwerkz.expression.ast.SimpleNode; 35 import com.tc.aspectwerkz.util.Util; 36 import com.tc.aspectwerkz.reflect.StaticInitializationInfo; 37 import com.tc.aspectwerkz.reflect.ReflectionInfo; 38 import com.tc.aspectwerkz.reflect.ClassInfo; 39 import com.tc.aspectwerkz.reflect.MemberInfo; 40 import com.tc.aspectwerkz.reflect.ClassInfoHelper; 41 import com.tc.aspectwerkz.reflect.MethodInfo; 42 import com.tc.aspectwerkz.reflect.FieldInfo; 43 import com.tc.aspectwerkz.reflect.ConstructorInfo; 44 45 56 public class AdvisedClassFilterExpressionVisitor extends ExpressionVisitor implements ExpressionParserVisitor { 57 58 65 public AdvisedClassFilterExpressionVisitor(final ExpressionInfo expressionInfo, final String expression, 66 final String namespace, final Node root) { 67 super(expressionInfo, expression, namespace, root); 68 } 69 70 public Object visit(SimpleNode node, Object data) { 72 return node.jjtGetChild(0).jjtAccept(this, data); 73 } 74 75 public Object visit(ASTRoot node, Object data) { 76 Node child = node.jjtGetChild(0); 77 Boolean match = (Boolean ) child.jjtAccept(this, data); 78 return match; 79 } 80 81 public Object visit(ASTExpression node, Object data) { 82 Node child = node.jjtGetChild(0); 83 Boolean match = (Boolean ) child.jjtAccept(this, data); 84 return match; 85 } 86 87 public Object visit(ASTNot node, Object data) { 88 return super.visit(node, data); 89 } 90 91 public Object visit(ASTPointcutReference node, Object data) { 93 ExpressionContext context = (ExpressionContext) data; 94 ExpressionNamespace namespace = ExpressionNamespace.getNamespace(m_namespace); 95 AdvisedClassFilterExpressionVisitor expression = namespace.getAdvisedClassExpression(node.getName()); 96 return expression.matchUndeterministic(context); 97 } 98 99 public Object visit(ASTExecution node, Object data) { 100 ExpressionContext context = (ExpressionContext) data; 101 102 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 104 boolean checkPattern = !(patternNode instanceof ASTAttribute); 105 106 if (checkPattern) { 107 if (context.hasWithinPointcut() || context.hasExecutionPointcut()) { 108 if (context.hasExecutionPointcut()) { 109 return patternNode.jjtAccept(this, context.getReflectionInfo()); 111 } else { 112 return patternNode.jjtAccept(this, context.getWithinReflectionInfo()); 114 } 115 } else { 116 return Boolean.FALSE; 117 } 118 } else { 119 return null; 120 } 121 } 122 123 public Object visit(ASTCall node, Object data) { 124 ExpressionContext context = (ExpressionContext) data; 125 126 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 128 boolean checkPattern = !(patternNode instanceof ASTAttribute); 129 130 if (checkPattern) { 131 if (context.hasWithinPointcut() || context.hasCallPointcut()) { 132 if (context.hasReflectionInfo()) { 133 return patternNode.jjtAccept(this, context.getReflectionInfo()); 134 } else { 135 return null; 136 } 137 } else { 138 return Boolean.FALSE; 139 } 140 } else { 141 return null; 142 } 143 } 144 145 public Object visit(ASTSet node, Object data) { 146 ExpressionContext context = (ExpressionContext) data; 147 148 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 150 boolean checkPattern = !(patternNode instanceof ASTAttribute); 151 152 if (checkPattern) { 155 if (context.hasWithinPointcut() || context.hasSetPointcut()) { 156 if (context.hasReflectionInfo()) { 157 return patternNode.jjtAccept(this, context.getReflectionInfo()); 158 } else { 159 return null; 160 } 161 } else { 162 return Boolean.FALSE; 163 } 164 } else { 165 return null; 166 } 167 } 168 169 public Object visit(ASTGet node, Object data) { 170 ExpressionContext context = (ExpressionContext) data; 171 172 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 174 boolean checkPattern = !(patternNode instanceof ASTAttribute); 175 176 if (checkPattern) { 179 if (context.hasWithinPointcut() || context.hasGetPointcut()) { 180 if (context.hasReflectionInfo()) { 181 return patternNode.jjtAccept(this, context.getReflectionInfo()); 182 } else { 183 return null; 184 } 185 } else { 186 return Boolean.FALSE; 187 } 188 } else { 189 return null; 190 } 191 } 192 193 public Object visit(ASTHandler node, Object data) { 194 return null; 195 } 196 197 public Object visit(ASTStaticInitialization node, Object data) { 198 ExpressionContext context = (ExpressionContext) data; 199 200 if (context.hasStaticInitializationPointcut() && context.hasWithinReflectionInfo()) { 201 ReflectionInfo reflectInfo = context.getWithinReflectionInfo(); 202 if (reflectInfo instanceof StaticInitializationInfo) { 203 reflectInfo = ((StaticInitializationInfo) reflectInfo).getDeclaringType(); 204 } 205 if (reflectInfo instanceof ClassInfo) { 206 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 208 if (!(patternNode instanceof ASTAttribute)) { 209 Boolean matchPattern = (Boolean ) patternNode.jjtAccept(this, reflectInfo); 210 if (Boolean.FALSE.equals(matchPattern)) { 211 return Boolean.FALSE; 212 } 213 } 214 215 boolean matchedAnnotations = visitAttributes(node, reflectInfo); 217 if (!matchedAnnotations) { 218 return Boolean.FALSE; 219 } else { 220 return null; } 222 } else { 223 return Boolean.FALSE; 224 } 225 } else { 226 return Boolean.FALSE; 227 } 228 } 229 230 public Object visit(ASTWithinCode node, Object data) { 231 ExpressionContext context = (ExpressionContext) data; 232 ReflectionInfo withinInfo = context.getWithinReflectionInfo(); 233 234 if (node.isStaticInitializer()) { 235 ASTWithin fastNode = new ASTWithin(0); 238 for (int i = 0; i < node.jjtGetChild(0).jjtGetNumChildren(); i++) { 239 fastNode.jjtAddChild(node.jjtGetChild(0).jjtGetChild(i), i); 240 } 241 return super.visit(fastNode, data); 242 } else { 243 Node patternNode = node.jjtGetChild(node.jjtGetNumChildren() - 1); 244 boolean checkPattern = !(patternNode instanceof ASTAttribute); 245 246 if (checkPattern) { 247 if (withinInfo instanceof MemberInfo) { 248 return patternNode.jjtAccept(this, withinInfo); 249 } else if (withinInfo instanceof ClassInfo) { 250 Boolean matchDeclaringType = (Boolean ) patternNode.jjtAccept(this, withinInfo); 251 if (Boolean.FALSE.equals(matchDeclaringType)) { 252 return Boolean.FALSE; 253 } else { 254 return null; 256 } 257 } else { 258 return null; 259 } 260 } else { 261 return null; 262 } 263 } 264 } 265 266 public Object visit(ASTCflow node, Object data) { 267 return null; 268 } 269 270 public Object visit(ASTCflowBelow node, Object data) { 271 return null; 272 } 273 274 public Object visit(ASTArgs node, Object data) { 275 return null; 276 } 277 278 public Object visit(ASTTarget node, Object data) { 279 return null; } 281 282 public Object visit(ASTThis node, Object data) { 283 ExpressionContext context = (ExpressionContext) data; 284 if (context.hasWithinReflectionInfo()) { 285 ReflectionInfo withinInfo = context.getWithinReflectionInfo(); 286 if (withinInfo instanceof MemberInfo) { 287 return Util.booleanValueOf( 288 ClassInfoHelper.instanceOf( 289 ((MemberInfo) withinInfo).getDeclaringType(), 290 node.getBoundedType(m_expressionInfo) 291 ) 292 ); 293 } else if (withinInfo instanceof ClassInfo) { 294 return Util.booleanValueOf( 295 ClassInfoHelper.instanceOf((ClassInfo) withinInfo, node.getBoundedType(m_expressionInfo)) 296 ); 297 } 298 } 299 return Boolean.FALSE; 300 } 301 302 318 public Object visit(ASTMethodPattern node, Object data) { 319 if (data instanceof ClassInfo) { 320 ClassInfo classInfo = (ClassInfo) data; 321 if (node.getDeclaringTypePattern().matchType(classInfo)) { 322 return Boolean.TRUE; 323 } 324 return Boolean.FALSE; 325 } else if (data instanceof MethodInfo) { 326 MethodInfo methodInfo = (MethodInfo) data; 327 if (node.getDeclaringTypePattern().matchType(methodInfo.getDeclaringType())) { 328 return null; } 330 return Boolean.FALSE; 331 } 332 return Boolean.FALSE; 333 } 334 335 public Object visit(ASTConstructorPattern node, Object data) { 336 if (data instanceof ClassInfo) { 337 ClassInfo classInfo = (ClassInfo) data; 338 if (node.getDeclaringTypePattern().matchType(classInfo)) { 339 return Boolean.TRUE; 341 } 342 } else if (data instanceof ConstructorInfo) { 343 ConstructorInfo constructorInfo = (ConstructorInfo) data; 344 if (node.getDeclaringTypePattern().matchType(constructorInfo.getDeclaringType())) { 345 return null; } 347 return Boolean.FALSE; 348 } 349 return Boolean.FALSE; 350 } 351 352 public Object visit(ASTFieldPattern node, Object data) { 353 if (data instanceof ClassInfo) { 354 ClassInfo classInfo = (ClassInfo) data; 355 if (node.getDeclaringTypePattern().matchType(classInfo)) { 356 return Boolean.TRUE; 358 } 359 } else if (data instanceof FieldInfo) { 360 FieldInfo fieldInfo = (FieldInfo) data; 361 if (node.getDeclaringTypePattern().matchType(fieldInfo.getDeclaringType())) { 362 return null; } 364 return Boolean.FALSE; 365 } 366 return Boolean.FALSE; 367 } 368 369 public Object visit(ASTParameter node, Object data) { 370 ClassInfo parameterType = (ClassInfo) data; 371 if (node.getDeclaringClassPattern().matchType(parameterType)) { 372 return Boolean.TRUE; 374 } else { 375 return Boolean.FALSE; 376 } 377 } 378 379 public Object visit(ASTArgParameter node, Object data) { 380 return Boolean.TRUE; 382 } 383 384 public Object visit(ASTAttribute node, Object data) { 385 boolean matchAnnotation = false; 387 AnnotationElement.Annotation[] annotations = (AnnotationElement.Annotation[]) data; 388 for (int i = 0; i < annotations.length; i++) { 389 AnnotationElement.Annotation annotation = annotations[i]; 390 if (annotation.getInterfaceName().equals(node.getName())) { 391 matchAnnotation = true; 392 } 393 } 394 if (node.isNot()) { 395 return Util.booleanValueOf(!matchAnnotation); 396 } else { 397 return Util.booleanValueOf(matchAnnotation); 398 } 399 } 400 401 public Object visit(ASTModifier node, Object data) { 402 return null; 404 } 405 406 411 public String toString() { 412 return m_expression; 413 } 414 } 415 | Popular Tags |