001: /**************************************************************************************
002: * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.cflow;
008:
009: import org.codehaus.aspectwerkz.expression.ast.ExpressionParserVisitor;
010: import org.codehaus.aspectwerkz.expression.ast.SimpleNode;
011: import org.codehaus.aspectwerkz.expression.ast.ASTRoot;
012: import org.codehaus.aspectwerkz.expression.ast.ASTExpression;
013: import org.codehaus.aspectwerkz.expression.ast.ASTAnd;
014: import org.codehaus.aspectwerkz.expression.ast.ASTOr;
015: import org.codehaus.aspectwerkz.expression.ast.ASTNot;
016: import org.codehaus.aspectwerkz.expression.ast.ASTPointcutReference;
017: import org.codehaus.aspectwerkz.expression.ast.ASTExecution;
018: import org.codehaus.aspectwerkz.expression.ast.ASTCall;
019: import org.codehaus.aspectwerkz.expression.ast.ASTSet;
020: import org.codehaus.aspectwerkz.expression.ast.ASTGet;
021: import org.codehaus.aspectwerkz.expression.ast.ASTHandler;
022: import org.codehaus.aspectwerkz.expression.ast.ASTWithin;
023: import org.codehaus.aspectwerkz.expression.ast.ASTWithinCode;
024: import org.codehaus.aspectwerkz.expression.ast.ASTStaticInitialization;
025: import org.codehaus.aspectwerkz.expression.ast.ASTCflow;
026: import org.codehaus.aspectwerkz.expression.ast.ASTCflowBelow;
027: import org.codehaus.aspectwerkz.expression.ast.ASTArgs;
028: import org.codehaus.aspectwerkz.expression.ast.ASTHasMethod;
029: import org.codehaus.aspectwerkz.expression.ast.ASTHasField;
030: import org.codehaus.aspectwerkz.expression.ast.ASTTarget;
031: import org.codehaus.aspectwerkz.expression.ast.ASTThis;
032: import org.codehaus.aspectwerkz.expression.ast.ASTClassPattern;
033: import org.codehaus.aspectwerkz.expression.ast.ASTMethodPattern;
034: import org.codehaus.aspectwerkz.expression.ast.ASTConstructorPattern;
035: import org.codehaus.aspectwerkz.expression.ast.ASTFieldPattern;
036: import org.codehaus.aspectwerkz.expression.ast.ASTParameter;
037: import org.codehaus.aspectwerkz.expression.ast.ASTArgParameter;
038: import org.codehaus.aspectwerkz.expression.ast.ASTAttribute;
039: import org.codehaus.aspectwerkz.expression.ast.ASTModifier;
040: import org.codehaus.aspectwerkz.expression.ast.Node;
041: import org.codehaus.aspectwerkz.expression.ExpressionNamespace;
042: import org.codehaus.aspectwerkz.expression.ExpressionInfo;
043:
044: import java.util.List;
045:
046: /**
047: * A visitor to create the bindings between cflow aspect and cflow subexpression.
048: * For each visited cflow / cflowbelow node, one CflowBinding is created
049: * with the cflow(below) subexpression as expressionInfo.
050: *
051: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
052: */
053: public class CflowAspectExpressionVisitor implements
054: ExpressionParserVisitor {
055:
056: private ExpressionInfo m_expressionInfo;
057: private Node m_root;
058: private String m_namespace;
059:
060: public CflowAspectExpressionVisitor(ExpressionInfo expressionInfo,
061: Node root, String namespace) {
062: m_expressionInfo = expressionInfo;
063: m_root = root;
064: m_namespace = namespace;
065: }
066:
067: /**
068: * Visit the expression and populate the list with CflowBinding for each cflow() or cflowbelow()
069: * subexpression encountered (including thru pointcut references)
070: *
071: * @param bindings
072: * @return the list of bindings
073: */
074: public List populateCflowAspectBindings(List bindings) {
075: visit(m_root, bindings);
076: return bindings;
077: }
078:
079: public Object visit(Node node, Object data) {
080: return node.jjtGetChild(0).jjtAccept(this , data);
081: }
082:
083: public Object visit(SimpleNode node, Object data) {
084: return node.jjtGetChild(0).jjtAccept(this , data);
085: }
086:
087: public Object visit(ASTRoot node, Object data) {
088: return node.jjtGetChild(0).jjtAccept(this , data);
089: }
090:
091: public Object visit(ASTExpression node, Object data) {
092: return node.jjtGetChild(0).jjtAccept(this , data);
093: }
094:
095: public Object visit(ASTAnd node, Object data) {
096: // the AND and OR can have more than 2 nodes [see jjt grammar]
097: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
098: node.jjtGetChild(i).jjtAccept(this , data);
099: }
100: return data;
101: }
102:
103: public Object visit(ASTOr node, Object data) {
104: // the AND and OR can have more than 2 nodes [see jjt grammar]
105: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
106: node.jjtGetChild(i).jjtAccept(this , data);
107: }
108: return data;
109: }
110:
111: public Object visit(ASTNot node, Object data) {
112: return node.jjtGetChild(0).jjtAccept(this , data);
113: }
114:
115: /**
116: * Resolve pointcut references
117: *
118: * @param node
119: * @param data
120: * @return
121: */
122: public Object visit(ASTPointcutReference node, Object data) {
123: ExpressionNamespace namespace = ExpressionNamespace
124: .getNamespace(m_namespace);
125: CflowAspectExpressionVisitor expression = namespace
126: .getExpressionInfo(node.getName())
127: .getCflowAspectExpression();
128: return expression.populateCflowAspectBindings((List) data);
129: }
130:
131: public Object visit(ASTExecution node, Object data) {
132: return data;
133: }
134:
135: public Object visit(ASTCall node, Object data) {
136: return data;
137: }
138:
139: public Object visit(ASTSet node, Object data) {
140: return data;
141: }
142:
143: public Object visit(ASTGet node, Object data) {
144: return data;
145: }
146:
147: public Object visit(ASTHandler node, Object data) {
148: return data;
149: }
150:
151: public Object visit(ASTWithin node, Object data) {
152: return data;
153: }
154:
155: public Object visit(ASTWithinCode node, Object data) {
156: return data;
157: }
158:
159: public Object visit(ASTStaticInitialization node, Object data) {
160: return data;
161: }
162:
163: /**
164: * build a cflow binding with the cflow sub expression
165: *
166: * @param node
167: * @param data
168: * @return
169: */
170: public Object visit(ASTCflow node, Object data) {
171: int cflowID = node.hashCode();
172: Node subNode = node.jjtGetChild(0);
173: ExpressionInfo subExpression = new ExpressionInfo(subNode,
174: m_namespace);
175: subExpression.inheritPossibleArgumentFrom(m_expressionInfo);
176: ((List) data).add(new CflowBinding(cflowID, subExpression,
177: m_expressionInfo, false));
178: return data;
179: }
180:
181: /**
182: * build a cflowbelow binding with the cflowbelow sub expression
183: *
184: * @param node
185: * @param data
186: * @return
187: */
188: public Object visit(ASTCflowBelow node, Object data) {
189: int cflowID = node.hashCode();
190: Node subNode = node.jjtGetChild(0);
191: ExpressionInfo subExpression = new ExpressionInfo(subNode,
192: m_namespace);
193: subExpression.inheritPossibleArgumentFrom(m_expressionInfo);
194: ((List) data).add(new CflowBinding(cflowID, subExpression,
195: m_expressionInfo, true));
196: return data;
197: }
198:
199: public Object visit(ASTArgs node, Object data) {
200: return data;
201: }
202:
203: public Object visit(ASTHasMethod node, Object data) {
204: return data;
205: }
206:
207: public Object visit(ASTHasField node, Object data) {
208: return data;
209: }
210:
211: public Object visit(ASTTarget node, Object data) {
212: return data;
213: }
214:
215: public Object visit(ASTThis node, Object data) {
216: return data;
217: }
218:
219: public Object visit(ASTClassPattern node, Object data) {
220: throw new UnsupportedOperationException("Should not be reached");
221: }
222:
223: public Object visit(ASTMethodPattern node, Object data) {
224: throw new UnsupportedOperationException("Should not be reached");
225: }
226:
227: public Object visit(ASTConstructorPattern node, Object data) {
228: throw new UnsupportedOperationException("Should not be reached");
229: }
230:
231: public Object visit(ASTFieldPattern node, Object data) {
232: throw new UnsupportedOperationException("Should not be reached");
233: }
234:
235: public Object visit(ASTParameter node, Object data) {
236: throw new UnsupportedOperationException("Should not be reached");
237: }
238:
239: public Object visit(ASTArgParameter node, Object data) {
240: throw new UnsupportedOperationException("Should not be reached");
241: }
242:
243: public Object visit(ASTAttribute node, Object data) {
244: throw new UnsupportedOperationException("Should not be reached");
245: }
246:
247: public Object visit(ASTModifier node, Object data) {
248: throw new UnsupportedOperationException("Should not be reached");
249: }
250: }
|