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