KickJava   Java API By Example, From Geeks To Geeks.

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


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.Advisor;
25 import org.jboss.aop.ClassAdvisor;
26 import org.jboss.aop.pointcut.ast.ASTAll;
27 import org.jboss.aop.pointcut.ast.ASTAllParameter;
28 import org.jboss.aop.pointcut.ast.ASTAndCFlow;
29 import org.jboss.aop.pointcut.ast.ASTAttribute;
30 import org.jboss.aop.pointcut.ast.ASTCFlow;
31 import org.jboss.aop.pointcut.ast.ASTCFlowBoolean;
32 import org.jboss.aop.pointcut.ast.ASTCFlowExpression;
33 import org.jboss.aop.pointcut.ast.ASTCall;
34 import org.jboss.aop.pointcut.ast.ASTCompositeCFlow;
35 import org.jboss.aop.pointcut.ast.ASTConstruction;
36 import org.jboss.aop.pointcut.ast.ASTConstructor;
37 import org.jboss.aop.pointcut.ast.ASTExecution;
38 import org.jboss.aop.pointcut.ast.ASTField;
39 import org.jboss.aop.pointcut.ast.ASTFieldExecution;
40 import org.jboss.aop.pointcut.ast.ASTGet;
41 import org.jboss.aop.pointcut.ast.ASTHas;
42 import org.jboss.aop.pointcut.ast.ASTHasField;
43 import org.jboss.aop.pointcut.ast.ASTMethod;
44 import org.jboss.aop.pointcut.ast.ASTNotCFlow;
45 import org.jboss.aop.pointcut.ast.ASTOrCFlow;
46 import org.jboss.aop.pointcut.ast.ASTParameter;
47 import org.jboss.aop.pointcut.ast.ASTSet;
48 import org.jboss.aop.pointcut.ast.ASTStart;
49 import org.jboss.aop.pointcut.ast.ASTSubCFlow;
50 import org.jboss.aop.pointcut.ast.ASTWithin;
51 import org.jboss.aop.pointcut.ast.ASTWithincode;
52 import org.jboss.aop.pointcut.ast.ClassExpression;
53 import org.jboss.aop.pointcut.ast.Node;
54 import org.jboss.aop.pointcut.ast.SimpleNode;
55
56 /**
57  * Comment
58  *
59  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
60  * @version $Revision: 43697 $
61  */

62 public class SoftClassMatcher extends MatcherHelper
63 {
64    protected Advisor advisor;
65    protected String JavaDoc classname;
66    protected boolean match = false;
67    protected Class JavaDoc clazz;
68
69    public SoftClassMatcher(Advisor advisor, String JavaDoc classname, ASTStart start)
70    {
71       super(start, advisor.getManager());
72       this.advisor = advisor;
73       this.classname = classname;
74       this.start = start;
75       clazz = advisor.getClazz();
76    }
77
78    protected Boolean JavaDoc resolvePointcut(Pointcut p)
79    {
80       return new Boolean JavaDoc(p.softMatch(advisor));
81    }
82
83    public Object JavaDoc visit(ASTCall node, Object JavaDoc data)
84    {
85       return node.getBehavior().jjtAccept(this, null);
86    }
87
88    public Object JavaDoc visit(ASTAll node, Object JavaDoc data)
89    {
90       return matches(node.getClazz());
91    }
92
93
94    public Boolean JavaDoc matches(ClassExpression expr)
95    {
96       if (expr.isAnnotation())
97       {
98          if (advisor == null) return Boolean.TRUE;
99          if (clazz == null) return Boolean.TRUE;
100          String JavaDoc sub = expr.getOriginal().substring(1);
101          if (!advisor.getClassMetaData().hasTag(sub))
102          {
103             try
104             {
105                if (clazz == null) return Boolean.TRUE;
106                if (!advisor.hasAnnotation(sub)) return Boolean.FALSE;
107             }
108             catch (Exception JavaDoc e)
109             {
110                throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
111
}
112          }
113       }
114       else if (expr.isInstanceOf())
115       {
116          if (clazz == null) return Boolean.TRUE;
117          if (!Util.subtypeOf(clazz, expr, advisor)) return Boolean.FALSE;
118
119       }
120       else if (expr.isTypedef())
121       {
122          if (clazz == null) return Boolean.TRUE; //Is this return correct?
123
if (!Util.matchesTypedef(clazz, expr, advisor)) return Boolean.FALSE;
124       }
125       else
126       {
127          if (!expr.matches(classname)) return Boolean.FALSE;
128       }
129       return Boolean.TRUE;
130    }
131
132    public Object JavaDoc visit(ASTWithin node, Object JavaDoc data)
133    {
134       return matches(node.getClazz());
135    }
136
137    public Object JavaDoc visit(ASTWithincode node, Object JavaDoc data)
138    {
139       return node.jjtGetChild(0).jjtAccept(this, null);
140    }
141
142    public Object JavaDoc visit(ASTExecution node, Object JavaDoc data)
143    {
144       return node.jjtGetChild(0).jjtAccept(this, null);
145    }
146
147    public Object JavaDoc visit(ASTConstruction node, Object JavaDoc data)
148    {
149       return node.jjtGetChild(0).jjtAccept(this, null);
150    }
151
152    public Object JavaDoc visit(ASTGet node, Object JavaDoc data)
153    {
154       return node.jjtGetChild(0).jjtAccept(this, null);
155    }
156
157    public Object JavaDoc visit(ASTSet node, Object JavaDoc data)
158    {
159       return node.jjtGetChild(0).jjtAccept(this, null);
160    }
161
162    public Object JavaDoc visit(ASTFieldExecution node, Object JavaDoc data)
163    {
164       return node.jjtGetChild(0).jjtAccept(this, null);
165    }
166
167    public Object JavaDoc visit(ASTMethod node, Object JavaDoc data)
168    {
169       ClassExpression classExpression = node.getClazz();
170       return matchClass(classExpression);
171    }
172
173    private Object JavaDoc matchClass(ClassExpression classExpression)
174    {
175       if (classExpression.isAnnotation())
176       {
177          String JavaDoc sub = classExpression.getOriginal().substring(1);
178          if (advisor.getClassMetaData().hasTag(sub)) return Boolean.TRUE;
179          return new Boolean JavaDoc(advisor.hasAnnotation(clazz, sub));
180       }
181       else if (classExpression.isInstanceOf())
182       {
183          if (clazz == null) return Boolean.TRUE;
184          if (Util.subtypeOf(clazz, classExpression, advisor)) return Boolean.TRUE;
185       }
186       else if (classExpression.isTypedef())
187       {
188          if (Util.matchesTypedef(clazz, classExpression, advisor)) return Boolean.TRUE;
189       }
190       else
191       {
192          return new Boolean JavaDoc(classExpression.matches(this.classname));
193       }
194       return Boolean.FALSE;
195    }
196
197    public Object JavaDoc visit(ASTConstructor node, Object JavaDoc data)
198    {
199       ClassExpression classExpression = node.getClazz();
200       return matchClass(classExpression);
201    }
202
203    
204    public Object JavaDoc visit(ASTField node, Object JavaDoc data)
205    {
206       ClassExpression classExpression = node.getClazz();
207       return matchClass(classExpression);
208    }
209
210    public Object JavaDoc visit(ASTHas node, Object JavaDoc data)
211    {
212       if (clazz == null) return Boolean.TRUE;
213       Node n = node.jjtGetChild(0);
214       if (n instanceof ASTMethod)
215       {
216          return new Boolean JavaDoc(Util.has(clazz, (ASTMethod) n, advisor));
217       }
218       else
219       {
220          return new Boolean JavaDoc(Util.has(clazz, (ASTConstructor) n, advisor));
221       }
222    }
223
224    public Object JavaDoc visit(ASTHasField node, Object JavaDoc data)
225    {
226       if (clazz == null) return Boolean.TRUE;
227       ASTField f = (ASTField) node.jjtGetChild(0);
228       return new Boolean JavaDoc(Util.has(clazz, f, advisor));
229    }
230
231    //----- NOT VISITED -----
232

233    public Object JavaDoc visit(ASTAttribute node, Object JavaDoc data)
234    {
235       return data;
236    }
237
238    public Object JavaDoc visit(ASTParameter node, Object JavaDoc data)
239    {
240       return data;
241    }
242
243    public Object JavaDoc visit(ASTAllParameter node, Object JavaDoc data)
244    {
245       return data;
246    }
247
248    public Object JavaDoc visit(ASTCFlowExpression node, Object JavaDoc data)
249    {
250       return data;
251    }
252
253    public Object JavaDoc visit(ASTCFlowBoolean node, Object JavaDoc data)
254    {
255       return data;
256    }
257
258    public Object JavaDoc visit(ASTNotCFlow node, Object JavaDoc data)
259    {
260       return data;
261    }
262
263    public Object JavaDoc visit(ASTCompositeCFlow node, Object JavaDoc data)
264    {
265       return data;
266    }
267
268    public Object JavaDoc visit(ASTSubCFlow node, Object JavaDoc data)
269    {
270       return data;
271    }
272
273    public Object JavaDoc visit(ASTAndCFlow node, Object JavaDoc data)
274    {
275       return data;
276    }
277
278    public Object JavaDoc visit(ASTOrCFlow node, Object JavaDoc data)
279    {
280       return data;
281    }
282
283    public Object JavaDoc visit(ASTCFlow node, Object JavaDoc data)
284    {
285       return data;
286    }
287
288    public Object JavaDoc visit(SimpleNode node, Object JavaDoc data)
289    {
290       return data;
291    }
292
293 }
294
Popular Tags