KickJava   Java API By Example, From Geeks To Geeks.

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


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.CtClass;
25 import javassist.CtConstructor;
26 import javassist.CtField;
27 import javassist.CtMethod;
28 import javassist.NotFoundException;
29 import org.jboss.aop.Advisor;
30 import org.jboss.aop.pointcut.ast.ASTAllParameter;
31 import org.jboss.aop.pointcut.ast.ASTAnd;
32 import org.jboss.aop.pointcut.ast.ASTAttribute;
33 import org.jboss.aop.pointcut.ast.ASTBoolean;
34 import org.jboss.aop.pointcut.ast.ASTClass;
35 import org.jboss.aop.pointcut.ast.ASTComposite;
36 import org.jboss.aop.pointcut.ast.ASTConstructor;
37 import org.jboss.aop.pointcut.ast.ASTException;
38 import org.jboss.aop.pointcut.ast.ASTField;
39 import org.jboss.aop.pointcut.ast.ASTHas;
40 import org.jboss.aop.pointcut.ast.ASTHasField;
41 import org.jboss.aop.pointcut.ast.ASTMethod;
42 import org.jboss.aop.pointcut.ast.ASTNot;
43 import org.jboss.aop.pointcut.ast.ASTOr;
44 import org.jboss.aop.pointcut.ast.ASTParameter;
45 import org.jboss.aop.pointcut.ast.ASTStart;
46 import org.jboss.aop.pointcut.ast.ASTSub;
47 import org.jboss.aop.pointcut.ast.Node;
48 import org.jboss.aop.pointcut.ast.SimpleNode;
49 import org.jboss.aop.pointcut.ast.TypeExpressionParserVisitor;
50
51 import java.lang.reflect.Constructor JavaDoc;
52 import java.lang.reflect.Field JavaDoc;
53 import java.lang.reflect.Method JavaDoc;
54
55 /**
56  * Takes a Javassist object and sees if the expression
57  * pertains to that Javassist object (CtMethod, CtField, CtClass, CtConstructor)
58  *
59  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
60  * @version $Revision: 37406 $
61  */

62 public class AnnotationMatcher implements TypeExpressionParserVisitor
63 {
64    private Advisor advisor;
65    private Object JavaDoc element;
66
67    public AnnotationMatcher(Advisor advisor, Object JavaDoc element)
68    {
69       this.advisor = advisor;
70       this.element = element;
71    }
72
73    public Object JavaDoc visit(ASTStart node, Object JavaDoc data)
74    {
75       return node.jjtGetChild(0).jjtAccept(this, data);
76    }
77
78    public Object JavaDoc visit(ASTBoolean node, Object JavaDoc data)
79    {
80       return node.jjtGetChild(0).jjtAccept(this, data);
81    }
82
83    public Object JavaDoc visit(ASTComposite node, Object JavaDoc data)
84    {
85       return node.jjtGetChild(0).jjtAccept(this, data);
86    }
87
88    public Object JavaDoc visit(ASTNot node, Object JavaDoc data)
89    {
90
91       Boolean JavaDoc bool = (Boolean JavaDoc) node.jjtGetChild(0).jjtAccept(this, data);
92       boolean val = bool.booleanValue();
93       return val ? Boolean.FALSE : Boolean.TRUE;
94    }
95
96    public Object JavaDoc visit(ASTSub node, Object JavaDoc data)
97    {
98       for (int i = 0; i < node.jjtGetNumChildren(); i++)
99       {
100          data = node.jjtGetChild(i).jjtAccept(this, data);
101       }
102       return data;
103    }
104
105    public Object JavaDoc visit(ASTAnd node, Object JavaDoc left)
106    {
107       Node andChild = node.jjtGetChild(0); // should only have one child
108
boolean val = ((Boolean JavaDoc) left).booleanValue();
109       return new Boolean JavaDoc(val && ((Boolean JavaDoc) andChild.jjtAccept(this, Boolean.FALSE)).booleanValue());
110    }
111
112    public Object JavaDoc visit(ASTOr node, Object JavaDoc left)
113    {
114       Node orChild = node.jjtGetChild(0); // should only have one child
115
boolean val = ((Boolean JavaDoc) left).booleanValue();
116       return new Boolean JavaDoc(val || ((Boolean JavaDoc) orChild.jjtAccept(this, Boolean.FALSE)).booleanValue());
117    }
118
119
120    public Object JavaDoc visit(SimpleNode node, Object JavaDoc data)
121    {
122       return null;
123    }
124
125    public CtClass getDeclaringClass()
126    {
127       if (element instanceof CtClass) return (CtClass) element;
128       if (element instanceof CtMethod) return ((CtMethod) element).getDeclaringClass();
129       if (element instanceof CtField) return ((CtField) element).getDeclaringClass();
130       if (element instanceof CtConstructor) return ((CtConstructor) element).getDeclaringClass();
131       return null;
132    }
133
134    public Class JavaDoc getJavaDeclaringClass()
135    {
136       if (element instanceof Class JavaDoc) return (Class JavaDoc) element;
137       if (element instanceof Method) return ((Method) element).getDeclaringClass();
138       if (element instanceof Field) return ((Field) element).getDeclaringClass();
139       if (element instanceof Constructor) return ((Constructor) element).getDeclaringClass();
140       return null;
141    }
142
143    public Object JavaDoc visit(ASTHas node, Object JavaDoc data)
144    {
145       Node n = node.jjtGetChild(0);
146       if (getDeclaringClass() != null)
147       {
148          CtClass clazz = getDeclaringClass();
149          if (n instanceof ASTMethod)
150          {
151             return new Boolean JavaDoc(Util.has(clazz, (ASTMethod) n, advisor));
152          }
153          else
154          {
155             return new Boolean JavaDoc(Util.has(clazz, (ASTConstructor) n, advisor));
156          }
157       }
158       else
159       {
160          Class JavaDoc clazz = getJavaDeclaringClass();
161          if (n instanceof ASTMethod)
162          {
163             return new Boolean JavaDoc(Util.has(clazz, (ASTMethod) n, advisor));
164          }
165          else
166          {
167             return new Boolean JavaDoc(Util.has(clazz, (ASTConstructor) n, advisor));
168          }
169       }
170
171    }
172
173    public Object JavaDoc visit(ASTHasField node, Object JavaDoc data)
174    {
175       if (getDeclaringClass() != null)
176       {
177          CtClass clazz = getDeclaringClass();
178          ASTField f = (ASTField) node.jjtGetChild(0);
179          return new Boolean JavaDoc(Util.has(clazz, f, advisor));
180       }
181       else
182       {
183          Class JavaDoc clazz = getJavaDeclaringClass();
184          ASTField f = (ASTField) node.jjtGetChild(0);
185          return new Boolean JavaDoc(Util.has(clazz, f, advisor));
186       }
187    }
188
189    public Object JavaDoc visit(ASTClass node, Object JavaDoc data)
190    {
191       if (!(element instanceof CtClass) && !(element instanceof Class JavaDoc)) return Boolean.FALSE;
192       if (element instanceof CtClass)
193       {
194          CtClass clazz = (CtClass) element;
195          return new Boolean JavaDoc(Util.matchesClassExpr(node.getClazz(), clazz, advisor));
196       }
197       else
198       {
199          Class JavaDoc clazz = (Class JavaDoc) element;
200          return new Boolean JavaDoc(Util.matchesClassExpr(node.getClazz(), clazz, advisor));
201       }
202
203    }
204
205    public Object JavaDoc visit(ASTMethod node, Object JavaDoc data)
206    {
207       if (!(element instanceof CtMethod) && !(element instanceof Method)) return Boolean.FALSE;
208       if (element instanceof CtMethod)
209       {
210          MethodMatcher methodMatcher = null;
211          methodMatcher = new MethodMatcher(advisor, (CtMethod) element, null);
212          return methodMatcher.matches(node);
213       }
214       else
215       {
216          MethodMatcher methodMatcher = null;
217          methodMatcher = new MethodMatcher(advisor, (Method) element, null);
218          return methodMatcher.matches(node);
219       }
220    }
221
222    public Object JavaDoc visit(ASTConstructor node, Object JavaDoc data)
223    {
224       if (!(element instanceof CtConstructor) && !(element instanceof Constructor)) return Boolean.FALSE;
225       if (element instanceof CtConstructor)
226       {
227          ConstructorMatcher conMatcher = null;
228          try
229          {
230             conMatcher = new ConstructorMatcher(advisor, (CtConstructor) element, null);
231          }
232          catch (NotFoundException e)
233          {
234             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
235
}
236          return conMatcher.matches(node);
237       }
238       else
239       {
240          ConstructorMatcher conMatcher = null;
241          conMatcher = new ConstructorMatcher(advisor, (Constructor) element, null);
242          return conMatcher.matches(node);
243       }
244
245    }
246
247    public Object JavaDoc visit(ASTField node, Object JavaDoc data)
248    {
249       if (!(element instanceof CtField) && !(element instanceof Field)) return Boolean.FALSE;
250       if (element instanceof CtField)
251       {
252          FieldMatcher fieldMatcher = null;
253          try
254          {
255             fieldMatcher = new FieldMatcher(advisor, (CtField) element, null);
256          }
257          catch (NotFoundException e)
258          {
259             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
260
}
261          return node.jjtAccept(fieldMatcher, null);
262       }
263       else
264       {
265          FieldMatcher fieldMatcher = null;
266          fieldMatcher = new FieldMatcher(advisor, (Field) element, null);
267          return node.jjtAccept(fieldMatcher, null);
268       }
269
270    }
271
272    public Object JavaDoc visit(ASTAttribute node, Object JavaDoc data)
273    {
274       return Boolean.FALSE;
275    }
276
277    public Object JavaDoc visit(ASTParameter node, Object JavaDoc data)
278    {
279       return Boolean.FALSE;
280    }
281
282    public Object JavaDoc visit(ASTAllParameter node, Object JavaDoc data)
283    {
284       return Boolean.FALSE;
285    }
286
287    public Object JavaDoc visit(ASTException node, Object JavaDoc data)
288    {
289       return Boolean.FALSE;
290    }
291    
292 }
293
Popular Tags