KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.StringReader JavaDoc;
25 import java.lang.reflect.AccessibleObject JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
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 /**
42  * Comment
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 46033 $
46  */

47 public class PointcutExpression implements Pointcut
48 {
49    protected String JavaDoc name;
50    protected String JavaDoc expr;
51    protected ASTStart ast;
52
53    protected PointcutStats stats;
54
55    public PointcutExpression(String JavaDoc name, String JavaDoc expr) throws ParseException
56    {
57       this.name = name;
58       this.expr = expr;
59
60
61       ast = new PointcutExpressionParser(new StringReader JavaDoc(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 JavaDoc getName()
79    {
80       return name;
81    }
82
83    public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e)
182       {
183          throw new RuntimeException JavaDoc("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 JavaDoc within, Class JavaDoc calledClass, Method JavaDoc 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 JavaDoc within, Class JavaDoc calledClass, Constructor JavaDoc 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 JavaDoc toString()
249    {
250       return "Pointcut[name=" + name + "; expr=" + expr + "]";
251    }
252 }
253
Popular Tags