aboutsummaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/CflowPointcut.java
blob: 4dbc87a8221749093a2175eb2a37d4214e0613e2 (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
/* *******************************************************************
 * 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 java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.aspectj.bridge.IMessage;
import org.aspectj.util.FileUtil;
import org.aspectj.util.FuzzyBoolean;
import org.aspectj.weaver.Advice;
import org.aspectj.weaver.CompressingDataOutputStream;
import org.aspectj.weaver.CrosscuttingMembers;
import org.aspectj.weaver.ISourceContext;
import org.aspectj.weaver.IntMap;
import org.aspectj.weaver.Member;
import org.aspectj.weaver.NameMangler;
import org.aspectj.weaver.ResolvedMember;
import org.aspectj.weaver.ResolvedMemberImpl;
import org.aspectj.weaver.ResolvedPointcutDefinition;
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.Test;
import org.aspectj.weaver.patterns.ConcreteCflowPointcut.Slot;

public class CflowPointcut extends Pointcut {
	private final Pointcut entry; // The pointcut inside the cflow() that
	// represents the 'entry' point
	boolean isBelow;// Is this cflowbelow?
	private int[] freeVars;

	/**
	 * Used to indicate that we're in the context of a cflow when concretizing if's
	 *
	 * Will be removed or replaced with something better when we handle this as a non-error
	 */
	public static final ResolvedPointcutDefinition CFLOW_MARKER = new ResolvedPointcutDefinition(null, 0, null,
			UnresolvedType.NONE, Pointcut.makeMatchesNothing(Pointcut.RESOLVED));

	public CflowPointcut(Pointcut entry, boolean isBelow, int[] freeVars) {
		// System.err.println("Building cflow pointcut "+entry.toString());
		this.entry = entry;
		this.isBelow = isBelow;
		this.freeVars = freeVars;
		pointcutKind = CFLOW;
	}

	public boolean isCflowBelow() {
		return isBelow;
	}

	public int couldMatchKinds() {
		return Shadow.ALL_SHADOW_KINDS_BITS;
	}

	// enh 76055
	public Pointcut getEntry() {
		return entry;
	}

	public FuzzyBoolean fastMatch(FastMatchInfo type) {
		return FuzzyBoolean.MAYBE;
	}

	protected FuzzyBoolean matchInternal(Shadow shadow) {
		// ??? this is not maximally efficient
		return FuzzyBoolean.MAYBE;
	}

	public void write(CompressingDataOutputStream s) throws IOException {
		s.writeByte(Pointcut.CFLOW);
		entry.write(s);
		s.writeBoolean(isBelow);
		FileUtil.writeIntArray(freeVars, s);
		writeLocation(s);
	}

	public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {

		CflowPointcut ret = new CflowPointcut(Pointcut.read(s, context), s.readBoolean(), FileUtil.readIntArray(s));
		ret.readLocation(context, s);
		return ret;
	}

	public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
		CflowPointcut ret = new CflowPointcut(entry.parameterizeWith(typeVariableMap, w), isBelow, freeVars);
		ret.copyLocationFrom(this);
		return ret;
	}

	public void resolveBindings(IScope scope, Bindings bindings) {
		if (bindings == null) {
			entry.resolveBindings(scope, null);
			entry.state = RESOLVED;
			freeVars = new int[0];
		} else {
			// ??? for if's sake we might need to be more careful here
			Bindings entryBindings = new Bindings(bindings.size());

			entry.resolveBindings(scope, entryBindings);
			entry.state = RESOLVED;

			freeVars = entryBindings.getUsedFormals();

			bindings.mergeIn(entryBindings, scope);
		}
	}

	public boolean equals(Object other) {
		if (!(other instanceof CflowPointcut)) {
			return false;
		}
		CflowPointcut o = (CflowPointcut) other;
		return o.entry.equals(entry) && o.isBelow == isBelow;
	}

	public int hashCode() {
		int result = 17;
		result = 37 * result + entry.hashCode();
		result = 37 * result + (isBelow ? 0 : 1);
		return result;
	}

	public String toString() {
		return "cflow" + (isBelow ? "below" : "") + "(" + entry + ")";
	}

	protected Test findResidueInternal(Shadow shadow, ExposedState state) {
		throw new RuntimeException("unimplemented - did concretization fail?");
	}

	public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {

		// the pointcut is marked as CONCRETE after returning from this
		// call - so we can't skip concretization
		// if (this.entry.state == Pointcut.SYMBOLIC) {
		// // too early to concretize, return unchanged
		// return this;
		// }

		// Enforce rule about which designators are supported in declare
		if (isDeclare(bindings.getEnclosingAdvice())) {
			inAspect.getWorld().showMessage(IMessage.ERROR,
					WeaverMessages.format(WeaverMessages.CFLOW_IN_DECLARE, isBelow ? "below" : ""),
					bindings.getEnclosingAdvice().getSourceLocation(), null);
			return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
		}

		// make this remap from formal positions to arrayIndices
		IntMap entryBindings = new IntMap();
		if (freeVars != null) {
			for (int i = 0, len = freeVars.length; i < len; i++) {
				int freeVar = freeVars[i];
				// int formalIndex = bindings.get(freeVar);
				entryBindings.put(freeVar, i);
			}
		}
		entryBindings.copyContext(bindings);
		// System.out.println(this + " bindings: " + entryBindings);

		World world = inAspect.getWorld();

		Pointcut concreteEntry;

		ResolvedType concreteAspect = bindings.getConcreteAspect();

		CrosscuttingMembers xcut = concreteAspect.crosscuttingMembers;
		Collection<ShadowMunger> previousCflowEntries = xcut.getCflowEntries();

		entryBindings.pushEnclosingDefinition(CFLOW_MARKER);
		// This block concretizes the pointcut within the cflow pointcut
		try {
			concreteEntry = entry.concretize(inAspect, declaringType, entryBindings);
		} finally {
			entryBindings.popEnclosingDefinitition();
		}

		List<ShadowMunger> innerCflowEntries = new ArrayList<>(xcut.getCflowEntries());
		innerCflowEntries.removeAll(previousCflowEntries);

		// Four routes of interest through this code (did I hear someone say
		// refactor??)
		// 1) no state in the cflow - we can use a counter *and* we have seen
		// this pointcut
		// before - so use the same counter as before.
		// 2) no state in the cflow - we can use a counter, but this is the
		// first time
		// we have seen this pointcut, so build the infrastructure.
		// 3) state in the cflow - we need to use a stack *and* we have seen
		// this pointcut
		// before - so share the stack.
		// 4) state in the cflow - we need to use a stack, but this is the first
		// time
		// we have seen this pointcut, so build the infrastructure.

		if (freeVars == null || freeVars.length == 0) { // No state, so don't
			// use a stack, use a
			// counter.
			ResolvedMember localCflowField = null;

			Object field = getCflowfield(xcut, concreteEntry, concreteAspect, "counter");

			// Check if we have already got a counter for this cflow pointcut
			if (field != null) {
				localCflowField = (ResolvedMember) field; // Use the one we
				// already have

			} else {

				// Create a counter field in the aspect
				localCflowField = new ResolvedMemberImpl(Member.FIELD, concreteAspect, Modifier.STATIC | Modifier.PUBLIC,
						NameMangler.cflowCounter(xcut), UnresolvedType.forName(NameMangler.CFLOW_COUNTER_TYPE)
						.getSignature());

				// Create type munger to add field to the aspect
				concreteAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().makeCflowCounterFieldAdder(
						localCflowField));

				// Create shadow munger to push stuff onto the stack
				concreteAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makeCflowEntry(world, concreteEntry, isBelow,
						localCflowField, freeVars == null ? 0 : freeVars.length, innerCflowEntries, inAspect));

				putCflowfield(xcut, concreteEntry, concreteAspect, localCflowField, "counter"); // Remember
				// it
			}

			Pointcut ret = new ConcreteCflowPointcut(concreteAspect, localCflowField, null, true);
			ret.copyLocationFrom(this);
			return ret;
		} else {

			List<Slot> slots = new ArrayList<>();

			for (int i = 0, len = freeVars.length; i < len; i++) {
				int freeVar = freeVars[i];

				// we don't need to keep state that isn't actually exposed to
				// advice
				// ??? this means that we will store some state that we won't
				// actually use, optimize this later
				if (!bindings.hasKey(freeVar)) {
					continue;
				}

				int formalIndex = bindings.get(freeVar);

				// We need to look in the right place for the type of the
				// formal. Suppose the advice looks like this:
				// before(String s): somePointcut(*,s)
				// where the first argument in somePointcut is of type Number
				// for free variable 0 we want to ask the pointcut for the type
				// of its first argument, if we only
				// ask the advice for the type of its first argument then we'll
				// get the wrong type (pr86903)

				ResolvedPointcutDefinition enclosingDef = bindings.peekEnclosingDefinition();
				ResolvedType formalType = null;

				// Is there a useful enclosing pointcut?
				if (enclosingDef != null && enclosingDef.getParameterTypes().length > 0) {
					formalType = enclosingDef.getParameterTypes()[freeVar].resolve(world);
				} else {
					formalType = bindings.getAdviceSignature().getParameterTypes()[formalIndex].resolve(world);
				}

				ConcreteCflowPointcut.Slot slot = new ConcreteCflowPointcut.Slot(formalIndex, formalType, i);
				slots.add(slot);
			}
			ResolvedMember localCflowField = null;
			Object field = getCflowfield(xcut, concreteEntry, concreteAspect, "stack");
			if (field != null) {
				localCflowField = (ResolvedMember) field;
			} else {

				localCflowField = new ResolvedMemberImpl(Member.FIELD, concreteAspect, Modifier.STATIC | Modifier.PUBLIC,
						NameMangler.cflowStack(xcut), UnresolvedType.forName(NameMangler.CFLOW_STACK_TYPE)
						.getSignature());
				// System.out.println("adding field to: " + inAspect + " field "
				// + cflowField);

				// add field and initializer to inAspect
				// XXX and then that info above needs to be mapped down here to
				// help with
				// XXX getting the exposed state right
				concreteAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makeCflowEntry(world, concreteEntry, isBelow,
						localCflowField, freeVars.length, innerCflowEntries, inAspect));

				concreteAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport()
						.makeCflowStackFieldAdder(localCflowField));
				putCflowfield(xcut, concreteEntry, concreteAspect, localCflowField, "stack");
			}
			Pointcut ret = new ConcreteCflowPointcut(concreteAspect, localCflowField, slots, false);
			ret.copyLocationFrom(this);
			return ret;
		}

	}

	private String getKey(Pointcut p, ResolvedType a, String stackOrCounter) {
		StringBuilder sb = new StringBuilder();
		sb.append(a.getName());
		sb.append("::");
		sb.append(p.toString());
		sb.append("::");
		sb.append(stackOrCounter);
		return sb.toString();
	}

	private Object getCflowfield(CrosscuttingMembers xcut, Pointcut pcutkey, ResolvedType concreteAspect, String stackOrCounter) {
		String key = getKey(pcutkey, concreteAspect, stackOrCounter);
		Object o = null;
		if (isBelow) {
			o = xcut.getCflowBelowFields().get(key);
		} else {
			o = xcut.getCflowFields().get(key);
		}
		// System.err.println("Retrieving for key "+key+" returning "+o);
		return o;
	}

	private void putCflowfield(CrosscuttingMembers xcut, Pointcut pcutkey, ResolvedType concreteAspect, Object o,
			String stackOrCounter) {
		String key = getKey(pcutkey, concreteAspect, stackOrCounter);
		// System.err.println("Storing cflow field for key"+key);
		if (isBelow) {
			xcut.getCflowBelowFields().put(key, o);
		} else {
			xcut.getCflowFields().put(key, o);
		}
	}

	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.entry != null)
			this.entry.traverse(visitor, ret);
		return ret;
	}
}