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
|
/* *******************************************************************
* 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.patterns;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.ISourceLocation;
import org.aspectj.bridge.MessageUtil;
import org.aspectj.util.FuzzyBoolean;
import org.aspectj.weaver.CompressingDataOutputStream;
import org.aspectj.weaver.ISourceContext;
import org.aspectj.weaver.IntMap;
import org.aspectj.weaver.ResolvedType;
import org.aspectj.weaver.Shadow;
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;
import org.aspectj.weaver.ast.Var;
/**
* @author colyer
*
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
* Templates
*/
public class ArgsAnnotationPointcut extends NameBindingPointcut {
private AnnotationPatternList arguments;
private String declarationText;
/**
*
*/
public ArgsAnnotationPointcut(AnnotationPatternList arguments) {
super();
this.arguments = arguments;
this.pointcutKind = ATARGS;
buildDeclarationText();
}
public AnnotationPatternList getArguments() {
return arguments;
}
public int couldMatchKinds() {
return Shadow.ALL_SHADOW_KINDS_BITS; // empty args() matches jps with no args
}
public Pointcut parameterizeWith(Map typeVariableMap, World w) {
ArgsAnnotationPointcut ret = new ArgsAnnotationPointcut(arguments.parameterizeWith(typeVariableMap, w));
ret.copyLocationFrom(this);
return ret;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
*/
public FuzzyBoolean fastMatch(FastMatchInfo info) {
return FuzzyBoolean.MAYBE;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
*/
protected FuzzyBoolean matchInternal(Shadow shadow) {
arguments.resolve(shadow.getIWorld());
FuzzyBoolean ret = arguments.matches(shadow.getIWorld().resolve(shadow.getArgTypes()));
return ret;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope,
* org.aspectj.weaver.patterns.Bindings)
*/
protected void resolveBindings(IScope scope, Bindings bindings) {
if (!scope.getWorld().isInJava5Mode()) {
scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.ATARGS_ONLY_SUPPORTED_AT_JAVA5_LEVEL),
getSourceLocation()));
return;
}
arguments.resolveBindings(scope, bindings, true);
if (arguments.ellipsisCount > 1) {
scope.message(IMessage.ERROR, this, "uses more than one .. in args (compiler limitation)");
}
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedType, org.aspectj.weaver.IntMap)
*/
protected Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
if (isDeclare(bindings.getEnclosingAdvice())) {
// Enforce rule about which designators are supported in declare
inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.ARGS_IN_DECLARE),
bindings.getEnclosingAdvice().getSourceLocation(), null);
return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
}
AnnotationPatternList list = arguments.resolveReferences(bindings);
Pointcut ret = new ArgsAnnotationPointcut(list);
ret.copyLocationFrom(this);
return ret;
}
/*
* (non-Javadoc)
*
* @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
*/
protected Test findResidueInternal(Shadow shadow, ExposedState state) {
int len = shadow.getArgCount();
// do some quick length tests first
int numArgsMatchedByEllipsis = (len + arguments.ellipsisCount) - arguments.size();
if (numArgsMatchedByEllipsis < 0) {
return Literal.FALSE; // should never happen
}
if ((numArgsMatchedByEllipsis > 0) && (arguments.ellipsisCount == 0)) {
return Literal.FALSE; // should never happen
}
// now work through the args and the patterns, skipping at ellipsis
Test ret = Literal.TRUE;
int argsIndex = 0;
for (int i = 0; i < arguments.size(); i++) {
if (arguments.get(i) == AnnotationTypePattern.ELLIPSIS) {
// match ellipsisMatchCount args
argsIndex += numArgsMatchedByEllipsis;
} else if (arguments.get(i) == AnnotationTypePattern.ANY) {
argsIndex++;
} else {
// match the argument type at argsIndex with the ExactAnnotationTypePattern
// we know it is exact because nothing else is allowed in args
ExactAnnotationTypePattern ap = (ExactAnnotationTypePattern) arguments.get(i);
UnresolvedType argType = shadow.getArgType(argsIndex);
ResolvedType rArgType = argType.resolve(shadow.getIWorld());
if (rArgType.isMissing()) {
shadow.getIWorld().getLint().cantFindType.signal(new String[] { WeaverMessages.format(
WeaverMessages.CANT_FIND_TYPE_ARG_TYPE, argType.getName()) }, shadow.getSourceLocation(),
new ISourceLocation[] { getSourceLocation() });
// IMessage msg = new Message(
// WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_ARG_TYPE,argType.getName()),
// "",IMessage.ERROR,shadow.getSourceLocation(),null,new ISourceLocation[]{getSourceLocation()});
}
ResolvedType rAnnType = ap.getAnnotationType().resolve(shadow.getIWorld());
if (ap instanceof BindingAnnotationTypePattern) {
BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern) ap;
Var annvar = shadow.getArgAnnotationVar(argsIndex, rAnnType);
state.set(btp.getFormalIndex(), annvar);
}
if (!ap.matches(rArgType).alwaysTrue()) {
// we need a test...
ret = Test.makeAnd(ret, Test.makeHasAnnotation(shadow.getArgVar(argsIndex), rAnnType));
}
argsIndex++;
}
}
return ret;
}
public List<BindingPattern> getBindingAnnotationTypePatterns() {
List<BindingPattern> l = new ArrayList<>();
AnnotationTypePattern[] pats = arguments.getAnnotationPatterns();
for (AnnotationTypePattern pat : pats) {
if (pat instanceof BindingAnnotationTypePattern) {
l.add((BindingPattern) pat);
}
}
return l;
}
public List<BindingTypePattern> getBindingTypePatterns() {
return Collections.emptyList();
}
public void write(CompressingDataOutputStream s) throws IOException {
s.writeByte(Pointcut.ATARGS);
arguments.write(s);
writeLocation(s);
}
public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
AnnotationPatternList annotationPatternList = AnnotationPatternList.read(s, context);
ArgsAnnotationPointcut ret = new ArgsAnnotationPointcut(annotationPatternList);
ret.readLocation(context, s);
return ret;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (!(obj instanceof ArgsAnnotationPointcut)) {
return false;
}
ArgsAnnotationPointcut other = (ArgsAnnotationPointcut) obj;
return other.arguments.equals(arguments);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return 17 + 37 * arguments.hashCode();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
private void buildDeclarationText() {
StringBuilder buf = new StringBuilder("@args");
buf.append(arguments.toString());
this.declarationText = buf.toString();
}
public String toString() {
return this.declarationText;
}
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.arguments != null)
this.arguments.traverse(visitor, ret);
return ret;
}
}
|