KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > ExpressionContext


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression;
5
6 import com.tc.aspectwerkz.reflect.ReflectionInfo;
7 import com.tc.aspectwerkz.reflect.MethodInfo;
8 import com.tc.aspectwerkz.reflect.ConstructorInfo;
9 import com.tc.aspectwerkz.reflect.StaticInitializationInfo;
10 import com.tc.aspectwerkz.reflect.ClassInfo;
11 import com.tc.aspectwerkz.reflect.FieldInfo;
12
13 import java.util.HashMap JavaDoc;
14
15 /**
16  * The expression context for AST evaluation.
17  *
18  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
20  */

21 public class ExpressionContext {
22   public static final int INFO_NOT_AVAILABLE = -1;
23
24   public static final int METHOD_INFO = 0;
25
26   public static final int CONSTRUCTOR_INFO = 1;
27
28   public static final int FIELD_INFO = 2;
29
30   public static final int CLASS_INFO = 3;
31
32   private static final int STATIC_INFO = 4;
33
34   private final int m_reflectionInfoType;
35
36   private final PointcutType m_pointcutType;
37
38   private final ReflectionInfo m_matchingReflectionInfo;
39
40   private final ReflectionInfo m_withinReflectionInfo;
41
42   private boolean m_inCflowSubAST = false;
43
44   private boolean m_cflowEvaluation = false;
45
46   private boolean m_hasBeenVisitingCflow = false;
47
48   private int m_currentTargetArgsIndex = 0;
49
50   /**
51    * Expression to advised target (method / ctor) argument index map.
52    * It depends on the matching context and the pointcut signature, as well as args(..)
53    *
54    * Value of this field is cloned, so had to use HashMap type.
55    */

56   public HashMap JavaDoc m_exprIndexToTargetIndex = new HashMap JavaDoc();
57
58   /**
59    * The variable name corresponding to the this(..) designator,
60    * or null if nothing is bound (this(<type>) or no this(..))
61    */

62   public String JavaDoc m_thisBoundedName = null;
63
64   /**
65    * The variable name corresponding to the target(..) designator,
66    * or null if nothing is bound (target(<type>) or no target(..))
67    */

68   public String JavaDoc m_targetBoundedName = null;
69
70   /**
71    * Set to true when we encounter a poincut using target(..) and when match cannot be done without a
72    * runtime check with instance of.
73    */

74   public boolean m_targetWithRuntimeCheck = false;
75
76   /**
77    * Creates a new expression context.
78    *
79    * @param pointcutType
80    * @param reflectionInfo - can be null f.e. with early evaluation of CALL pointcut
81    * @param withinReflectionInfo
82    */

83   public ExpressionContext(final PointcutType pointcutType,
84                            final ReflectionInfo reflectionInfo,
85                            final ReflectionInfo withinReflectionInfo) {
86     if (pointcutType == null) {
87       throw new IllegalArgumentException JavaDoc("pointcut type can not be null");
88     }
89     m_pointcutType = pointcutType;
90     m_matchingReflectionInfo = reflectionInfo;
91     if (withinReflectionInfo != null) {
92       m_withinReflectionInfo = withinReflectionInfo;
93     } else {
94       if (PointcutType.EXECUTION.equals(pointcutType)
95               || PointcutType.STATIC_INITIALIZATION.equals(pointcutType)
96               || PointcutType.WITHIN.equals(pointcutType)) {
97         m_withinReflectionInfo = m_matchingReflectionInfo;
98       } else {
99         m_withinReflectionInfo = null;
100       }
101     }
102     if (reflectionInfo instanceof MethodInfo) {
103       m_reflectionInfoType = METHOD_INFO;
104     } else if (reflectionInfo instanceof ConstructorInfo) {
105       m_reflectionInfoType = CONSTRUCTOR_INFO;
106     } else if (reflectionInfo instanceof FieldInfo) {
107       m_reflectionInfoType = FIELD_INFO;
108     } else if (reflectionInfo instanceof ClassInfo) {
109       m_reflectionInfoType = CLASS_INFO;
110     } else if (reflectionInfo instanceof StaticInitializationInfo) {
111       m_reflectionInfoType = STATIC_INFO;
112     } else {
113       m_reflectionInfoType = INFO_NOT_AVAILABLE;// used for early eval on CALL
114
}
115   }
116
117   public ReflectionInfo getReflectionInfo() {
118     return m_matchingReflectionInfo;
119   }
120
121   public ReflectionInfo getWithinReflectionInfo() {
122     return m_withinReflectionInfo;
123   }
124
125   public boolean hasExecutionPointcut() {
126     return m_pointcutType.equals(PointcutType.EXECUTION);
127   }
128
129   public boolean hasCallPointcut() {
130     return m_pointcutType.equals(PointcutType.CALL);
131   }
132
133   public boolean hasSetPointcut() {
134     return m_pointcutType.equals(PointcutType.SET);
135   }
136
137   public boolean hasGetPointcut() {
138     return m_pointcutType.equals(PointcutType.GET);
139   }
140
141   public boolean hasHandlerPointcut() {
142     return m_pointcutType.equals(PointcutType.HANDLER);
143   }
144
145   public boolean hasStaticInitializationPointcut() {
146     return m_pointcutType.equals(PointcutType.STATIC_INITIALIZATION);
147   }
148
149   public boolean hasWithinPointcut() {
150     return m_pointcutType.equals(PointcutType.WITHIN);
151   }
152 //
153
// public boolean hasHasMethodPointcut() {
154
// return m_pointcutType.equals(PointcutType.HAS_METHOD);
155
// }
156
//
157
// public boolean hasHasFieldPointcut() {
158
// return m_pointcutType.equals(PointcutType.HAS_FIELD);
159
// }
160

161   public boolean hasWithinReflectionInfo() {
162     return m_withinReflectionInfo != null;
163   }
164
165   public boolean hasMethodInfo() {
166     return m_reflectionInfoType == METHOD_INFO;
167   }
168
169   public boolean hasConstructorInfo() {
170     return m_reflectionInfoType == CONSTRUCTOR_INFO;
171   }
172
173   public boolean hasFieldInfo() {
174     return m_reflectionInfoType == FIELD_INFO;
175   }
176
177   public boolean hasClassInfo() {
178     return m_reflectionInfoType == CLASS_INFO;
179   }
180
181   public boolean hasReflectionInfo() {
182     return m_reflectionInfoType != INFO_NOT_AVAILABLE;
183   }
184
185   public void setInCflowSubAST(final boolean inCflowAST) {
186     m_inCflowSubAST = inCflowAST;
187   }
188
189   public boolean inCflowSubAST() {
190     return m_inCflowSubAST;
191   }
192
193   public void setHasBeenVisitingCflow(final boolean hasBeenVisitingCflow) {
194     m_hasBeenVisitingCflow = hasBeenVisitingCflow;
195   }
196
197   public boolean hasBeenVisitingCflow() {
198     return m_hasBeenVisitingCflow;
199   }
200
201   public boolean getCflowEvaluation() {
202     return m_cflowEvaluation;
203   }
204
205   public void setCflowEvaluation(boolean cflowEvaluation) {
206     m_cflowEvaluation = cflowEvaluation;
207   }
208
209   public int getCurrentTargetArgsIndex() {
210     return m_currentTargetArgsIndex;
211   }
212
213   public void setCurrentTargetArgsIndex(int argsIndex) {
214     this.m_currentTargetArgsIndex = argsIndex;
215   }
216
217   public boolean equals(Object JavaDoc o) {
218     if (this == o) {
219       return true;
220     }
221     if (!(o instanceof ExpressionContext)) {
222       return false;
223     }
224     final ExpressionContext expressionContext = (ExpressionContext) o;
225     if (m_reflectionInfoType != expressionContext.m_reflectionInfoType) {
226       return false;
227     }
228     if (!m_matchingReflectionInfo.equals(expressionContext.m_matchingReflectionInfo)) {
229       return false;
230     }
231     if (!m_pointcutType.equals(expressionContext.m_pointcutType)) {
232       return false;
233     }
234     if ((m_withinReflectionInfo != null) ?
235             (!m_withinReflectionInfo
236                     .equals(expressionContext.m_withinReflectionInfo)) :
237             (expressionContext.m_withinReflectionInfo != null)) {
238       return false;
239     }
240     return true;
241   }
242
243   public int hashCode() {
244     int result;
245     result = m_pointcutType.hashCode();
246     result = (29 * result) + m_matchingReflectionInfo.hashCode();
247     result = (29 * result) + ((m_withinReflectionInfo != null) ? m_withinReflectionInfo.hashCode() : 0);
248     result = (29 * result) + m_reflectionInfoType;
249     return result;
250   }
251
252   public PointcutType getPointcutType() {
253     return m_pointcutType;
254   }
255
256   public void resetRuntimeState() {
257     m_targetBoundedName = null;
258     m_thisBoundedName = null;
259     m_exprIndexToTargetIndex = new HashMap JavaDoc();
260     m_targetWithRuntimeCheck = false;
261   }
262
263   public String JavaDoc getDebugString() {
264         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
265         buffer.append("[ExpressionContext:");
266         buffer.append(" INFO_NOT_AVAILABLE: ");
267         buffer.append(INFO_NOT_AVAILABLE);
268         buffer.append(" METHOD_INFO: ");
269         buffer.append(METHOD_INFO);
270         buffer.append(" CONSTRUCTOR_INFO: ");
271         buffer.append(CONSTRUCTOR_INFO);
272         buffer.append(" FIELD_INFO: ");
273         buffer.append(FIELD_INFO);
274         buffer.append(" CLASS_INFO: ");
275         buffer.append(CLASS_INFO);
276         buffer.append(" STATIC_INFO: ");
277         buffer.append(STATIC_INFO);
278         buffer.append(" m_reflectionInfoType: ");
279         buffer.append(m_reflectionInfoType);
280         buffer.append(" m_pointcutType: ");
281         buffer.append(m_pointcutType);
282         buffer.append(" m_matchingReflectionInfo: ");
283         buffer.append(m_matchingReflectionInfo);
284         buffer.append(" m_withinReflectionInfo: ");
285         buffer.append(m_withinReflectionInfo);
286         buffer.append(" m_inCflowSubAST: ");
287         buffer.append(m_inCflowSubAST);
288         buffer.append(" m_cflowEvaluation: ");
289         buffer.append(m_cflowEvaluation);
290         buffer.append(" m_hasBeenVisitingCflow: ");
291         buffer.append(m_hasBeenVisitingCflow);
292         buffer.append(" m_currentTargetArgsIndex: ");
293         buffer.append(m_currentTargetArgsIndex);
294         buffer.append(" m_exprIndexToTargetIndex: ");
295         buffer.append(m_exprIndexToTargetIndex);
296         buffer.append(" m_thisBoundedName: ");
297         buffer.append(m_thisBoundedName);
298         buffer.append(" m_targetBoundedName: ");
299         buffer.append(m_targetBoundedName);
300         buffer.append(" m_targetWithRuntimeCheck: ");
301         buffer.append(m_targetWithRuntimeCheck);
302         buffer.append("]");
303         return buffer.toString();
304     }
305
306
307 }
Popular Tags