KickJava   Java API By Example, From Geeks To Geeks.

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


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.CtConstructor;
25 import javassist.NotFoundException;
26 import javassist.expr.NewExpr;
27 import org.jboss.aop.Advisor;
28 import org.jboss.aop.AspectManager;
29 import org.jboss.aop.ClassAdvisor;
30 import org.jboss.aop.pointcut.ast.ASTCall;
31 import org.jboss.aop.pointcut.ast.ASTConstructor;
32 import org.jboss.aop.pointcut.ast.ASTField;
33 import org.jboss.aop.pointcut.ast.ASTHas;
34 import org.jboss.aop.pointcut.ast.ASTHasField;
35 import org.jboss.aop.pointcut.ast.ASTMethod;
36 import org.jboss.aop.pointcut.ast.ASTStart;
37 import org.jboss.aop.pointcut.ast.ASTWithin;
38 import org.jboss.aop.pointcut.ast.ASTWithincode;
39 import org.jboss.aop.pointcut.ast.Node;
40
41 /**
42  * Comment
43  *
44  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
45  * @version $Revision: 37406 $
46  */

47 public class NewExprMatcher extends MatcherHelper
48 {
49    NewExpr call;
50    Advisor advisor;
51
52    public NewExprMatcher(Advisor advisor, NewExpr call, ASTStart start) throws NotFoundException
53    {
54       super(start, advisor.getManager());
55       this.advisor = advisor;
56       this.call = call;
57    }
58
59    public Object JavaDoc visit(ASTCall node, Object JavaDoc data)
60    {
61       try
62       {
63          if (!(node.getBehavior() instanceof ASTConstructor)) return Boolean.FALSE;
64          ASTConstructor astCon = (ASTConstructor) node.getBehavior();
65
66          // do simple checks to avoid loading CtClasses
67
if (astCon.getClazz().isSimple())
68          {
69             if (!astCon.getClazz().matches(call.getClassName())) return Boolean.FALSE;
70          }
71          CtConstructor calledCon = call.getConstructor();
72          ClassAdvisor calledAdvisor = AspectManager.instance().getTempClassAdvisor(calledCon.getDeclaringClass());
73          ConstructorMatcher conMatcher = new ConstructorMatcher(calledAdvisor, calledCon, null);
74          return conMatcher.matches(astCon);
75       }
76       catch (Exception JavaDoc e)
77       {
78          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
79
}
80    }
81
82    public Object JavaDoc visit(ASTWithin node, Object JavaDoc data)
83    {
84       WithinMatcher within = null;
85       try
86       {
87          within = new WithinMatcher(advisor, call.where(), null);
88       }
89       catch (NotFoundException e)
90       {
91          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
92
}
93       return node.jjtAccept(within, null);
94    }
95
96    public Object JavaDoc visit(ASTWithincode node, Object JavaDoc data)
97    {
98       WithinMatcher within = null;
99       try
100       {
101          within = new WithinMatcher(advisor, call.where(), null);
102       }
103       catch (NotFoundException e)
104       {
105          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
106
}
107       return node.jjtAccept(within, null);
108    }
109
110    protected Boolean JavaDoc resolvePointcut(Pointcut p)
111    {
112       try
113       {
114          return new Boolean JavaDoc(p.matchesCall(advisor, call));
115       }
116       catch (NotFoundException e)
117       {
118          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
119
}
120    }
121
122    public Object JavaDoc visit(ASTHas node, Object JavaDoc data)
123    {
124       try
125       {
126          Node n = node.jjtGetChild(0);
127          CtConstructor con = call.getConstructor();
128          if (n instanceof ASTMethod)
129          {
130             return new Boolean JavaDoc(Util.has(con.getDeclaringClass(), (ASTMethod) n, advisor));
131          }
132          else
133          {
134             return new Boolean JavaDoc(Util.has(con.getDeclaringClass(), (ASTConstructor) n, advisor));
135          }
136       }
137       catch (NotFoundException e)
138       {
139          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
140
}
141    }
142
143    public Object JavaDoc visit(ASTHasField node, Object JavaDoc data)
144    {
145       ASTField f = (ASTField) node.jjtGetChild(0);
146       CtConstructor con = null;
147       try
148       {
149          con = call.getConstructor();
150       }
151       catch (NotFoundException e)
152       {
153          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
154
}
155       return new Boolean JavaDoc(Util.has(con.getDeclaringClass(), f, advisor));
156    }
157 }
158
Popular Tags