KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > pointcut > CFlow


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
34 import java.io.StringReader JavaDoc;
35 import java.security.AccessController JavaDoc;
36 import java.security.PrivilegedActionException JavaDoc;
37 import java.security.PrivilegedExceptionAction JavaDoc;
38
39 /**
40  * Comment
41  *
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @version $Revision: 45968 $
44  *
45  **/

46 public class CFlow
47 {
48    public static int NOT_FOUND = -2;
49
50    private String JavaDoc original;
51    private SimpleNode point;
52    private boolean not;
53
54    public CFlow(String JavaDoc expr, boolean not)
55    {
56       original = expr;
57       expr = "execution(" + expr + ")";
58       ASTExecution exc = null;
59       try
60       {
61          exc = new PointcutExpressionParser(new StringReader JavaDoc(expr)).execution();
62       }
63       catch (ParseException e)
64       {
65          throw new RuntimeException JavaDoc("Illegal cflow expression: " + original, e);
66       }
67       point = (SimpleNode) exc.jjtGetChild(0);
68       this.not = not;
69    }
70
71    public int matches(StackTraceElement JavaDoc[] 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 JavaDoc[] 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 JavaDoc[] 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; // match
112
}
113       }
114       return -2;
115    }
116    
117    private boolean matchesClass(ClassExpression expr, StackTraceElement JavaDoc element)
118    {
119       if (expr.isSimple())
120       {
121          if (!expr.matches(element.getClassName()))
122          {
123             return false;
124          }
125       }
126       else
127       {
128          Class JavaDoc clazz = loadClass(element.getClassName());
129          
130          if (Untransformable.class.isAssignableFrom(clazz))
131          {
132             //Invocation classes should not be checked (they are not in the cache at runtime)
133
return false;
134          }
135          if (!Util.matchesClassExpr(expr, clazz))
136          {
137             return false;
138          }
139       }
140       
141       return true;
142    }
143    
144    private Class JavaDoc loadClass(String JavaDoc name)
145    {
146       return SecurityActions.loadClass(name);
147    }
148 }
149
Popular Tags