aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/KindedPointcut.java
blob: 098c2873fb26bed7dca53df14f8682f2784e1533 (plain)
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* *******************************************************************
 * 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 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:
 *     PARC     initial implementation
 * ******************************************************************/

package org.aspectj.weaver.patterns;

import static org.aspectj.util.FuzzyBoolean.MAYBE;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import org.aspectj.bridge.ISourceLocation;
import org.aspectj.bridge.MessageUtil;
import org.aspectj.util.FuzzyBoolean;
import org.aspectj.weaver.Checker;
import org.aspectj.weaver.CompressingDataOutputStream;
import org.aspectj.weaver.ISourceContext;
import org.aspectj.weaver.IntMap;
import org.aspectj.weaver.Member;
import org.aspectj.weaver.ResolvedMember;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.Shadow;
import org.aspectj.weaver.ShadowMunger;
import org.aspectj.weaver.UnresolvedType;
import org.aspectj.weaver.VersionedDataInputStream;
import org.aspectj.weaver.WeaverMessages;
import org.aspectj.weaver.World;
import org.aspectj.weaver.ast.Literal;
import org.aspectj.weaver.ast.Test;

public class KindedPointcut extends Pointcut {
	Shadow.Kind kind;
	private SignaturePattern signature;
	private int matchKinds;

	private ShadowMunger munger = null; // only set after concretization

	public KindedPointcut(Shadow.Kind kind, SignaturePattern signature) {
		this.kind = kind;
		this.signature = signature;
		this.pointcutKind = KINDED;
		this.matchKinds = kind.bit;
	}

	public KindedPointcut(Shadow.Kind kind, SignaturePattern signature, ShadowMunger munger) {
		this(kind, signature);
		this.munger = munger;
	}

	public SignaturePattern getSignature() {
		return signature;
	}

	@Override
	public int couldMatchKinds() {
		return matchKinds;
	}

	public boolean couldEverMatchSameJoinPointsAs(KindedPointcut other) {
		if (this.kind != other.kind) {
			return false;
		}
		String myName = signature.getName().maybeGetSimpleName();
		String yourName = other.signature.getName().maybeGetSimpleName();
		if (myName != null && yourName != null) {
			if (!myName.equals(yourName)) {
				return false;
			}
		}
		if (signature.getParameterTypes().ellipsisCount == 0) {
			if (other.signature.getParameterTypes().ellipsisCount == 0) {
				if (signature.getParameterTypes().getTypePatterns().length != other.signature.getParameterTypes().getTypePatterns().length) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public FuzzyBoolean fastMatch(FastMatchInfo info) {
		// info.getKind()==null means all kinds
		if (info.getKind() != null) {
			if (info.getKind() != kind) {
				return FuzzyBoolean.NO;
			}
		}

		// KindedPointcut represents these join points:
		// method-execution/ctor-execution/method-call/ctor-call/field-get/field-set/advice-execution/static-initialization
		// initialization/pre-initialization

		// Check if the global fastmatch flag is on - the flag can be removed (and this made default) once it proves stable!
		if (info.world.optimizedMatching) {

			// For now, just consider MethodExecution and Initialization
			if ((kind == Shadow.MethodExecution || kind == Shadow.Initialization) && info.getKind() == null) {
				boolean fastMatchingOnAspect = info.getType().isAspect();
				// an Aspect may define ITDs, and although our signature declaring type pattern may not match on the
				// aspect, the ITDs may have a different signature as we iterate through the members of the aspect. Let's not
				// try and work through that here and just say MAYBE
				if (fastMatchingOnAspect) {
					return MAYBE;
				}
				// Aim here is to do the same test as is done for signature pattern declaring type pattern matching
				if (this.getSignature().isExactDeclaringTypePattern()) {
					ExactTypePattern typePattern = (ExactTypePattern) this.getSignature().getDeclaringType();
					// Interface checks are more expensive, they could be anywhere...
					ResolvedType patternExactType = typePattern.getResolvedExactType(info.world);
					if (patternExactType.isInterface()) {
						ResolvedType curr = info.getType();
						Iterator<ResolvedType> hierarchyWalker = curr.getHierarchy(true, true);
						boolean found = false;
						while (hierarchyWalker.hasNext()) {
							curr = hierarchyWalker.next();
							if (typePattern.matchesStatically(curr)) {
								found = true;
								break;
							}
						}
						if (!found) {
							return FuzzyBoolean.NO;
						}
					} else if (patternExactType.isClass()) {
						ResolvedType curr = info.getType();
						do {
							if (typePattern.matchesStatically(curr)) {
								break;
							}
							curr = curr.getSuperclass();
						} while (curr != null);
						if (curr == null) {
							return FuzzyBoolean.NO;
						}
					}
				} else if (this.getSignature().getDeclaringType() instanceof AnyWithAnnotationTypePattern) {
					// aim here is to say NO if the annotation is not possible in the hierarchy here
					ResolvedType type = info.getType();
					AnnotationTypePattern annotationTypePattern = ((AnyWithAnnotationTypePattern) getSignature().getDeclaringType())
							.getAnnotationPattern();
					if (annotationTypePattern instanceof ExactAnnotationTypePattern) {
						ExactAnnotationTypePattern exactAnnotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern;
						if (exactAnnotationTypePattern.getAnnotationValues() == null
								|| exactAnnotationTypePattern.getAnnotationValues().size() == 0) {
							ResolvedType annotationType = exactAnnotationTypePattern.getAnnotationType().resolve(info.world);
							if (type.hasAnnotation(annotationType)) {
								return FuzzyBoolean.MAYBE;
							}
							if (annotationType.isInheritedAnnotation()) {
								// ok - we may be picking it up from further up the hierarchy (but only a super*class*)
								ResolvedType toMatchAgainst = type.getSuperclass();
								boolean found = false;
								while (toMatchAgainst != null) {
									if (toMatchAgainst.hasAnnotation(annotationType)) {
										found = true;
										break;
									}
									toMatchAgainst = toMatchAgainst.getSuperclass();
								}
								if (!found) {
									return FuzzyBoolean.NO;
								}
							} else {
								return FuzzyBoolean.NO;
							}
						}
					}
					// Optimization from 532033: passes AspectJ tests but breaks Spring Framework
//				} else  if (this.getSignature().getDeclaringType() instanceof WildTypePattern) {
//					final WildTypePattern pattern = (WildTypePattern) this.getSignature().getDeclaringType();
//					final ResolvedType type = info.getType();
//					return pattern.matches(type, TypePattern.STATIC);
				}
			}
		}

		return FuzzyBoolean.MAYBE;
	}

	@Override
	protected FuzzyBoolean matchInternal(Shadow shadow) {
		if (shadow.getKind() != kind) {
			return FuzzyBoolean.NO;
		}

		if (shadow.getKind() == Shadow.SynchronizationLock && kind == Shadow.SynchronizationLock) {
			return FuzzyBoolean.YES;
		}
		if (shadow.getKind() == Shadow.SynchronizationUnlock && kind == Shadow.SynchronizationUnlock) {
			return FuzzyBoolean.YES;
		}

		if (!signature.matches(shadow.getMatchingSignature(), shadow.getIWorld(), this.kind == Shadow.MethodCall)) {

			if (kind == Shadow.MethodCall) {
				warnOnConfusingSig(shadow);
				// warnOnBridgeMethod(shadow);
			}
			return FuzzyBoolean.NO;
		}

		return FuzzyBoolean.YES;
	}

	// private void warnOnBridgeMethod(Shadow shadow) {
	// if (shadow.getIWorld().getLint().noJoinpointsForBridgeMethods.isEnabled()) {
	// ResolvedMember rm = shadow.getSignature().resolve(shadow.getIWorld());
	// if (rm!=null) {
	// int shadowModifiers = rm.getModifiers(); //shadow.getSignature().getModifiers(shadow.getIWorld());
	// if (ResolvedType.hasBridgeModifier(shadowModifiers)) {
	// shadow.getIWorld().getLint().noJoinpointsForBridgeMethods.signal(new String[]{},getSourceLocation(),
	// new ISourceLocation[]{shadow.getSourceLocation()});
	// }
	// }
	// }
	// }

	private void warnOnConfusingSig(Shadow shadow) {
		// Don't do all this processing if we don't need to !
		if (!shadow.getIWorld().getLint().unmatchedSuperTypeInCall.isEnabled()) {
			return;
		}

		// no warnings for declare error/warning
		if (munger instanceof Checker) {
			return;
		}

		World world = shadow.getIWorld();

		// warning never needed if the declaring type is any
		UnresolvedType exactDeclaringType = signature.getDeclaringType().getExactType();

		ResolvedType shadowDeclaringType = shadow.getSignature().getDeclaringType().resolve(world);

		if (signature.getDeclaringType().isStar() || ResolvedType.isMissing(exactDeclaringType)
				|| exactDeclaringType.resolve(world).isMissing()) {
			return;
		}

		// warning not needed if match type couldn't ever be the declaring type
		if (!shadowDeclaringType.isAssignableFrom(exactDeclaringType.resolve(world))) {
			return;
		}

		// if the method in the declaring type is *not* visible to the
		// exact declaring type then warning not needed.
		ResolvedMember rm = shadow.getSignature().resolve(world);
		// rm can be null in the case where we are binary weaving, and looking at a class with a call to a method in another class,
		// but because of class incompatibilities, the method does not exist on the target class anymore.
		// this will be reported elsewhere.
		if (rm == null) {
			return;
		}

		int shadowModifiers = rm.getModifiers();
		if (!ResolvedType.isVisible(shadowModifiers, shadowDeclaringType, exactDeclaringType.resolve(world))) {
			return;
		}

		if (!signature.getReturnType().matchesStatically(shadow.getSignature().getReturnType().resolve(world))) {
			// Covariance issue...
			// The reason we didn't match is that the type pattern for the pointcut (Car) doesn't match the
			// return type for the specific declaration at the shadow. (FastCar Sub.getCar())
			// XXX Put out another XLINT in this case?
			return;
		}
		// PR60015 - Don't report the warning if the declaring type is object and 'this' is an interface
		if (exactDeclaringType.resolve(world).isInterface() && shadowDeclaringType.equals(world.resolve("java.lang.Object"))) {
			return;
		}

		SignaturePattern nonConfusingPattern = new SignaturePattern(signature.getKind(), signature.getModifiers(),
				signature.getReturnType(), TypePattern.ANY, signature.getName(), signature.getParameterTypes(),
				signature.getThrowsPattern(), signature.getAnnotationPattern());

		if (nonConfusingPattern.matches(shadow.getSignature(), shadow.getIWorld(), true)) {
			shadow.getIWorld().getLint().unmatchedSuperTypeInCall.signal(new String[] {
					shadow.getSignature().getDeclaringType().toString(), signature.getDeclaringType().toString() },
					this.getSourceLocation(), new ISourceLocation[] { shadow.getSourceLocation() });
		}
	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof KindedPointcut)) {
			return false;
		}
		KindedPointcut o = (KindedPointcut) other;
		return o.kind == this.kind && o.signature.equals(this.signature);
	}

	@Override
	public int hashCode() {
		int result = 17;
		result = 37 * result + kind.hashCode();
		result = 37 * result + signature.hashCode();
		return result;
	}

	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(kind.getSimpleName());
		buf.append("(");
		buf.append(signature.toString());
		buf.append(")");
		return buf.toString();
	}

	@Override
	public void postRead(ResolvedType enclosingType) {
		signature.postRead(enclosingType);
	}

	@Override
	public void write(CompressingDataOutputStream s) throws IOException {
		s.writeByte(Pointcut.KINDED);
		kind.write(s);
		signature.write(s);
		writeLocation(s);
	}

	public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
		Shadow.Kind kind = Shadow.Kind.read(s);
		SignaturePattern sig = SignaturePattern.read(s, context);
		KindedPointcut ret = new KindedPointcut(kind, sig);
		ret.readLocation(context, s);
		return ret;
	}

	// XXX note: there is no namebinding in any kinded pointcut.
	// still might want to do something for better error messages
	// We want to do something here to make sure we don't sidestep the parameter
	// list in capturing type identifiers.
	@Override
	public void resolveBindings(IScope scope, Bindings bindings) {
		if (kind == Shadow.Initialization) {
			// scope.getMessageHandler().handleMessage(
			// MessageUtil.error(
			// "initialization unimplemented in 1.1beta1",
			// this.getSourceLocation()));
		}
		signature = signature.resolveBindings(scope, bindings);

		if (kind == Shadow.ConstructorExecution) { // Bug fix 60936
			if (signature.getDeclaringType() != null) {
				World world = scope.getWorld();
				UnresolvedType exactType = signature.getDeclaringType().getExactType();
				if (signature.getKind() == Member.CONSTRUCTOR && !ResolvedType.isMissing(exactType)
						&& exactType.resolve(world).isInterface() && !signature.getDeclaringType().isIncludeSubtypes()) {
					world.getLint().noInterfaceCtorJoinpoint.signal(exactType.toString(), getSourceLocation());
				}
			}
		}

		// no parameterized types
		if (kind == Shadow.StaticInitialization) {
			HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getDeclaringType().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_STATIC_INIT_JPS_FOR_PARAMETERIZED_TYPES),
						getSourceLocation()));
			}
		}

		// no parameterized types in declaring type position
		if ((kind == Shadow.FieldGet) || (kind == Shadow.FieldSet)) {
			HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getDeclaringType().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.GET_AND_SET_DONT_SUPPORT_DEC_TYPE_PARAMETERS),
						getSourceLocation()));
			}

			// fields can't have a void type!
			UnresolvedType returnType = signature.getReturnType().getExactType();
			if (returnType.equals(UnresolvedType.VOID)) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.FIELDS_CANT_HAVE_VOID_TYPE),
						getSourceLocation()));
			}
		}

		// no join points for initialization and preinitialization of parameterized types
		// no throwable parameterized types
		if ((kind == Shadow.Initialization) || (kind == Shadow.PreInitialization)) {
			HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getDeclaringType().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_INIT_JPS_FOR_PARAMETERIZED_TYPES),
						getSourceLocation()));
			}

			visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getThrowsPattern().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_GENERIC_THROWABLES), getSourceLocation()));
			}
		}

		// no parameterized types in declaring type position
		// no throwable parameterized types
		if ((kind == Shadow.MethodExecution) || (kind == Shadow.ConstructorExecution)) {
			HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getDeclaringType().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(
						WeaverMessages.format(WeaverMessages.EXECUTION_DOESNT_SUPPORT_PARAMETERIZED_DECLARING_TYPES),
						getSourceLocation()));
			}

			visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getThrowsPattern().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_GENERIC_THROWABLES), getSourceLocation()));
			}
		}

		// no parameterized types in declaring type position
		// no throwable parameterized types
		if ((kind == Shadow.MethodCall) || (kind == Shadow.ConstructorCall)) {
			HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getDeclaringType().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(
						WeaverMessages.format(WeaverMessages.CALL_DOESNT_SUPPORT_PARAMETERIZED_DECLARING_TYPES),
						getSourceLocation()));
			}

			visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
			signature.getThrowsPattern().traverse(visitor, null);
			if (visitor.wellHasItThen/* ? */()) {
				scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_GENERIC_THROWABLES), getSourceLocation()));
			}
			if (!scope.getWorld().isJoinpointArrayConstructionEnabled() && kind == Shadow.ConstructorCall
					&& signature.getDeclaringType().isArray()) {
				scope.message(MessageUtil.warn(WeaverMessages.format(WeaverMessages.NO_NEWARRAY_JOINPOINTS_BY_DEFAULT),
						getSourceLocation()));
			}
		}
	}

	@Override
	protected Test findResidueInternal(Shadow shadow, ExposedState state) {
		return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
	}

	@Override
	public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
		Pointcut ret = new KindedPointcut(kind, signature, bindings.getEnclosingAdvice());
		ret.copyLocationFrom(this);
		return ret;
	}

	@Override
	public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
		Pointcut ret = new KindedPointcut(kind, signature.parameterizeWith(typeVariableMap, w), munger);
		ret.copyLocationFrom(this);
		return ret;
	}

	public Shadow.Kind getKind() {
		return kind;
	}

	@Override
	public Object accept(PatternNodeVisitor visitor, Object data) {
		return visitor.visit(this, data);
	}

	@Override
	public Object traverse(PatternNodeVisitor visitor, Object data) {
		Object ret = accept(visitor, data);
		if (this.signature != null)
			this.signature.traverse(visitor, ret);
		return ret;
	}
}