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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/* *******************************************************************
* Copyright (c) 2004 IBM Corporation.
* 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
*
* ******************************************************************/
package org.aspectj.weaver.internal.tools;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.Shadow;
import org.aspectj.weaver.World;
import org.aspectj.weaver.ast.Literal;
import org.aspectj.weaver.ast.Test;
import org.aspectj.weaver.patterns.AbstractPatternNodeVisitor;
import org.aspectj.weaver.patterns.AnnotationPointcut;
import org.aspectj.weaver.patterns.ArgsAnnotationPointcut;
import org.aspectj.weaver.patterns.ArgsPointcut;
import org.aspectj.weaver.patterns.CflowPointcut;
import org.aspectj.weaver.patterns.ExposedState;
import org.aspectj.weaver.patterns.IfPointcut;
import org.aspectj.weaver.patterns.NotAnnotationTypePattern;
import org.aspectj.weaver.patterns.NotPointcut;
import org.aspectj.weaver.patterns.Pointcut;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.aspectj.weaver.patterns.ThisOrTargetPointcut;
import org.aspectj.weaver.patterns.WithinAnnotationPointcut;
import org.aspectj.weaver.patterns.WithinCodeAnnotationPointcut;
import org.aspectj.weaver.reflect.ReflectionFastMatchInfo;
import org.aspectj.weaver.reflect.ReflectionShadow;
import org.aspectj.weaver.reflect.ReflectionWorld;
import org.aspectj.weaver.reflect.ShadowMatchImpl;
import org.aspectj.weaver.tools.DefaultMatchingContext;
import org.aspectj.weaver.tools.MatchingContext;
import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParameter;
import org.aspectj.weaver.tools.ShadowMatch;
/**
* Map from weaver.tools interface to internal Pointcut implementation...
*/
public class PointcutExpressionImpl implements PointcutExpression {
private final static boolean MATCH_INFO = false;
private World world;
private Pointcut pointcut;
private String expression;
private PointcutParameter[] parameters;
private MatchingContext matchContext = new DefaultMatchingContext();
public PointcutExpressionImpl(Pointcut pointcut, String expression, PointcutParameter[] params, World inWorld) {
this.pointcut = pointcut;
this.expression = expression;
this.world = inWorld;
this.parameters = params;
if (this.parameters == null) {
this.parameters = new PointcutParameter[0];
}
}
public Pointcut getUnderlyingPointcut() {
return this.pointcut;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.tools.PointcutExpression#setMatchingContext(org.aspectj.weaver.tools.MatchingContext)
*/
public void setMatchingContext(MatchingContext aMatchContext) {
this.matchContext = aMatchContext;
}
public boolean couldMatchJoinPointsInType(Class aClass) {
ResolvedType matchType = world.resolve(aClass.getName());
if (matchType.isMissing() && (world instanceof ReflectionWorld)) {
// Class is a generated class that cannot be 'looked up' via getResource.
// For example a proxy or lambda.
// Use the class itself in this case
matchType = ((ReflectionWorld)world).resolveUsingClass(aClass);
}
ReflectionFastMatchInfo info = new ReflectionFastMatchInfo(matchType, null, this.matchContext, world);
boolean couldMatch = pointcut.fastMatch(info).maybeTrue();
if (MATCH_INFO) {
System.out.println("MATCHINFO: fast match for '" + this.expression + "' against '" + aClass.getName() + "': "
+ couldMatch);
}
return couldMatch;
}
public boolean mayNeedDynamicTest() {
HasPossibleDynamicContentVisitor visitor = new HasPossibleDynamicContentVisitor();
pointcut.traverse(visitor, null);
return visitor.hasDynamicContent();
}
private ExposedState getExposedState() {
return new ExposedState(parameters.length);
}
public ShadowMatch matchesMethodExecution(Method aMethod) {
ShadowMatch match = matchesExecution(aMethod);
if (MATCH_INFO && match.maybeMatches()) {
System.out.println("MATCHINFO: method execution match on '" + aMethod + "' for '" + this.expression + "': "
+ (match.alwaysMatches() ? "YES" : "MAYBE"));
}
return match;
}
public ShadowMatch matchesConstructorExecution(Constructor aConstructor) {
ShadowMatch match = matchesExecution(aConstructor);
if (MATCH_INFO && match.maybeMatches()) {
System.out.println("MATCHINFO: constructor execution match on '" + aConstructor + "' for '" + this.expression + "': "
+ (match.alwaysMatches() ? "YES" : "MAYBE"));
}
return match;
}
private ShadowMatch matchesExecution(Member aMember) {
Shadow s = ReflectionShadow.makeExecutionShadow(world, aMember, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aMember);
sm.setWithinCode(null);
sm.setWithinType(aMember.getDeclaringClass());
return sm;
}
public ShadowMatch matchesStaticInitialization(Class aClass) {
Shadow s = ReflectionShadow.makeStaticInitializationShadow(world, aClass, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(null);
sm.setWithinCode(null);
sm.setWithinType(aClass);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: static initialization match on '" + aClass.getName() + "' for '" + this.expression
+ "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesAdviceExecution(Method aMethod) {
Shadow s = ReflectionShadow.makeAdviceExecutionShadow(world, aMethod, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aMethod);
sm.setWithinCode(null);
sm.setWithinType(aMethod.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: advice execution match on '" + aMethod + "' for '" + this.expression + "': "
+ (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesInitialization(Constructor aConstructor) {
Shadow s = ReflectionShadow.makeInitializationShadow(world, aConstructor, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aConstructor);
sm.setWithinCode(null);
sm.setWithinType(aConstructor.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: initialization match on '" + aConstructor + "' for '" + this.expression + "': "
+ (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesPreInitialization(Constructor aConstructor) {
Shadow s = ReflectionShadow.makePreInitializationShadow(world, aConstructor, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aConstructor);
sm.setWithinCode(null);
sm.setWithinType(aConstructor.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: preinitialization match on '" + aConstructor + "' for '" + this.expression + "': "
+ (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesMethodCall(Method aMethod, Member withinCode) {
Shadow s = ReflectionShadow.makeCallShadow(world, aMethod, withinCode, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aMethod);
sm.setWithinCode(withinCode);
sm.setWithinType(withinCode.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: method call match on '" + aMethod + "' withinCode='" + withinCode + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesMethodCall(Method aMethod, Class callerType) {
Shadow s = ReflectionShadow.makeCallShadow(world, aMethod, callerType, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aMethod);
sm.setWithinCode(null);
sm.setWithinType(callerType);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: method call match on '" + aMethod + "' callerType='" + callerType.getName() + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesConstructorCall(Constructor aConstructor, Class callerType) {
Shadow s = ReflectionShadow.makeCallShadow(world, aConstructor, callerType, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aConstructor);
sm.setWithinCode(null);
sm.setWithinType(callerType);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: constructor call match on '" + aConstructor + "' callerType='" + callerType.getName()
+ "' for '" + this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesConstructorCall(Constructor aConstructor, Member withinCode) {
Shadow s = ReflectionShadow.makeCallShadow(world, aConstructor, withinCode, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aConstructor);
sm.setWithinCode(withinCode);
sm.setWithinType(withinCode.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: constructor call match on '" + aConstructor + "' withinCode='" + withinCode + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesHandler(Class exceptionType, Class handlingType) {
Shadow s = ReflectionShadow.makeHandlerShadow(world, exceptionType, handlingType, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(null);
sm.setWithinCode(null);
sm.setWithinType(handlingType);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: handler match on '" + exceptionType.getName() + "' handlingType='" + handlingType
+ "' for '" + this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesHandler(Class exceptionType, Member withinCode) {
Shadow s = ReflectionShadow.makeHandlerShadow(world, exceptionType, withinCode, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(null);
sm.setWithinCode(withinCode);
sm.setWithinType(withinCode.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: handler match on '" + exceptionType.getName() + "' withinCode='" + withinCode
+ "' for '" + this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesFieldGet(Field aField, Class withinType) {
Shadow s = ReflectionShadow.makeFieldGetShadow(world, aField, withinType, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aField);
sm.setWithinCode(null);
sm.setWithinType(withinType);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: field get match on '" + aField + "' withinType='" + withinType.getName() + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesFieldGet(Field aField, Member withinCode) {
Shadow s = ReflectionShadow.makeFieldGetShadow(world, aField, withinCode, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aField);
sm.setWithinCode(withinCode);
sm.setWithinType(withinCode.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: field get match on '" + aField + "' withinCode='" + withinCode + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesFieldSet(Field aField, Class withinType) {
Shadow s = ReflectionShadow.makeFieldSetShadow(world, aField, withinType, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aField);
sm.setWithinCode(null);
sm.setWithinType(withinType);
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: field set match on '" + aField + "' withinType='" + withinType.getName() + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
public ShadowMatch matchesFieldSet(Field aField, Member withinCode) {
Shadow s = ReflectionShadow.makeFieldSetShadow(world, aField, withinCode, this.matchContext);
ShadowMatchImpl sm = getShadowMatch(s);
sm.setSubject(aField);
sm.setWithinCode(withinCode);
sm.setWithinType(withinCode.getDeclaringClass());
if (MATCH_INFO && sm.maybeMatches()) {
System.out.println("MATCHINFO: field set match on '" + aField + "' withinCode='" + withinCode + "' for '"
+ this.expression + "': " + (sm.alwaysMatches() ? "YES" : "MAYBE"));
}
return sm;
}
private ShadowMatchImpl getShadowMatch(Shadow forShadow) {
org.aspectj.util.FuzzyBoolean match = pointcut.match(forShadow);
Test residueTest = Literal.TRUE;
ExposedState state = getExposedState();
if (match.maybeTrue()) {
residueTest = pointcut.findResidue(forShadow, state);
}
ShadowMatchImpl sm = new ShadowMatchImpl(match, residueTest, state, parameters);
sm.setMatchingContext(this.matchContext);
return sm;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.tools.PointcutExpression#getPointcutExpression()
*/
public String getPointcutExpression() {
return expression;
}
private static class HasPossibleDynamicContentVisitor extends AbstractPatternNodeVisitor {
private boolean hasDynamicContent = false;
public boolean hasDynamicContent() {
return hasDynamicContent;
}
@Override
public Object visit(WithinAnnotationPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(WithinCodeAnnotationPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(AnnotationPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(ArgsAnnotationPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(ArgsPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(CflowPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(IfPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(NotAnnotationTypePattern node, Object data) {
return node.getNegatedPattern().accept(this, data);
}
@Override
public Object visit(NotPointcut node, Object data) {
return node.getNegatedPointcut().accept(this, data);
}
@Override
public Object visit(ThisOrTargetAnnotationPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
@Override
public Object visit(ThisOrTargetPointcut node, Object data) {
hasDynamicContent = true;
return null;
}
}
public static class Handler implements Member {
private Class<?> decClass;
private Class<?> exType;
public Handler(Class decClass, Class exType) {
this.decClass = decClass;
this.exType = exType;
}
public int getModifiers() {
return 0;
}
public Class getDeclaringClass() {
return decClass;
}
public String getName() {
return null;
}
public Class getHandledExceptionType() {
return exType;
}
public boolean isSynthetic() {
return false;
}
}
}
|