KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Field JavaDoc;
25 import org.jboss.aop.Advisor;
26 import org.jboss.aop.pointcut.ast.ASTAll;
27 import org.jboss.aop.pointcut.ast.ASTAttribute;
28 import org.jboss.aop.pointcut.ast.ASTConstructor;
29 import org.jboss.aop.pointcut.ast.ASTField;
30 import org.jboss.aop.pointcut.ast.ASTFieldExecution;
31 import org.jboss.aop.pointcut.ast.ASTHas;
32 import org.jboss.aop.pointcut.ast.ASTHasField;
33 import org.jboss.aop.pointcut.ast.ASTMethod;
34 import org.jboss.aop.pointcut.ast.ASTStart;
35 import org.jboss.aop.pointcut.ast.ClassExpression;
36 import org.jboss.aop.pointcut.ast.Node;
37 import javassist.CtField;
38 import javassist.NotFoundException;
39
40 /**
41  * Comment
42  *
43  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
44  * @version $Revision: 44925 $
45  */

46 public class FieldMatcher extends MatcherHelper
47 {
48    protected Advisor advisor;
49    protected String JavaDoc classname;
50    protected String JavaDoc fieldName;
51    protected int fieldModifiers;
52    protected CtField ctField;
53    protected Field JavaDoc refField;
54
55    public FieldMatcher(Advisor advisor, CtField field, ASTStart start) throws NotFoundException
56    {
57       super(start, advisor.getManager());
58       this.advisor = advisor;
59       this.classname = field.getDeclaringClass().getName();
60       this.start = start;
61       this.fieldName = field.getName();
62       this.fieldModifiers = field.getModifiers();
63       this.ctField = field;
64    }
65
66    public FieldMatcher(Advisor advisor, Field JavaDoc field, ASTStart start)
67    {
68       super(start, advisor.getManager());
69       this.advisor = advisor;
70       this.classname = field.getDeclaringClass().getName();
71       this.start = start;
72       this.fieldName = field.getName();
73       this.fieldModifiers = field.getModifiers();
74       this.refField = field;
75    }
76
77    protected Boolean JavaDoc resolvePointcut(Pointcut p)
78    {
79       throw new RuntimeException JavaDoc("SHOULD NOT BE CALLED");
80    }
81
82    public Object JavaDoc visit(ASTField node, Object JavaDoc data)
83    {
84
85       if (node.getAttributes().size() > 0)
86       {
87          for (int i = 0; i < node.getAttributes().size(); i++)
88          {
89             ASTAttribute attr = (ASTAttribute) node.getAttributes().get(i);
90             if (!Util.matchModifiers(attr, fieldModifiers))
91             {
92                return Boolean.FALSE;
93             }
94          }
95       }
96       
97       try
98       {
99          ClassExpression type = node.getType();
100          if (ctField != null)
101          {
102             if (!Util.matchesClassExpr(type, ctField.getType(), advisor)) return Boolean.FALSE;
103             if (!Util.matchesClassExpr(node.getClazz(), ctField.getDeclaringClass(), advisor)) return Boolean.FALSE;
104          }
105          else
106          {
107             if (!Util.matchesClassExpr(type, refField.getType(), advisor)) return Boolean.FALSE;
108             if (!Util.matchesClassExpr(node.getClazz(), refField.getDeclaringClass(), advisor)) return Boolean.FALSE;
109          }
110       }
111       catch (NotFoundException nfe)
112       {
113          throw new RuntimeException JavaDoc(nfe);
114       }
115       
116       if (node.getFieldIdentifier().isAnnotation())
117       {
118          String JavaDoc sub = node.getFieldIdentifier().getOriginal().substring(1);
119          if (advisor.getFieldMetaData().hasTag(fieldName, sub) || advisor.getDefaultMetaData().hasTag(sub))
120          {
121             return Boolean.TRUE;
122          }
123          try
124          {
125             if (ctField != null)
126             {
127                if (advisor.hasAnnotation(ctField, sub))
128                {
129                   return Boolean.TRUE;
130                }
131             }
132             else
133             {
134                if (advisor.hasAnnotation(refField, sub))
135                {
136                   return Boolean.TRUE;
137                }
138             }
139          }
140          catch (Exception JavaDoc e)
141          {
142             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
143
}
144       }
145       else
146       {
147          if (node.getFieldIdentifier().matches(fieldName))
148          {
149             return Boolean.TRUE;
150          }
151       }
152       return Boolean.FALSE;
153    }
154
155    public Object JavaDoc visit(ASTFieldExecution node, Object JavaDoc data)
156    {
157       return node.jjtGetChild(0).jjtAccept(this, null);
158    }
159
160    public Object JavaDoc visit(ASTAll node, Object JavaDoc data)
161    {
162       if (node.getClazz().isAnnotation())
163       {
164          String JavaDoc sub = node.getClazz().getOriginal().substring(1);
165          if (!advisor.getFieldMetaData().hasTag(fieldName, sub))
166          {
167             if (!advisor.getDefaultMetaData().hasTag(sub))
168             {
169                if (ctField != null)
170                {
171                   if (!advisor.hasAnnotation(ctField, sub)) return Boolean.FALSE;
172                }
173                else
174                {
175                   try
176                   {
177                      if (!advisor.hasAnnotation(refField, sub)) return Boolean.FALSE;
178                   }
179                   catch (Exception JavaDoc e)
180                   {
181                      throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
182
}
183                }
184             }
185          }
186       }
187       else if (node.getClazz().isInstanceOf())
188       {
189          if (ctField != null)
190          {
191             if (!Util.subtypeOf(ctField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE;
192          }
193          else if (!Util.subtypeOf(refField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE;
194
195       }
196       else if (node.getClazz().isTypedef())
197       {
198          if (ctField != null)
199          {
200             try
201             {
202                if (!Util.matchesTypedef(ctField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE;
203             }
204             catch (Exception JavaDoc e)
205             {
206                throw new RuntimeException JavaDoc(e);
207             }
208          }
209          else if (!Util.matchesTypedef(refField.getDeclaringClass(), node.getClazz(), advisor)) return Boolean.FALSE;
210       }
211       else if (!node.getClazz().matches(classname))
212       {
213          return Boolean.FALSE;
214       }
215
216       return Boolean.TRUE;
217    }
218
219    public Object JavaDoc visit(ASTHasField node, Object JavaDoc data)
220    {
221       ASTField f = (ASTField) node.jjtGetChild(0);
222       if (ctField != null)
223          return new Boolean JavaDoc(Util.has(ctField.getDeclaringClass(), f, advisor));
224       else
225          return new Boolean JavaDoc(Util.has(refField.getDeclaringClass(), f, advisor));
226    }
227
228    public Object JavaDoc visit(ASTHas node, Object JavaDoc data)
229    {
230       Node n = node.jjtGetChild(0);
231       if (n instanceof ASTMethod)
232       {
233          if (ctField != null)
234          {
235             return new Boolean JavaDoc(Util.has(ctField.getDeclaringClass(), (ASTMethod) n, advisor));
236          }
237          else
238          {
239             return new Boolean JavaDoc(Util.has(refField.getDeclaringClass(), (ASTMethod) n, advisor));
240
241          }
242       }
243       else
244       {
245          if (refField != null)
246          {
247             return new Boolean JavaDoc(Util.has(ctField.getDeclaringClass(), (ASTConstructor) n, advisor));
248          }
249          else
250          {
251             return new Boolean JavaDoc(Util.has(refField.getDeclaringClass(), (ASTConstructor) n, advisor));
252
253          }
254       }
255    }
256 }
257
Popular Tags