KickJava   Java API By Example, From Geeks To Geeks.

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


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 javassist.CtMethod;
25 import javassist.NotFoundException;
26 import javassist.expr.MethodCall;
27 import org.jboss.aop.Advisor;
28 import org.jboss.aop.AspectManager;
29 import org.jboss.aop.pointcut.ast.ASTCall;
30 import org.jboss.aop.pointcut.ast.ASTConstructor;
31 import org.jboss.aop.pointcut.ast.ASTField;
32 import org.jboss.aop.pointcut.ast.ASTHas;
33 import org.jboss.aop.pointcut.ast.ASTHasField;
34 import org.jboss.aop.pointcut.ast.ASTMethod;
35 import org.jboss.aop.pointcut.ast.ASTStart;
36 import org.jboss.aop.pointcut.ast.ASTWithin;
37 import org.jboss.aop.pointcut.ast.ASTWithincode;
38 import org.jboss.aop.pointcut.ast.Node;
39
40 /**
41  * Comment
42  *
43  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
44  * @version $Revision: 37406 $
45  */

46 public class MethodCallMatcher extends MatcherHelper
47 {
48    MethodCall call;
49    Advisor advisor;
50
51    public MethodCallMatcher(Advisor advisor, MethodCall call, ASTStart start) throws NotFoundException
52    {
53       super(start, advisor.getManager());
54       this.advisor = advisor;
55       this.call = call;
56    }
57
58    public Object JavaDoc visit(ASTCall node, Object JavaDoc data)
59    {
60       try
61       {
62          if (!(node.getBehavior() instanceof ASTMethod)) return Boolean.FALSE;
63
64          ASTMethod astMethod = (ASTMethod) node.getBehavior();
65
66          // do simple checks to avoid loading CtClasses
67
if (astMethod.getClazz().isSimple())
68          {
69             if (!astMethod.getClazz().matches(call.getClassName())) return Boolean.FALSE;
70             if (!astMethod.getMethodIdentifier().isAnnotation())
71             {
72                if (!astMethod.getMethodIdentifier().matches(call.getMethodName())) return Boolean.FALSE;
73             }
74          }
75          CtMethod calledMethod = call.getMethod();
76          Advisor calledAdvisor = AspectManager.instance().getTempClassAdvisor(calledMethod.getDeclaringClass());
77          MethodMatcher methodMatcher = new MethodMatcher(calledAdvisor, calledMethod, null);
78          return methodMatcher.matches(astMethod);
79       }
80       catch (Exception JavaDoc e)
81       {
82          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
83
}
84    }
85
86    public Object JavaDoc visit(ASTHas node, Object JavaDoc data)
87    {
88       try
89       {
90          Node n = node.jjtGetChild(0);
91          CtMethod method = call.getMethod();
92          if (n instanceof ASTMethod)
93          {
94             return new Boolean JavaDoc(Util.has(method.getDeclaringClass(), (ASTMethod) n, advisor));
95          }
96          else
97          {
98             return new Boolean JavaDoc(Util.has(method.getDeclaringClass(), (ASTConstructor) n, advisor));
99          }
100       }
101       catch (NotFoundException e)
102       {
103          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
104
}
105    }
106
107    public Object JavaDoc visit(ASTHasField node, Object JavaDoc data)
108    {
109       ASTField f = (ASTField) node.jjtGetChild(0);
110       CtMethod method = null;
111       try
112       {
113          method = call.getMethod();
114       }
115       catch (NotFoundException e)
116       {
117          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
118
}
119       return new Boolean JavaDoc(Util.has(method.getDeclaringClass(), f, advisor));
120    }
121
122    public Object JavaDoc visit(ASTWithin node, Object JavaDoc data)
123    {
124       WithinMatcher within = null;
125       try
126       {
127          within = new WithinMatcher(advisor, call.where(), null);
128       }
129       catch (NotFoundException e)
130       {
131          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
132
}
133       return node.jjtAccept(within, null);
134    }
135
136    public Object JavaDoc visit(ASTWithincode node, Object JavaDoc data)
137    {
138       WithinMatcher within = null;
139       try
140       {
141          within = new WithinMatcher(advisor, call.where(), null);
142       }
143       catch (NotFoundException e)
144       {
145          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
146
}
147       return node.jjtAccept(within, null);
148    }
149
150    protected Boolean JavaDoc resolvePointcut(Pointcut p)
151    {
152       try
153       {
154          return new Boolean JavaDoc(p.matchesCall(advisor, call));
155       }
156       catch (NotFoundException e)
157       {
158          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
159
}
160    }
161
162 }
163
Popular Tags