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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* *******************************************************************
* Copyright (c) 2005 Contributors.
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* Adrian Colyer Initial implementation
* ******************************************************************/
package org.aspectj.weaver.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.bridge.ISourceLocation;
import org.aspectj.weaver.Member;
import org.aspectj.weaver.ResolvedMember;
import org.aspectj.weaver.ResolvedMemberImpl;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.Shadow;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.World;
import org.aspectj.weaver.ast.Var;
import org.aspectj.weaver.tools.MatchingContext;
/**
* @author colyer
*
*/
public class StandardShadow extends Shadow {
private final World world;
private final ResolvedType enclosingType;
private final ResolvedMember enclosingMember;
private final MatchingContext matchContext;
private Var thisVar = null;
private Var targetVar = null;
private Var[] argsVars = null;
private Var atThisVar = null;
private Var atTargetVar = null;
private Map<ResolvedType, Var[]> atArgsVars = new HashMap<>();
private Map<ResolvedType, Var> withinAnnotationVar = new HashMap<>();
private Map<ResolvedType, Var> withinCodeAnnotationVar = new HashMap<>();
private Map<ResolvedType, Var> annotationVar = new HashMap<>();
private AnnotationFinder annotationFinder;
public static Shadow makeExecutionShadow(World inWorld, java.lang.reflect.Member forMethod, MatchingContext withContext) {
Kind kind = (forMethod instanceof Method) ? Shadow.MethodExecution : Shadow.ConstructorExecution;
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forMethod, inWorld);
ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
}
public static Shadow makeExecutionShadow(World inWorld, ResolvedMember forMethod, MatchingContext withContext) {
Kind kind = forMethod.getName().equals("<init>") ? Shadow.ConstructorExecution : Shadow.MethodExecution;
// Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forMethod, inWorld);
// ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, forMethod, null, (ResolvedType) forMethod.getDeclaringType(), null, withContext);
}
public static Shadow makeAdviceExecutionShadow(World inWorld, java.lang.reflect.Method forMethod, MatchingContext withContext) {
Kind kind = Shadow.AdviceExecution;
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedAdviceMember(forMethod, inWorld);
ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
}
public static Shadow makeCallShadow(World inWorld, ResolvedMember aMember, ResolvedMember withinCode,
MatchingContext withContext) {
Shadow enclosingShadow = makeExecutionShadow(inWorld, withinCode, withContext);
// Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(aMember, inWorld);
// ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(withinCode, inWorld);
// ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = !aMember.getName().equals("<init>") ? Shadow.MethodCall : Shadow.ConstructorCall;
return new StandardShadow(inWorld, kind, aMember, enclosingShadow, (ResolvedType) withinCode.getDeclaringType(),
withinCode, withContext);
}
public static Shadow makeCallShadow(World inWorld, java.lang.reflect.Member aMember, Class thisClass,
MatchingContext withContext) {
Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, thisClass, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(aMember, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(thisClass, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = aMember instanceof Method ? Shadow.MethodCall : Shadow.ConstructorCall;
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeStaticInitializationShadow(World inWorld, Class forType, MatchingContext withContext) {
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(forType, inWorld);
ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
Kind kind = Shadow.StaticInitialization;
return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
}
public static Shadow makeStaticInitializationShadow(World inWorld, ResolvedType forType, MatchingContext withContext) {
ResolvedMember[] members = forType.getDeclaredMethods();
int clinit = -1;
for (int i = 0; i < members.length && clinit == -1; i++) {
if (members[i].getName().equals("<clinit>")) {
clinit = i;
}
}
// Member signature = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(forType, inWorld);
Kind kind = Shadow.StaticInitialization;
if (clinit == -1) {
Member clinitMember = new ResolvedMemberImpl(org.aspectj.weaver.Member.STATIC_INITIALIZATION, forType, Modifier.STATIC,
UnresolvedType.VOID, "<clinit>", UnresolvedType.NONE, UnresolvedType.NONE);
return new StandardShadow(inWorld, kind, clinitMember, null, forType, null, withContext);
} else {
return new StandardShadow(inWorld, kind, members[clinit], null, forType, null, withContext);
}
}
public static Shadow makePreInitializationShadow(World inWorld, Constructor forConstructor, MatchingContext withContext) {
Kind kind = Shadow.PreInitialization;
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forConstructor, inWorld);
ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
}
public static Shadow makeInitializationShadow(World inWorld, Constructor forConstructor, MatchingContext withContext) {
Kind kind = Shadow.Initialization;
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(forConstructor, inWorld);
ResolvedType enclosingType = signature.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, null, enclosingType, null, withContext);
}
public static Shadow makeHandlerShadow(World inWorld, Class exceptionType, Class withinType, MatchingContext withContext) {
Kind kind = Shadow.ExceptionHandler;
Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, withinType, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createHandlerMember(exceptionType, withinType, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(withinType, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeHandlerShadow(World inWorld, Class exceptionType, java.lang.reflect.Member withinCode,
MatchingContext withContext) {
Kind kind = Shadow.ExceptionHandler;
Shadow enclosingShadow = makeExecutionShadow(inWorld, withinCode, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createHandlerMember(exceptionType,
withinCode.getDeclaringClass(), inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(withinCode, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeFieldGetShadow(World inWorld, Field forField, Class callerType, MatchingContext withContext) {
Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, callerType, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(callerType, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = Shadow.FieldGet;
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeFieldGetShadow(World inWorld, Field forField, java.lang.reflect.Member inMember,
MatchingContext withContext) {
Shadow enclosingShadow = makeExecutionShadow(inWorld, inMember, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(inMember, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = Shadow.FieldGet;
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeFieldSetShadow(World inWorld, Field forField, Class callerType, MatchingContext withContext) {
Shadow enclosingShadow = makeStaticInitializationShadow(inWorld, callerType, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createStaticInitMember(callerType, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = Shadow.FieldSet;
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public static Shadow makeFieldSetShadow(World inWorld, Field forField, java.lang.reflect.Member inMember,
MatchingContext withContext) {
Shadow enclosingShadow = makeExecutionShadow(inWorld, inMember, withContext);
Member signature = ReflectionBasedReferenceTypeDelegateFactory.createResolvedField(forField, inWorld);
ResolvedMember enclosingMember = ReflectionBasedReferenceTypeDelegateFactory.createResolvedMember(inMember, inWorld);
ResolvedType enclosingType = enclosingMember.getDeclaringType().resolve(inWorld);
Kind kind = Shadow.FieldSet;
return new StandardShadow(inWorld, kind, signature, enclosingShadow, enclosingType, enclosingMember, withContext);
}
public StandardShadow(World world, Kind kind, Member signature, Shadow enclosingShadow, ResolvedType enclosingType,
ResolvedMember enclosingMember, MatchingContext withContext) {
super(kind, signature, enclosingShadow);
this.world = world;
this.enclosingType = enclosingType;
this.enclosingMember = enclosingMember;
this.matchContext = withContext;
if (world instanceof IReflectionWorld) {
this.annotationFinder = ((IReflectionWorld) world).getAnnotationFinder();
}
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getIWorld()
*/
public World getIWorld() {
return world;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getThisVar()
*/
public Var getThisVar() {
if (thisVar == null && hasThis()) {
thisVar = ReflectionVar.createThisVar(getThisType().resolve(world), this.annotationFinder);
}
return thisVar;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getTargetVar()
*/
public Var getTargetVar() {
if (targetVar == null && hasTarget()) {
targetVar = ReflectionVar.createTargetVar(getThisType().resolve(world), this.annotationFinder);
}
return targetVar;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getEnclosingType()
*/
public UnresolvedType getEnclosingType() {
return this.enclosingType;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getArgVar(int)
*/
public Var getArgVar(int i) {
if (argsVars == null) {
this.argsVars = new Var[this.getArgCount()];
for (int j = 0; j < this.argsVars.length; j++) {
this.argsVars[j] = ReflectionVar.createArgsVar(getArgType(j).resolve(world), j, this.annotationFinder);
}
}
if (i < argsVars.length) {
return argsVars[i];
} else {
return null;
}
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getThisJoinPointVar()
*/
public Var getThisJoinPointVar() {
// TODO Auto-generated method stub
return null;
}
public Var getThisJoinPointStaticPartVar() {
return null;
}
public Var getThisEnclosingJoinPointStaticPartVar() {
return null;
}
public Var getThisAspectInstanceVar(ResolvedType aspectType) {
return null;
}
public Var getKindedAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (annotationVar.get(annType) == null) {
Var v = ReflectionVar.createAtAnnotationVar(annType, this.annotationFinder);
annotationVar.put(annType, v);
}
return annotationVar.get(annType);
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getWithinAnnotationVar(org.aspectj.weaver.UnresolvedType)
*/
public Var getWithinAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (withinAnnotationVar.get(annType) == null) {
Var v = ReflectionVar.createWithinAnnotationVar(annType, this.annotationFinder);
withinAnnotationVar.put(annType, v);
}
return withinAnnotationVar.get(annType);
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getWithinCodeAnnotationVar(org.aspectj.weaver.UnresolvedType)
*/
public Var getWithinCodeAnnotationVar(UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (withinCodeAnnotationVar.get(annType) == null) {
Var v = ReflectionVar.createWithinCodeAnnotationVar(annType, this.annotationFinder);
withinCodeAnnotationVar.put(annType, v);
}
return withinCodeAnnotationVar.get(annType);
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getThisAnnotationVar(org.aspectj.weaver.UnresolvedType)
*/
public Var getThisAnnotationVar(UnresolvedType forAnnotationType) {
if (atThisVar == null) {
atThisVar = ReflectionVar.createThisAnnotationVar(forAnnotationType.resolve(world), this.annotationFinder);
}
return atThisVar;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getTargetAnnotationVar(org.aspectj.weaver.UnresolvedType)
*/
public Var getTargetAnnotationVar(UnresolvedType forAnnotationType) {
if (atTargetVar == null) {
atTargetVar = ReflectionVar.createTargetAnnotationVar(forAnnotationType.resolve(world), this.annotationFinder);
}
return atTargetVar;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getArgAnnotationVar(int, org.aspectj.weaver.UnresolvedType)
*/
public Var getArgAnnotationVar(int i, UnresolvedType forAnnotationType) {
ResolvedType annType = forAnnotationType.resolve(world);
if (atArgsVars.get(annType) == null) {
Var[] vars = new Var[getArgCount()];
atArgsVars.put(annType, vars);
}
Var[] vars = atArgsVars.get(annType);
if (i > (vars.length - 1))
return null;
if (vars[i] == null) {
vars[i] = ReflectionVar.createArgsAnnotationVar(annType, i, this.annotationFinder);
}
return vars[i];
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getEnclosingCodeSignature()
*/
public Member getEnclosingCodeSignature() {
// XXX this code is copied from BcelShadow with one minor change...
if (getKind().isEnclosingKind()) {
return getSignature();
} else if (getKind() == Shadow.PreInitialization) {
// PreInit doesn't enclose code but its signature
// is correctly the signature of the ctor.
return getSignature();
} else if (enclosingShadow == null) {
return this.enclosingMember;
} else {
return enclosingShadow.getSignature();
}
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.Shadow#getSourceLocation()
*/
public ISourceLocation getSourceLocation() {
return null;
}
public MatchingContext getMatchingContext() {
return this.matchContext;
}
}
|