KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.jboss.aop.Advisor;
28 import org.jboss.aop.pointcut.ast.ASTAll;
29 import org.jboss.aop.pointcut.ast.ASTAttribute;
30 import org.jboss.aop.pointcut.ast.ASTMethod;
31 import org.jboss.aop.pointcut.ast.ASTStart;
32 import org.jboss.aop.pointcut.ast.ClassExpression;
33
34 import javassist.CtMethod;
35 import javassist.NotFoundException;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 46033 $
42  */

43 public class MethodMatcher extends MatcherHelper
44 {
45    protected Advisor advisor;
46    protected CtMethod ctMethod;
47    protected Method JavaDoc refMethod;
48    protected int methodModifiers;
49    protected String JavaDoc classname;
50    protected String JavaDoc methodName;
51    protected Class JavaDoc matchedClass;
52    protected int matchLevel;//0 if matches the exact class, 1 if the direct superclass etc.
53
protected boolean isInstanceof;
54
55    public MethodMatcher(Advisor advisor, CtMethod method, ASTStart start)
56    {
57       super(start, advisor.getManager());
58       this.advisor = advisor;
59       this.start = start;
60       this.methodModifiers = method.getModifiers();
61       this.classname = method.getDeclaringClass().getName();
62       this.ctMethod = method;
63       this.methodName = ctMethod.getName();
64    }
65
66    public MethodMatcher(Advisor advisor, Method JavaDoc method, ASTStart start)
67    {
68       super(start, advisor.getManager());
69       this.advisor = advisor;
70       this.start = start;
71       this.methodModifiers = method.getModifiers();
72       this.classname = method.getDeclaringClass().getName();
73       this.refMethod = method;
74       this.methodName = refMethod.getName();
75    }
76
77    public Class JavaDoc getMatchedClass()
78    {
79       return matchedClass;
80    }
81  
82    public int getMatchLevel()
83    {
84       return matchLevel;
85    }
86    
87    public boolean isInstanceOf()
88    {
89       return isInstanceof;
90    }
91  
92    protected Boolean JavaDoc resolvePointcut(Pointcut p)
93    {
94       throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
95    }
96
97    public Object JavaDoc visit(ASTMethod node, Object JavaDoc data)
98    {
99       return matches(node);
100    }
101
102    public Boolean JavaDoc matches(ASTMethod node)
103    {
104       if (!matchesModifiers(node)) return Boolean.FALSE;
105       if (!matchesClass(node)) return Boolean.FALSE;
106       if (!matchesIdentifier(node))return Boolean.FALSE;
107       if (!matchesExceptions(node))return Boolean.FALSE;
108       if (!matchesReturnType(node)) return Boolean.FALSE;
109       if (!matchesParameters(node)) return Boolean.FALSE;
110
111       return Boolean.TRUE;
112    }
113
114    public Object JavaDoc visit(ASTAll node, Object JavaDoc data)
115    {
116       Boolean JavaDoc matches = Boolean.FALSE;
117       ClassExpression expr = node.getClazz();
118       
119       if (ctMethod != null)
120       {
121          return classMatchesAll(expr);
122       }
123       else
124       {
125          Class JavaDoc declaringClass = MatcherStrategy.getMatcher(advisor).getDeclaringClass(advisor, refMethod);
126          if (!advisor.chainOverridingForInheritedMethods())
127          {
128             matches = classMatchesAll(expr);
129          }
130          else
131          {
132             Class JavaDoc advisedClass = advisor.getClazz();
133             matchedClass = advisedClass;
134             while (matchedClass != null)
135             {
136                if (classMatchesAll(node.getClazz()).booleanValue())
137                {
138                   if (node.getClazz().isInstanceOf())
139                   {
140                      isInstanceof = true;
141                   }
142                   return Boolean.TRUE;
143                }
144                
145                if (matchedClass == declaringClass)
146                {
147                   break;
148                }
149                matchedClass = matchedClass.getSuperclass();
150                matchLevel++;
151             }
152          }
153       }
154       return matches;
155    }
156    
157    public Boolean JavaDoc classMatchesAll(ClassExpression expr)
158    {
159       if (expr.isAnnotation())
160       {
161          String JavaDoc sub = expr.getOriginal().substring(1);
162          if (ctMethod != null)
163          {
164             if (!advisor.getMethodMetaData().hasGroup(ctMethod, sub))
165             {
166                if (!advisor.getDefaultMetaData().hasTag(sub))
167                {
168                   if (!advisor.hasAnnotation(ctMethod, sub)) return Boolean.FALSE;
169                }
170             }
171          }
172          else
173          {
174             if (!advisor.getMethodMetaData().hasTag(refMethod, sub))
175             {
176                if (!advisor.getDefaultMetaData().hasTag(sub))
177                {
178                   try
179                   {
180                      if (!advisor.hasAnnotation(refMethod, sub)) return Boolean.FALSE;
181                   }
182                   catch (Exception JavaDoc e)
183                   {
184                      throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
185
}
186                }
187             }
188          }
189       }
190       else if (expr.isInstanceOf())
191       {
192          if (ctMethod != null)
193          {
194             if (!Util.subtypeOf(ctMethod.getDeclaringClass(), expr, advisor)) return Boolean.FALSE;
195          }
196          else if (!Util.subtypeOf(refMethod.getDeclaringClass(), expr, advisor)) return Boolean.FALSE;
197
198       }
199       else if (expr.isTypedef())
200       {
201          if (ctMethod != null)
202          {
203             try
204             {
205                if (!Util.matchesTypedef(ctMethod.getDeclaringClass(), expr, advisor)) return Boolean.FALSE;
206             }
207             catch (Exception JavaDoc e)
208             {
209                throw new RuntimeException JavaDoc(e);
210             }
211          }
212          else if (!Util.matchesTypedef(refMethod.getDeclaringClass(), expr, advisor)) return Boolean.FALSE;
213       }
214       else if (!expr.matches(classname))
215       {
216          return Boolean.FALSE;
217       }
218
219       return Boolean.TRUE;
220    }
221    
222    protected boolean matchesModifiers(ASTMethod node)
223    {
224       if (node.getAttributes().size() > 0)
225       {
226          for (int i = 0; i < node.getAttributes().size(); i++)
227          {
228             ASTAttribute attr = (ASTAttribute) node.getAttributes().get(i);
229             if (!Util.matchModifiers(attr, methodModifiers)) return false;
230          }
231       }
232       return true;
233    }
234
235    protected boolean matchesClass(ASTMethod node)
236    {
237       if (ctMethod != null)
238       {
239          if (!Util.matchesClassExpr(node.getClazz(), ctMethod.getDeclaringClass(), advisor)) return false;
240       }
241       else
242       {
243          Class JavaDoc declaringClass = MatcherStrategy.getMatcher(advisor).getDeclaringClass(advisor, refMethod);
244          if (!advisor.chainOverridingForInheritedMethods())
245          {
246             if (Util.matchesClassExpr(node.getClazz(), declaringClass, advisor))
247             {
248                matchedClass = declaringClass;
249                return true;
250             }
251             return false;
252          }
253          else
254          {
255             Class JavaDoc advisedClass = advisor.getClazz();
256             if (advisor.getClazz() == null)
257             {
258                throw new RuntimeException JavaDoc("Advisor is null");
259             }
260             matchedClass = advisedClass;
261             while (matchedClass != null)
262             {
263                if (Util.matchesClassExpr(node.getClazz(), matchedClass, advisor))
264                {
265                   if (node.getClazz().isInstanceOf())
266                   {
267                      isInstanceof = true;
268                   }
269                   return true;
270                }
271                
272                if (matchedClass == declaringClass)
273                {
274                   break;
275                }
276                matchedClass = matchedClass.getSuperclass();
277                matchLevel++;
278             }
279             return false;
280          }
281       }
282       return true;
283    }
284    
285    protected boolean matchesIdentifier(ASTMethod node)
286    {
287       if (node.getMethodIdentifier().isAnnotation())
288       {
289          if (advisor == null) return false;
290          String JavaDoc sub = node.getMethodIdentifier().getOriginal().substring(1);
291          if (ctMethod != null)
292          {
293             if (!advisor.getMethodMetaData().hasGroup(ctMethod, sub))
294             {
295                if (!advisor.getDefaultMetaData().hasTag(sub))
296                {
297                   if (!advisor.hasAnnotation(ctMethod, sub)) return false;
298                }
299             }
300          }
301          else
302          {
303             if (!advisor.getMethodMetaData().hasTag(refMethod, sub))
304             {
305                if (!advisor.getDefaultMetaData().hasTag(sub))
306                {
307                   try
308                   {
309                      if (!advisor.hasAnnotation(refMethod, sub)) return false;
310                   }
311                   catch (Exception JavaDoc e)
312                   {
313                      throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
314
}
315                }
316             }
317          }
318       }
319       else if (node.getMethodIdentifier().isImplements() || node.getMethodIdentifier().isImplementing())
320       {
321          try
322          {
323             boolean exactSuper = node.getMethodIdentifier().isImplements(); //Implementing will check all super classes
324
ClassExpression implemented = node.getMethodIdentifier().getImplementsExpression();
325             if (ctMethod != null)
326             {
327                if (Util.methodExistsInSuperClassOrInterface(ctMethod, implemented, exactSuper))
328                {
329                   return true;
330                }
331             }
332             else
333             {
334                if (Util.methodExistsInSuperClassOrInterface(refMethod, implemented, exactSuper, advisor))
335                {
336                   return true;
337                }
338             }
339          }
340          catch (Exception JavaDoc e)
341          {
342             throw new RuntimeException JavaDoc(e);
343          }
344          return false;
345       }
346       else
347       {
348          if (!node.getMethodIdentifier().matches(methodName)) return false;
349       }
350       return true;
351    }
352       
353    protected boolean matchesExceptions(ASTMethod node)
354    {
355       //Match exceptions
356
ArrayList JavaDoc nodeExceptions = node.getExceptions();
357       if (nodeExceptions.size() > 0)
358       {
359          if (ctMethod != null)
360          {
361             try
362             {
363                if (!Util.matchExceptions(nodeExceptions, ctMethod.getExceptionTypes()))
364                {
365                   return false;
366                }
367             }
368             catch (NotFoundException e)
369             {
370                throw new RuntimeException JavaDoc(e);
371             }
372          }
373          else
374          {
375             if (!Util.matchExceptions(nodeExceptions, refMethod.getExceptionTypes()))
376             {
377                return false;
378             }
379          }
380       }
381       return true;
382    }
383    
384    protected boolean matchesReturnType(ASTMethod node)
385    {
386       try
387       {
388          if (ctMethod != null)
389          {
390             if (!Util.matchesClassExpr(node.getReturnType(), ctMethod.getReturnType(), advisor)) return false;
391          }
392          else
393          {
394             if (!Util.matchesClassExpr(node.getReturnType(), refMethod.getReturnType(), advisor)) return false;
395          }
396       }
397       catch (NotFoundException nfe)
398       {
399          throw new RuntimeException JavaDoc(nfe);
400       }
401       
402       return true;
403    }
404    
405    protected boolean matchesParameters(ASTMethod node)
406    {
407       if (ctMethod != null)
408       {
409          return Util.matchesParameters(advisor, node, ctMethod);
410       }
411       else
412       {
413          return Util.matchesParameters(advisor, node, refMethod);
414       }
415    }
416 }
417
Popular Tags