KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringReader JavaDoc;
25
26 import javassist.CtClass;
27 import javassist.NotFoundException;
28 import javassist.expr.MethodCall;
29 import javassist.expr.NewExpr;
30
31 import org.jboss.aop.Advisor;
32 import org.jboss.aop.pointcut.ast.ASTStart;
33 import org.jboss.aop.pointcut.ast.ParseException;
34 import org.jboss.aop.pointcut.ast.PointcutExpressionParser;
35 import org.jboss.aop.pointcut.ast.TypeExpressionParser;
36
37 /**
38  *
39  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
40  * @version $Revision: 37406 $
41  */

42 public class DeclareDef
43 {
44    String JavaDoc name;
45    String JavaDoc expr;
46    boolean warning;
47    String JavaDoc msg;
48    ASTStart ast;
49    boolean pointcut;
50    
51    public DeclareDef(String JavaDoc name, String JavaDoc expr, boolean warning, String JavaDoc msg) throws ParseException
52    {
53       this.name = name;
54       this.expr = expr;
55       this.warning = warning;
56       this.msg = msg;
57       
58       try
59       {
60          ast = new PointcutExpressionParser(new StringReader JavaDoc(expr)).Start();
61          pointcut = true;
62       }
63       catch (ParseException pe)
64       {
65          try
66          {
67             ast = new TypeExpressionParser(new StringReader JavaDoc(expr)).Start();
68          }
69          catch (ParseException te)
70          {
71             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("The expression '" +
72                   expr + "' resolves to neither a pointcut nor a type expression");
73             sb.append("\n\nPointcut parse error:\n");
74             sb.append(pe.getMessage());
75             sb.append("\n\nType expression parse error:\n");
76             sb.append(te.getMessage());
77             throw new ParseException(sb.toString());
78          }
79       }
80    }
81    
82
83    public ASTStart getAst()
84    {
85       return ast;
86    }
87    
88    public String JavaDoc getExpr()
89    {
90       return expr;
91    }
92    
93    public String JavaDoc getName()
94    {
95       return name;
96    }
97    
98    public boolean getWarning()
99    {
100       return warning;
101    }
102    
103    public String JavaDoc getMsg()
104    {
105       return msg;
106    }
107    
108    /**
109     * @return true if a pointcut expression, false if a type expression
110     */

111    public boolean isPointcut()
112    {
113       return pointcut;
114    }
115    
116    public boolean matches(Advisor advisor, CtClass clazz)
117    {
118       if (pointcut)return false;
119       DeclareTypeMatcher matcher = new DeclareTypeMatcher(advisor, clazz);
120       return ((Boolean JavaDoc) ast.jjtAccept(matcher, null)).booleanValue();
121    }
122
123    public boolean matches(Advisor advisor, Class JavaDoc clazz)
124    {
125       if (pointcut)return false;
126       DeclareTypeMatcher matcher = new DeclareTypeMatcher(advisor, clazz);
127       return ((Boolean JavaDoc) ast.jjtAccept(matcher, null)).booleanValue();
128    }
129
130    public boolean matchesCall(Advisor callingAdvisor, MethodCall methodCall) throws NotFoundException
131    {
132       if (!pointcut)return false;
133       MethodCallMatcher matcher = new MethodCallMatcher(callingAdvisor, methodCall, ast);
134       return matcher.matches();
135    }
136
137    public boolean matchesCall(Advisor callingAdvisor, NewExpr methodCall) throws NotFoundException
138    {
139       if (!pointcut)return false;
140       NewExprMatcher matcher = new NewExprMatcher(callingAdvisor, methodCall, ast);
141       return matcher.matches();
142    }
143
144    
145 }
146
Popular Tags