KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > instrument > JoinpointClassifier


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.instrument;
23
24 import javassist.CtConstructor;
25 import javassist.CtField;
26 import javassist.CtMember;
27 import javassist.CtMethod;
28 import javassist.NotFoundException;
29
30 import org.jboss.aop.Advisor;
31 import org.jboss.aop.pointcut.Pointcut;
32
33 /**
34  * This class performs the joinpoint classifications. It is responsible for classifying
35  * a joinpoint into:<ul>
36  * <li> NOT_INSTRUMENTED: the joinpoint mustn't be instrumented.</li>
37  * <li> PREPARED: means that the joinpoint containing class must be instrumented (class
38  * signature change) before it gets loaded. This step makes the runtime wrapping instrumentation
39  * feasible.</li>
40  * <li> WRAPPED: the joinpoint must be submitted
41  * to wrapping instrumentation, i.e., it must be wrapped inside a block code responsible for
42  * intercepting the joinpoint.</li>
43  * <li> DYNAMICALY_WRAPPED: equivalent to the previous one, except that this classification indicates
44  * the joinpoint will be wrapped due only to bindings added dynamicaly..</li>
45  * <ul>
46  * @author Flavia Rainone
47  * @see org.jboss.aop.instrument.JoinpointClassification
48  */

49 public abstract class JoinpointClassifier
50 {
51    /**
52     * This interface encapsulates a matching method. This allows us to use instances
53     * of <code>Matcher</code> as references to matching methods.
54     */

55    protected interface Matcher
56    {
57       /**
58        * Checks if <code>pointcut</code> matches a joinpoint. The joinpoint this method refers
59        * to is dependend of the <code>Macther</code> interface implementer. All that is known
60        * about the joinpoint is the class member related to it: <code>member</code>.
61        * @param pointcut the pointcut whose matching will be tested.
62        * @param advisor advisor associated with the class declaring <code>member</code>.
63        * @param member class member related to the joinpoint.
64        * @return <code>true</code> if the <code>pointcut</code> matches the joinpoint.
65        * @throws NotFoundException thrown if the matching of pointcut fails.
66        */

67       boolean matches(Pointcut pointcut, Advisor advisor, CtMember member) throws NotFoundException;
68    }
69    
70    /**
71     * Matches a field reading joinpoint against a pointcut expression.
72     */

73    private Matcher fieldGetMatcher = new Matcher()
74    {
75       public boolean matches(Pointcut pointcut, Advisor advisor, CtMember member) throws NotFoundException
76       {
77          return pointcut.matchesGet(advisor, (CtField) member);
78       }
79    };
80    
81    /**
82     * Matches a field writing joinpoint against a pointcut expression.
83     */

84    private Matcher fieldSetMatcher = new Matcher()
85    {
86       public boolean matches(Pointcut pointcut, Advisor advisor, CtMember member) throws NotFoundException
87       {
88          return pointcut.matchesSet(advisor, (CtField) member);
89       }
90    };
91    
92    /**
93     * Matches a constructor execution joinpoint against a pointcut expression.
94     */

95    private Matcher constructorMatcher = new Matcher()
96    {
97       public boolean matches(Pointcut pointcut, Advisor advisor, CtMember member) throws NotFoundException
98       {
99          return pointcut.matchesExecution(advisor, (CtConstructor) member);
100       }
101    };
102
103    /**
104     * Matches a pointcut with a method execution joinpoint.
105     */

106    private Matcher methodMatcher = new Matcher()
107    {
108       public boolean matches(Pointcut pointcut, Advisor advisor, CtMember member) throws NotFoundException
109       {
110          return pointcut.matchesExecution(advisor, (CtMethod) member);
111       }
112    };
113    
114    /**
115     * Classifies a joinpoint.
116     * Subclasses must implement this method, which contains the
117     * joinpoint classification algorithm.
118     * @param member the member associated with the joinpoint to be classified.
119     * @param advisor the advisor associated with <code>member</code> declaring class.
120     * @param joinpointMatcher this matcher must be used to find out if a
121     * pointcut matches the joinpoint.
122     * @return the joinpoint classification.
123     * @throws NotFoundException thrown if the matching of pointcuts fails.
124     */

125    protected abstract JoinpointClassification classifyJoinpoint(CtMember member, Advisor advisor, Matcher joinpointMatcher) throws NotFoundException;
126    
127    /**
128     * Classifies the reading of <code>field</code> value.
129     * @param field the field whose reading joinpoint is being classified.
130     * @param advisor the advisor related to the class declaring <code>field</code>.
131     * @return the classification of <code>field</code> read.
132     * @throws NotFoundException thrown if the matching of pointcuts fails.
133     */

134    public JoinpointClassification classifyFieldGet(CtField field, Advisor advisor) throws NotFoundException
135    {
136       return this.classifyJoinpoint(field, advisor, fieldGetMatcher);
137    }
138    
139    /**
140     * Classifies the writing of <code>field</code> value.
141     * @param field the field whose writing joinpoint is being classified.
142     * @param advisor the advisor related to the class declaring <code>field</code>.
143     * @return the classification of <code>field</code> write.
144     * @throws NotFoundException thrown if the matching of pointcuts fails.
145     */

146    public JoinpointClassification classifyFieldSet(CtField field, Advisor advisor) throws NotFoundException
147    {
148       return this.classifyJoinpoint(field, advisor, fieldSetMatcher);
149    }
150    
151    /**
152     * Classifies the execution of <code>cons</code>.
153     * @param cons the constructor whose execution joinpoint is being classified.
154     * @param advisor the advisor related to the class declaring <code>cons</code>.
155     * @return the classification of <code>cons</code> execution.
156     * @throws NotFoundException thrown if the matching of pointcuts fails.
157     */

158    public JoinpointClassification classifyConstructorExecution(CtConstructor cons, Advisor advisor) throws NotFoundException
159    {
160       return this.classifyJoinpoint(cons, advisor, constructorMatcher);
161    }
162
163    /**
164     * Classifies the execution of <code>method</code>.
165     * @param method the method whose execution joinpoint is being classified.
166     * @param advisor the advisor related to the class declaring <code>method</code>.
167     * @return the classification of <code>method</code> execution.
168     * @throws NotFoundException thrown if the matching of pointcuts fails.
169     */

170    public JoinpointClassification classifyMethodExecution(CtMethod method, Advisor advisor) throws NotFoundException
171    {
172       return this.classifyJoinpoint(method, advisor, methodMatcher);
173    }
174 }
Popular Tags