1
/* *******************************************************************
2
* Copyright (c) 2004 IBM Corporation.
4
* This program and the accompanying materials are made available
5
* under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
9
* ******************************************************************/
10
package org.aspectj.weaver.patterns;
12
import java.io.DataOutputStream;
13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.List;
19
import org.aspectj.bridge.IMessage;
20
import org.aspectj.bridge.MessageUtil;
21
import org.aspectj.util.FuzzyBoolean;
22
import org.aspectj.weaver.ISourceContext;
23
import org.aspectj.weaver.IntMap;
24
import org.aspectj.weaver.ResolvedType;
25
import org.aspectj.weaver.Shadow;
26
import org.aspectj.weaver.ShadowMunger;
27
import org.aspectj.weaver.UnresolvedType;
28
import org.aspectj.weaver.VersionedDataInputStream;
29
import org.aspectj.weaver.WeaverMessages;
30
import org.aspectj.weaver.World;
31
import org.aspectj.weaver.ast.Literal;
32
import org.aspectj.weaver.ast.Test;
33
import org.aspectj.weaver.ast.Var;
38
* TODO To change the template for this generated type comment go to
39
* Window - Preferences - Java - Code Style - Code Templates
41
public class ThisOrTargetAnnotationPointcut extends NameBindingPointcut {
43
private boolean isThis;
44
private boolean alreadyWarnedAboutDEoW = false;
45
private ExactAnnotationTypePattern annotationTypePattern;
46
private String declarationText;
48
private static final int thisKindSet;
49
private static final int targetKindSet;
52
int thisFlags = Shadow.ALL_SHADOW_KINDS_BITS;
53
int targFlags = Shadow.ALL_SHADOW_KINDS_BITS;
54
for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
55
Shadow.Kind kind = Shadow.SHADOW_KINDS[i];
56
if (kind.neverHasThis()) thisFlags-=kind.bit;
57
if (kind.neverHasTarget()) targFlags-=kind.bit;
59
thisKindSet = thisFlags;
60
targetKindSet=targFlags;
66
public ThisOrTargetAnnotationPointcut(boolean isThis, ExactAnnotationTypePattern type) {
69
this.annotationTypePattern = type;
70
this.pointcutKind = ATTHIS_OR_TARGET;
71
buildDeclarationText();
74
public ThisOrTargetAnnotationPointcut(boolean isThis, ExactAnnotationTypePattern type, ShadowMunger munger) {
78
public ExactAnnotationTypePattern getAnnotationTypePattern() {
79
return annotationTypePattern;
82
public int couldMatchKinds() {
83
return isThis ? thisKindSet : targetKindSet;
86
public Pointcut parameterizeWith(Map typeVariableMap,World w) {
87
ExactAnnotationTypePattern newPattern = (ExactAnnotationTypePattern) this.annotationTypePattern.parameterizeWith(typeVariableMap,w);
88
if (newPattern.getAnnotationType() instanceof ResolvedType) {
89
verifyRuntimeRetention(newPattern.getResolvedAnnotationType());
91
ThisOrTargetAnnotationPointcut ret = new ThisOrTargetAnnotationPointcut(isThis,(ExactAnnotationTypePattern)annotationTypePattern.parameterizeWith(typeVariableMap,w));
92
ret.copyLocationFrom(this);
97
* @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
99
public FuzzyBoolean fastMatch(FastMatchInfo info) {
100
return FuzzyBoolean.MAYBE;
104
* @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
106
protected FuzzyBoolean matchInternal(Shadow shadow) {
107
if (!couldMatch(shadow)) return FuzzyBoolean.NO;
108
ResolvedType toMatchAgainst =
109
(isThis ? shadow.getThisType() : shadow.getTargetType() ).resolve(shadow.getIWorld());
110
annotationTypePattern.resolve(shadow.getIWorld());
111
if (annotationTypePattern.matchesRuntimeType(toMatchAgainst).alwaysTrue()) {
112
return FuzzyBoolean.YES;
114
// a subtype may match at runtime
115
return FuzzyBoolean.MAYBE;
119
public boolean isThis() { return isThis; }
122
* @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings)
124
protected void resolveBindings(IScope scope, Bindings bindings) {
125
if (!scope.getWorld().isInJava5Mode()) {
126
scope.message(MessageUtil.error(WeaverMessages.format(
127
isThis ? WeaverMessages.ATTHIS_ONLY_SUPPORTED_AT_JAVA5_LEVEL : WeaverMessages.ATTARGET_ONLY_SUPPORTED_AT_JAVA5_LEVEL),
128
getSourceLocation()));
131
annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope,bindings,true);
132
// must be either a Var, or an annotation type pattern
133
// if annotationType does not have runtime retention, this is an error
134
if (annotationTypePattern.annotationType == null) {
135
// it's a formal with a binding error
138
ResolvedType rAnnotationType = (ResolvedType) annotationTypePattern.annotationType;
139
if (rAnnotationType.isTypeVariableReference()) return; // we'll deal with this next check when the type var is actually bound...
140
verifyRuntimeRetention(rAnnotationType);
144
private void verifyRuntimeRetention(ResolvedType rAnnotationType) {
145
if (!(rAnnotationType.isAnnotationWithRuntimeRetention())) {
146
IMessage m = MessageUtil.error(
147
WeaverMessages.format(WeaverMessages.BINDING_NON_RUNTIME_RETENTION_ANNOTATION,rAnnotationType.getName()),
148
getSourceLocation());
149
rAnnotationType.getWorld().getMessageHandler().handleMessage(m);
154
* @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedType, org.aspectj.weaver.IntMap)
156
protected Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
157
if (isDeclare(bindings.getEnclosingAdvice())) {
158
// Enforce rule about which designators are supported in declare
159
if (!alreadyWarnedAboutDEoW) {
160
inAspect.getWorld().showMessage(IMessage.ERROR,
161
WeaverMessages.format(WeaverMessages.THIS_OR_TARGET_IN_DECLARE,isThis?"this":"target"),
162
bindings.getEnclosingAdvice().getSourceLocation(), null);
163
alreadyWarnedAboutDEoW = true;
165
return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
168
ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
169
ThisOrTargetAnnotationPointcut ret =
170
new ThisOrTargetAnnotationPointcut(isThis, newType, bindings.getEnclosingAdvice());
171
ret.alreadyWarnedAboutDEoW = alreadyWarnedAboutDEoW;
172
ret.copyLocationFrom(this);
177
* @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
180
* The guard here is going to be the hasAnnotation() test - if it gets through (which we cannot determine until runtime) then
181
* we must have a TypeAnnotationAccessVar in place - this means we must *always* have one in place.
183
protected Test findResidueInternal(Shadow shadow, ExposedState state) {
184
if (!couldMatch(shadow)) return Literal.FALSE;
185
boolean alwaysMatches = match(shadow).alwaysTrue();
186
Var var = isThis ? shadow.getThisVar() : shadow.getTargetVar();
189
// Are annotations being bound?
190
UnresolvedType annotationType = annotationTypePattern.annotationType;
191
if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
192
BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern)annotationTypePattern;
193
annotationType = btp.annotationType;
195
annVar = isThis ? shadow.getThisAnnotationVar(annotationType) :
196
shadow.getTargetAnnotationVar(annotationType);
198
throw new RuntimeException("Impossible!");
200
state.set(btp.getFormalIndex(),annVar);
203
if (alwaysMatches && (annVar == null)) {//change check to verify if its the 'generic' annVar that is being used
206
ResolvedType rType = annotationType.resolve(shadow.getIWorld());
207
return Test.makeHasAnnotation(var,rType);
212
private boolean couldMatch(Shadow shadow) {
213
return isThis ? shadow.hasThis() : shadow.hasTarget();
218
* @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
220
public List getBindingAnnotationTypePatterns() {
221
if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
222
List l = new ArrayList();
223
l.add(annotationTypePattern);
225
} else return Collections.EMPTY_LIST;
229
* @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
231
public List getBindingTypePatterns() {
232
return Collections.EMPTY_LIST;
236
* @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
238
public void write(DataOutputStream s) throws IOException {
239
s.writeByte(Pointcut.ATTHIS_OR_TARGET);
240
s.writeBoolean(isThis);
241
annotationTypePattern.write(s);
245
public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
246
boolean isThis = s.readBoolean();
247
AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
248
ThisOrTargetAnnotationPointcut ret = new ThisOrTargetAnnotationPointcut(isThis,(ExactAnnotationTypePattern)type);
249
ret.readLocation(context, s);
254
* @see java.lang.Object#equals(java.lang.Object)
256
public boolean equals(Object obj) {
257
if (!(obj instanceof ThisOrTargetAnnotationPointcut)) return false;
258
ThisOrTargetAnnotationPointcut other = (ThisOrTargetAnnotationPointcut) obj;
259
return ( other.annotationTypePattern.equals(this.annotationTypePattern) &&
260
(other.isThis == this.isThis) );
264
* @see java.lang.Object#hashCode()
266
public int hashCode() {
267
return 17 + 37*annotationTypePattern.hashCode() + (isThis ? 49 : 13);
271
* @see java.lang.Object#toString()
273
private void buildDeclarationText() {
274
StringBuffer buf = new StringBuffer();
275
buf.append(isThis ? "@this(" : "@target(");
276
String annPatt = annotationTypePattern.toString();
277
buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
279
this.declarationText = buf.toString();
282
public String toString() { return this.declarationText; }
284
public Object accept(PatternNodeVisitor visitor, Object data) {
285
return visitor.visit(this, data);