KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constructor JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import org.jboss.aop.Advisor;
27 import org.jboss.aop.pointcut.ast.ASTAll;
28 import org.jboss.aop.pointcut.ast.ASTAttribute;
29 import org.jboss.aop.pointcut.ast.ASTConstructor;
30 import org.jboss.aop.pointcut.ast.ASTStart;
31
32 import javassist.CtConstructor;
33 import javassist.NotFoundException;
34
35 /**
36  * Comment
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 44925 $
40  */

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