1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.expression;
9
10 import org.codehaus.aspectwerkz.exception.DefinitionException;
11 import org.codehaus.aspectwerkz.expression.ast.ASTAnd;
12 import org.codehaus.aspectwerkz.expression.ast.ASTAttribute;
13 import org.codehaus.aspectwerkz.expression.ast.ASTCall;
14 import org.codehaus.aspectwerkz.expression.ast.ASTCflow;
15 import org.codehaus.aspectwerkz.expression.ast.ASTCflowBelow;
16 import org.codehaus.aspectwerkz.expression.ast.ASTClassPattern;
17 import org.codehaus.aspectwerkz.expression.ast.ASTConstructorPattern;
18 import org.codehaus.aspectwerkz.expression.ast.ASTExecution;
19 import org.codehaus.aspectwerkz.expression.ast.ASTExpression;
20 import org.codehaus.aspectwerkz.expression.ast.ASTFieldPattern;
21 import org.codehaus.aspectwerkz.expression.ast.ASTGet;
22 import org.codehaus.aspectwerkz.expression.ast.ASTHandler;
23 import org.codehaus.aspectwerkz.expression.ast.ASTMethodPattern;
24 import org.codehaus.aspectwerkz.expression.ast.ASTModifier;
25 import org.codehaus.aspectwerkz.expression.ast.ASTNot;
26 import org.codehaus.aspectwerkz.expression.ast.ASTOr;
27 import org.codehaus.aspectwerkz.expression.ast.ASTParameter;
28 import org.codehaus.aspectwerkz.expression.ast.ASTPointcutReference;
29 import org.codehaus.aspectwerkz.expression.ast.ASTRoot;
30 import org.codehaus.aspectwerkz.expression.ast.ASTSet;
31 import org.codehaus.aspectwerkz.expression.ast.ASTStaticInitialization;
32 import org.codehaus.aspectwerkz.expression.ast.ASTWithin;
33 import org.codehaus.aspectwerkz.expression.ast.ASTWithinCode;
34 import org.codehaus.aspectwerkz.expression.ast.ExpressionParserVisitor;
35 import org.codehaus.aspectwerkz.expression.ast.SimpleNode;
36 import org.codehaus.aspectwerkz.expression.ast.ASTArgs;
37 import org.codehaus.aspectwerkz.expression.ast.ASTArgParameter;
38 import org.codehaus.aspectwerkz.expression.ast.ASTHasField;
39 import org.codehaus.aspectwerkz.expression.ast.ASTHasMethod;
40
41 /***
42 * Checks if the expression has a cflow pointcut.
43 *
44 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
45 * @author Michael Nascimento
46 */
47 public class CflowPointcutFinderVisitor implements ExpressionParserVisitor {
48 protected final ASTRoot m_root;
49
50 protected final String m_expression;
51
52 protected final String m_namespace;
53
54 /***
55 * Creates a new finder.
56 *
57 * @param expression the expression as a string
58 * @param namespace the namespace
59 * @param root the AST root
60 */
61 public CflowPointcutFinderVisitor(final String expression, final String namespace, final ASTRoot root) {
62 m_root = root;
63 m_expression = expression;
64 m_namespace = namespace;
65 }
66
67 /***
68 * Checks if the expression has a cflow pointcut.
69 *
70 * @return
71 */
72 public boolean hasCflowPointcut() {
73 return ((Boolean) visit(m_root, null)).booleanValue();
74 }
75
76
77 public Object visit(SimpleNode node, Object data) {
78 return node.jjtGetChild(0).jjtAccept(this, data);
79 }
80
81 public Object visit(ASTRoot node, Object data) {
82 return node.jjtGetChild(0).jjtAccept(this, data);
83 }
84
85 public Object visit(ASTExpression node, Object data) {
86 return node.jjtGetChild(0).jjtAccept(this, data);
87 }
88
89
90 public Object visit(ASTOr node, Object data) {
91 int nrOfChildren = node.jjtGetNumChildren();
92 for (int i = 0; i < nrOfChildren; i++) {
93 if (((Boolean) node.jjtGetChild(i).jjtAccept(this, data)).booleanValue()) {
94 return Boolean.TRUE;
95 }
96 }
97 return Boolean.FALSE;
98 }
99
100 public Object visit(ASTAnd node, Object data) {
101 int nrOfChildren = node.jjtGetNumChildren();
102 for (int i = 0; i < nrOfChildren; i++) {
103 if (((Boolean) node.jjtGetChild(i).jjtAccept(this, data)).booleanValue()) {
104 return Boolean.TRUE;
105 }
106 }
107 return Boolean.FALSE;
108 }
109
110 public Object visit(ASTNot node, Object data) {
111 return node.jjtGetChild(0).jjtAccept(this, data);
112 }
113
114
115 public Object visit(ASTPointcutReference node, Object data) {
116 ExpressionNamespace namespace = ExpressionNamespace.getNamespace(m_namespace);
117 ExpressionInfo expressionInfo = namespace.getExpressionInfo(node.getName());
118 if (expressionInfo == null) {
119 throw new DefinitionException("Could not find pointcut reference " + node.getName() +
120 " in namespace " + m_namespace);
121 }
122 if (expressionInfo.hasCflowPointcut()) {
123 return Boolean.TRUE;
124 } else {
125 return Boolean.FALSE;
126 }
127 }
128
129 public Object visit(ASTExecution node, Object data) {
130 return Boolean.FALSE;
131 }
132
133 public Object visit(ASTCall node, Object data) {
134 return Boolean.FALSE;
135 }
136
137 public Object visit(ASTSet node, Object data) {
138 return Boolean.FALSE;
139 }
140
141 public Object visit(ASTGet node, Object data) {
142 return Boolean.FALSE;
143 }
144
145 public Object visit(ASTHandler node, Object data) {
146 return Boolean.FALSE;
147 }
148
149 public Object visit(ASTStaticInitialization node, Object data) {
150 return Boolean.FALSE;
151 }
152
153 public Object visit(ASTWithin node, Object data) {
154 return Boolean.FALSE;
155 }
156
157 public Object visit(ASTWithinCode node, Object data) {
158 return Boolean.FALSE;
159 }
160
161 public Object visit(ASTCflow node, Object data) {
162 return Boolean.TRUE;
163 }
164
165 public Object visit(ASTCflowBelow node, Object data) {
166 return Boolean.TRUE;
167 }
168
169 public Object visit(ASTArgs node, Object data) {
170 return Boolean.FALSE;
171 }
172
173 public Object visit(ASTHasMethod node, Object data) {
174 return Boolean.FALSE;
175 }
176
177 public Object visit(ASTHasField node, Object data) {
178 return Boolean.FALSE;
179 }
180
181
182 public Object visit(ASTClassPattern node, Object data) {
183 return Boolean.FALSE;
184 }
185
186 public Object visit(ASTMethodPattern node, Object data) {
187 return Boolean.FALSE;
188 }
189
190 public Object visit(ASTConstructorPattern node, Object data) {
191 return Boolean.FALSE;
192 }
193
194 public Object visit(ASTFieldPattern node, Object data) {
195 return Boolean.FALSE;
196 }
197
198 public Object visit(ASTParameter node, Object data) {
199 return Boolean.FALSE;
200 }
201
202 public Object visit(ASTArgParameter node, Object data) {
203 return Boolean.FALSE;
204 }
205
206 public Object visit(ASTAttribute node, Object data) {
207 return Boolean.FALSE;
208 }
209
210 public Object visit(ASTModifier node, Object data) {
211 return Boolean.FALSE;
212 }
213
214 /***
215 * Returns the string representation of the AST.
216 *
217 * @return
218 */
219 public String toString() {
220 return m_expression;
221 }
222
223 /***
224 * Returns the namespace.
225 *
226 * @return
227 */
228 public String getNamespace() {
229 return m_namespace;
230 }
231 }