1 22 package org.jboss.aop.pointcut; 23 24 import org.jboss.aop.instrument.Untransformable; 25 import org.jboss.aop.pointcut.ast.ASTConstructor; 26 import org.jboss.aop.pointcut.ast.ASTExecution; 27 import org.jboss.aop.pointcut.ast.ASTMethod; 28 import org.jboss.aop.pointcut.ast.ClassExpression; 29 import org.jboss.aop.pointcut.ast.ParseException; 30 import org.jboss.aop.pointcut.ast.PointcutExpressionParser; 31 import org.jboss.aop.pointcut.ast.SimpleNode; 32 33 import java.io.InputStream ; 34 import java.io.StringReader ; 35 import java.security.AccessController ; 36 import java.security.PrivilegedActionException ; 37 import java.security.PrivilegedExceptionAction ; 38 39 46 public class CFlow 47 { 48 public static int NOT_FOUND = -2; 49 50 private String original; 51 private SimpleNode point; 52 private boolean not; 53 54 public CFlow(String expr, boolean not) 55 { 56 original = expr; 57 expr = "execution(" + expr + ")"; 58 ASTExecution exc = null; 59 try 60 { 61 exc = new PointcutExpressionParser(new StringReader (expr)).execution(); 62 } 63 catch (ParseException e) 64 { 65 throw new RuntimeException ("Illegal cflow expression: " + original, e); 66 } 67 point = (SimpleNode) exc.jjtGetChild(0); 68 this.not = not; 69 } 70 71 public int matches(StackTraceElement [] stack, int index) 72 { 73 int found = NOT_FOUND; 74 if (point instanceof ASTMethod) 75 { 76 found = matches((ASTMethod) point, stack, index); 77 } 78 else 79 { 80 found = matches((ASTConstructor) point, stack, index); 81 } 82 if ((found == NOT_FOUND) && not) return index; 83 if (found > NOT_FOUND && not) return NOT_FOUND; 84 return found; 85 } 86 87 private int matches(ASTMethod method, StackTraceElement [] stack, int index) 88 { 89 for (int i = index; i >= 0; i--) 90 { 91 ClassExpression expr = method.getClazz(); 92 if (!matchesClass(expr, stack[i])) continue; 93 94 if (method.getMethodIdentifier().matches(stack[i].getMethodName())) 95 { 96 return i - 1; 97 } 98 } 99 return -2; 100 } 101 102 private int matches(ASTConstructor con, StackTraceElement [] stack, int index) 103 { 104 for (int i = index; i >= 0; i--) 105 { 106 ClassExpression expr = con.getClazz(); 107 if (!matchesClass(expr, stack[i])) continue; 108 109 if (stack[i].getMethodName().equals("<init>")) 110 { 111 return i - 1; } 113 } 114 return -2; 115 } 116 117 private boolean matchesClass(ClassExpression expr, StackTraceElement element) 118 { 119 if (expr.isSimple()) 120 { 121 if (!expr.matches(element.getClassName())) 122 { 123 return false; 124 } 125 } 126 else 127 { 128 Class clazz = loadClass(element.getClassName()); 129 130 if (Untransformable.class.isAssignableFrom(clazz)) 131 { 132 return false; 134 } 135 if (!Util.matchesClassExpr(expr, clazz)) 136 { 137 return false; 138 } 139 } 140 141 return true; 142 } 143 144 private Class loadClass(String name) 145 { 146 return SecurityActions.loadClass(name); 147 } 148 } 149 | Popular Tags |