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.reflect.ClassInfo;
11 import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
12 import org.codehaus.aspectwerkz.reflect.FieldInfo;
13 import org.codehaus.aspectwerkz.reflect.MethodInfo;
14 import org.codehaus.aspectwerkz.reflect.ReflectionInfo;
15
16 /***
17 * The expression context for AST evaluation.
18 *
19 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
20 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21 */
22 public class ExpressionContext {
23 public static final int NOTAVAILABLE_INFO = -1;
24
25 public static final int METHOD_INFO = 0;
26
27 public static final int CONSTRUCTOR_INFO = 1;
28
29 public static final int FIELD_INFO = 2;
30
31 public static final int CLASS_INFO = 3;
32
33 private final int m_reflectionInfoType;
34
35 private final PointcutType m_pointcutType;
36
37 private final ReflectionInfo m_matchingReflectionInfo;
38
39 private final ReflectionInfo m_withinReflectionInfo;
40
41 private boolean m_inCflowSubAST = false;
42
43 private boolean m_cflowEvaluation = false;
44
45 private boolean m_hasBeenVisitingCflow = false;
46
47 private int m_currentTartgetArgsIndex = 0;
48
49 public gnu.trove.TIntIntHashMap m_exprIndexToTargetIndex = new gnu.trove.TIntIntHashMap();
50
51 /***
52 * Creates a new expression context.
53 *
54 * @param pointcutType
55 * @param reflectionInfo - can be null f.e. with early evaluation of CALL pointcut
56 * @param withinReflectionInfo
57 */
58 public ExpressionContext(final PointcutType pointcutType,
59 final ReflectionInfo reflectionInfo,
60 final ReflectionInfo withinReflectionInfo) {
61 if (pointcutType == null) {
62 throw new IllegalArgumentException("pointcut type can not be null");
63 }
64 m_pointcutType = pointcutType;
65 m_matchingReflectionInfo = reflectionInfo;
66 if (withinReflectionInfo != null) {
67 m_withinReflectionInfo = withinReflectionInfo;
68 } else {
69
70 m_withinReflectionInfo = m_matchingReflectionInfo;
71 }
72 if (reflectionInfo instanceof MethodInfo) {
73 m_reflectionInfoType = METHOD_INFO;
74 } else if (reflectionInfo instanceof ConstructorInfo) {
75 m_reflectionInfoType = CONSTRUCTOR_INFO;
76 } else if (reflectionInfo instanceof FieldInfo) {
77 m_reflectionInfoType = FIELD_INFO;
78 } else if (reflectionInfo instanceof ClassInfo) {
79 m_reflectionInfoType = CLASS_INFO;
80 } else {
81 m_reflectionInfoType = NOTAVAILABLE_INFO;
82 }
83 }
84
85 public ReflectionInfo getReflectionInfo() {
86 return m_matchingReflectionInfo;
87 }
88
89 public ReflectionInfo getWithinReflectionInfo() {
90 return m_withinReflectionInfo;
91 }
92
93 public boolean hasExecutionPointcut() {
94 return m_pointcutType.equals(PointcutType.EXECUTION);
95 }
96
97 public boolean hasCallPointcut() {
98 return m_pointcutType.equals(PointcutType.CALL);
99 }
100
101 public boolean hasSetPointcut() {
102 return m_pointcutType.equals(PointcutType.SET);
103 }
104
105 public boolean hasGetPointcut() {
106 return m_pointcutType.equals(PointcutType.GET);
107 }
108
109 public boolean hasHandlerPointcut() {
110 return m_pointcutType.equals(PointcutType.HANDLER);
111 }
112
113 public boolean hasStaticInitializationPointcut() {
114 return m_pointcutType.equals(PointcutType.STATIC_INITIALIZATION);
115 }
116
117 public boolean hasWithinPointcut() {
118 return m_pointcutType.equals(PointcutType.WITHIN);
119 }
120
121
122
123
124
125
126
127
128
129 public boolean hasWithinReflectionInfo() {
130 return m_withinReflectionInfo != null;
131 }
132
133 public boolean hasMethodInfo() {
134 return m_reflectionInfoType == METHOD_INFO;
135 }
136
137 public boolean hasConstructorInfo() {
138 return m_reflectionInfoType == CONSTRUCTOR_INFO;
139 }
140
141 public boolean hasFieldInfo() {
142 return m_reflectionInfoType == FIELD_INFO;
143 }
144
145 public boolean hasClassInfo() {
146 return m_reflectionInfoType == CLASS_INFO;
147 }
148
149 public boolean hasReflectionInfo() {
150 return m_reflectionInfoType != NOTAVAILABLE_INFO;
151 }
152
153 public void setInCflowSubAST(final boolean inCflowAST) {
154 m_inCflowSubAST = inCflowAST;
155 }
156
157 public boolean inCflowSubAST() {
158 return m_inCflowSubAST;
159 }
160
161 public void setHasBeenVisitingCflow(final boolean hasBeenVisitingCflow) {
162 m_hasBeenVisitingCflow = hasBeenVisitingCflow;
163 }
164
165 public boolean hasBeenVisitingCflow() {
166 return m_hasBeenVisitingCflow;
167 }
168
169 public boolean getCflowEvaluation() {
170 return m_cflowEvaluation;
171 }
172
173 public void setCflowEvaluation(boolean cflowEvaluation) {
174 m_cflowEvaluation = cflowEvaluation;
175 }
176
177 public int getCurrentTargetArgsIndex() {
178 return m_currentTartgetArgsIndex;
179 }
180
181 public void setCurrentTargetArgsIndex(int argsIndex) {
182 this.m_currentTartgetArgsIndex = argsIndex;
183 }
184
185 public boolean equals(Object o) {
186 if (this == o) {
187 return true;
188 }
189 if (!(o instanceof ExpressionContext)) {
190 return false;
191 }
192 final ExpressionContext expressionContext = (ExpressionContext) o;
193 if (m_reflectionInfoType != expressionContext.m_reflectionInfoType) {
194 return false;
195 }
196 if (!m_matchingReflectionInfo.equals(expressionContext.m_matchingReflectionInfo)) {
197 return false;
198 }
199 if (!m_pointcutType.equals(expressionContext.m_pointcutType)) {
200 return false;
201 }
202 if ((m_withinReflectionInfo != null) ? (!m_withinReflectionInfo
203 .equals(expressionContext.m_withinReflectionInfo)) : (expressionContext.m_withinReflectionInfo != null)) {
204 return false;
205 }
206 return true;
207 }
208
209 public int hashCode() {
210 int result;
211 result = m_pointcutType.hashCode();
212 result = (29 * result) + m_matchingReflectionInfo.hashCode();
213 result = (29 * result) + ((m_withinReflectionInfo != null) ? m_withinReflectionInfo.hashCode() : 0);
214 result = (29 * result) + m_reflectionInfoType;
215 return result;
216 }
217
218 public PointcutType getPointcutType() {
219 return m_pointcutType;
220 }
221 }