KickJava   Java API By Example, From Geeks To Geeks.

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


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.joinpoint.ConstructorCalledByConstructorInvocation;
25 import org.jboss.aop.joinpoint.ConstructorCalledByMethodInvocation;
26 import org.jboss.aop.joinpoint.ConstructorInvocation;
27 import org.jboss.aop.joinpoint.FieldReadInvocation;
28 import org.jboss.aop.joinpoint.FieldWriteInvocation;
29 import org.jboss.aop.joinpoint.Invocation;
30 import org.jboss.aop.joinpoint.MethodCalledByConstructorInvocation;
31 import org.jboss.aop.joinpoint.MethodCalledByMethodInvocation;
32 import org.jboss.aop.joinpoint.MethodInvocation;
33 import org.jboss.aop.pointcut.ast.ParseException;
34
35 /**
36  * Allows you to match an Invocation to a given joinpoint.
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 46033 $
40  */

41 public class JoinPointMatcher
42 {
43    Pointcut p;
44
45    /**
46     * @param expr a pointcut expression to match against
47     * @throws ParseException
48     */

49    public JoinPointMatcher(String JavaDoc expr) throws ParseException
50    {
51       p = new PointcutExpression("test", expr);
52    }
53
54    public boolean matches(Invocation invocation)
55    {
56       if (invocation instanceof MethodInvocation)
57       {
58          MethodInvocation mi = (MethodInvocation) invocation;
59          PointcutMethodMatch pmatch = p.matchesExecution(mi.getAdvisor(), mi.getMethod());
60          if (pmatch == null)
61          {
62             return false;
63          }
64          return pmatch.isMatch();
65       }
66       else if (invocation instanceof ConstructorInvocation)
67       {
68          ConstructorInvocation mi = (ConstructorInvocation) invocation;
69          return p.matchesExecution(mi.getAdvisor(), mi.getConstructor());
70       }
71       else if (invocation instanceof FieldReadInvocation)
72       {
73          FieldReadInvocation mi = (FieldReadInvocation) invocation;
74          return p.matchesGet(mi.getAdvisor(), mi.getField());
75       }
76       else if (invocation instanceof FieldWriteInvocation)
77       {
78          FieldWriteInvocation mi = (FieldWriteInvocation) invocation;
79          return p.matchesSet(mi.getAdvisor(), mi.getField());
80       }
81       else if (invocation instanceof MethodCalledByMethodInvocation)
82       {
83          MethodCalledByMethodInvocation mi = (MethodCalledByMethodInvocation) invocation;
84          return p.matchesCall(mi.getAdvisor(), mi.getCallingMethod(), mi.getCalledMethod().getDeclaringClass(), mi.getCalledMethod());
85       }
86       else if (invocation instanceof MethodCalledByConstructorInvocation)
87       {
88          MethodCalledByConstructorInvocation mi = (MethodCalledByConstructorInvocation) invocation;
89          return p.matchesCall(mi.getAdvisor(), mi.getCalling(), mi.getCalledMethod().getDeclaringClass(), mi.getCalledMethod());
90       }
91       else if (invocation instanceof ConstructorCalledByConstructorInvocation)
92       {
93          ConstructorCalledByConstructorInvocation mi = (ConstructorCalledByConstructorInvocation) invocation;
94          return p.matchesCall(mi.getAdvisor(), mi.getCallingConstructor(), mi.getCalledConstructor().getDeclaringClass(), mi.getCalledConstructor());
95       }
96       else if (invocation instanceof ConstructorCalledByMethodInvocation)
97       {
98          ConstructorCalledByMethodInvocation mi = (ConstructorCalledByMethodInvocation) invocation;
99          return p.matchesCall(mi.getAdvisor(), mi.getCallingMethod(), mi.getCalledConstructor().getDeclaringClass(), mi.getCalledConstructor());
100       }
101       throw new RuntimeException JavaDoc("UNKNOWN JOINPOINT TYPE: " + invocation.getClass().getName());
102    }
103 }
104
Popular Tags