1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* PARC initial implementation
* ******************************************************************/
package org.aspectj.weaver;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.aspectj.bridge.ISourceLocation;
import org.aspectj.weaver.patterns.DeclareErrorOrWarning;
import org.aspectj.weaver.patterns.PerClause;
import org.aspectj.weaver.patterns.Pointcut;
/**
* Representation of a shadow munger for a declare error or warning declaration.
*
* @author Andy Clement
*/
public class Checker extends ShadowMunger {
private boolean isError; // if not error then it is a warning
private String message;
private volatile int hashCode = -1;
@SuppressWarnings("unused")
private Checker() {
}
/**
* Create a Checker for a declare error or declare warning.
*
* @param deow the declare error or declare warning for which to create the checker munger
*/
public Checker(DeclareErrorOrWarning deow) {
super(deow.getPointcut(), deow.getStart(), deow.getEnd(), deow.getSourceContext(), ShadowMungerDeow);
this.message = deow.getMessage();
this.isError = deow.isError();
}
/**
* Only used when filling in a parameterized Checker
*/
private Checker(Pointcut pointcut, int start, int end, ISourceContext context, String message, boolean isError) {
super(pointcut, start, end, context, ShadowMungerDeow);
this.message = message;
this.isError = isError;
}
public boolean isError() {
return isError;
}
public String getMessage(Shadow shadow) {
return format(this.message, shadow);
}
@Override
public void specializeOn(Shadow shadow) {
throw new IllegalStateException("Cannot call specializeOn(...) for a Checker");
}
@Override
public boolean implementOn(Shadow shadow) {
throw new IllegalStateException("Cannot call implementOn(...) for a Checker");
}
/**
* Determine if the Checker matches at a shadow. If it does then we can immediately report the message. Currently, there can
* never be a non-statically determinable match.
*
* @param shadow the shadow which to match against
* @param world the world through which to access message handlers
*/
@Override
public boolean match(Shadow shadow, World world) {
if (super.match(shadow, world)) {
world.reportCheckerMatch(this, shadow);
}
return false;
}
// implementation for PartialOrder.PartialComparable
public int compareTo(Object other) {
return 0;
}
@Override
public boolean mustCheckExceptions() {
return true;
}
@Override
public Collection<ResolvedType> getThrownExceptions() {
return Collections.emptyList();
}
// FIXME this perhaps ought to take account of the other fields in advice (use super.equals?)
@Override
public boolean equals(Object other) {
if (!(other instanceof Checker)) {
return false;
}
Checker o = (Checker) other;
return o.isError == isError && ((o.pointcut == null) ? (pointcut == null) : o.pointcut.equals(pointcut));
}
@Override
public int hashCode() {
if (hashCode == -1) {
int result = 17;
result = 37 * result + (isError ? 1 : 0);
result = 37 * result + ((pointcut == null) ? 0 : pointcut.hashCode());
hashCode = result;
}
return hashCode;
}
/**
* Parameterize the Checker by parameterizing the pointcut
*/
@Override
public ShadowMunger parameterizeWith(ResolvedType declaringType, Map<String, UnresolvedType> typeVariableMap) {
Checker ret = new Checker(this.pointcut.parameterizeWith(typeVariableMap, declaringType.getWorld()), this.start, this.end,
this.sourceContext, this.message, this.isError);
return ret;
}
/**
* Concretize this Checker by concretizing the pointcut
*/
@Override
public ShadowMunger concretize(ResolvedType theAspect, World world, PerClause clause) {
this.pointcut = this.pointcut.concretize(theAspect, getDeclaringType(), 0, this);
this.hashCode = -1;
return this;
}
@Override
public ResolvedType getConcreteAspect() {
return getDeclaringType();
}
// public void write(DataOutputStream stream) throws IOException {
// super.write(stream);
// stream.writeBoolean(isError);
// stream.writeUTF(message);
// }
//
// public static Checker read(DataInputStream stream, World world) throws IOException {
// Checker checker = new Checker();
// checker.isError = stream.readBoolean();
// checker.message = stream.readUTF();
// return checker;
// }
// Return the next non-escaped (with a '\') open curly
private int nextCurly(String string, int pos) {
do {
int curlyIndex = string.indexOf('{', pos);
if (curlyIndex == -1) {
return -1;
}
if (curlyIndex == 0) {
return 0;
}
if (string.charAt(curlyIndex - 1) != '\\') {
return curlyIndex;
}
pos = curlyIndex + 1;
} while (pos < string.length());
return -1;
}
private String format(String msg, Shadow shadow) {
int pos = 0;
int curlyIndex = nextCurly(msg, 0);
if (curlyIndex == -1) {
// was there an escaped one?
if (msg.indexOf('{') != -1) {
return msg.replace("\\{", "{");
} else {
return msg;
}
}
StringBuffer ret = new StringBuffer();
while (curlyIndex >= 0) {
if (curlyIndex > 0) {
ret.append(msg.substring(pos, curlyIndex).replace("\\{", "{"));
}
int endCurly = msg.indexOf('}', curlyIndex);
if (endCurly == -1) {
// wasn't closed properly - ignore it
ret.append('{');
pos = curlyIndex + 1;
} else {
ret.append(getValue(msg.substring(curlyIndex + 1, endCurly), shadow));
}
pos = endCurly + 1;
curlyIndex = nextCurly(msg, pos);
}
ret.append(msg.substring(pos, msg.length()));
return ret.toString();
}
/**
* @param buf the buffer in which to insert the substitution
* @param shadow shadow from which to draw context info
* @param c the substitution character
*/
private String getValue(String key, Shadow shadow) {
if (key.equalsIgnoreCase("joinpoint")) {
return shadow.toString();
} else if (key.equalsIgnoreCase("joinpoint.kind")) {
return shadow.getKind().getName();
} else if (key.equalsIgnoreCase("joinpoint.enclosingclass")) {
return shadow.getEnclosingType().getName();
} else if (key.equalsIgnoreCase("joinpoint.enclosingmember.name")) {
Member member = shadow.getEnclosingCodeSignature();
if (member==null) {
return "";
} else {
return member.getName();
}
} else if (key.equalsIgnoreCase("joinpoint.enclosingmember")) {
Member member = shadow.getEnclosingCodeSignature();
if (member==null) {
return "";
} else {
return member.toString();
}
} else if (key.equalsIgnoreCase("joinpoint.signature")) {
return shadow.getSignature().toString();
} else if (key.equalsIgnoreCase("joinpoint.signature.declaringtype")) {
return shadow.getSignature().getDeclaringType().toString();
} else if (key.equalsIgnoreCase("joinpoint.signature.name")) {
return shadow.getSignature().getName();
} else if (key.equalsIgnoreCase("joinpoint.sourcelocation.sourcefile")) {
ISourceLocation loc = shadow.getSourceLocation();
if ((loc != null) && (loc.getSourceFile() != null)) {
return loc.getSourceFile().toString();
} else {
return "UNKNOWN";
}
} else if (key.equalsIgnoreCase("joinpoint.sourcelocation.line")) {
ISourceLocation loc = shadow.getSourceLocation();
if (loc != null) {
return Integer.toString(loc.getLine());
} else {
return "-1";
}
} else if (key.equalsIgnoreCase("advice.aspecttype")) {
return getDeclaringType().getName();
} else if (key.equalsIgnoreCase("advice.sourcelocation.line")) {
ISourceLocation loc = getSourceLocation();
if ((loc != null) && (loc.getSourceFile() != null)) {
return Integer.toString(loc.getLine());
} else {
return "-1";
}
} else if (key.equalsIgnoreCase("advice.sourcelocation.sourcefile")) {
ISourceLocation loc = getSourceLocation();
if ((loc != null) && (loc.getSourceFile() != null)) {
return loc.getSourceFile().toString();
} else {
return "UNKNOWN";
}
} else {
return "UNKNOWN_KEY{" + key + "}";
}
}
}
|