summaryrefslogtreecommitdiffstats
path: root/bridge/src/org/aspectj/bridge/context/CompilationAndWeavingContext.java
blob: f011572d91819fb11840d390bfeac51a7c1d3415 (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
/* *******************************************************************
 * Copyright (c) 2005 Contributors.
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *   Adrian Colyer			Initial implementation
 * ******************************************************************/
package org.aspectj.bridge.context;

import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author colyer This class is responsible for tracking progress through the various phases of compilation and weaving. When an
 *         exception occurs (or a message is issued, if desired), you can ask this class for a "stack trace" that gives information
 *         about what the compiler was doing at the time. The trace will say something like:
 * 
 *         when matching pointcut xyz when matching shadow sss when weaving type ABC when weaving shadow mungers
 * 
 *         Since we can't use ThreadLocal (have to work on 1.3), we use a map from Thread -> ContextStack.
 */
public class CompilationAndWeavingContext {

	private static int nextTokenId = 1;

	// unique constants for the different phases that can be registered

	// "FRONT END"
	public static final int BATCH_BUILD = 0;
	public static final int INCREMENTAL_BUILD = 1;
	public static final int PROCESSING_COMPILATION_UNIT = 2;
	public static final int RESOLVING_COMPILATION_UNIT = 3;
	public static final int ANALYSING_COMPILATION_UNIT = 4;
	public static final int GENERATING_UNWOVEN_CODE_FOR_COMPILATION_UNIT = 5;
	public static final int COMPLETING_TYPE_BINDINGS = 6;
	public static final int PROCESSING_DECLARE_PARENTS = 7;
	public static final int CHECK_AND_SET_IMPORTS = 8;
	public static final int CONNECTING_TYPE_HIERARCHY = 9;
	public static final int BUILDING_FIELDS_AND_METHODS = 10;
	public static final int COLLECTING_ITDS_AND_DECLARES = 11;
	public static final int PROCESSING_DECLARE_ANNOTATIONS = 12;
	public static final int WEAVING_INTERTYPE_DECLARATIONS = 13;
	public static final int RESOLVING_POINTCUT_DECLARATIONS = 14;
	public static final int ADDING_DECLARE_WARNINGS_AND_ERRORS = 15;
	public static final int VALIDATING_AT_ASPECTJ_ANNOTATIONS = 16;
	public static final int ACCESS_FOR_INLINE = 17;
	public static final int ADDING_AT_ASPECTJ_ANNOTATIONS = 18;
	public static final int FIXING_SUPER_CALLS_IN_ITDS = 19;
	public static final int FIXING_SUPER_CALLS = 20;
	public static final int OPTIMIZING_THIS_JOIN_POINT_CALLS = 21;

	// "BACK END"

	public static final int WEAVING = 22;
	public static final int PROCESSING_REWEAVABLE_STATE = 23;
	public static final int PROCESSING_TYPE_MUNGERS = 24;
	public static final int WEAVING_ASPECTS = 25;
	public static final int WEAVING_CLASSES = 26;
	public static final int WEAVING_TYPE = 27;
	public static final int MATCHING_SHADOW = 28;
	public static final int IMPLEMENTING_ON_SHADOW = 29;
	public static final int MATCHING_POINTCUT = 30;
	public static final int MUNGING_WITH = 31;
	public static final int PROCESSING_ATASPECTJTYPE_MUNGERS_ONLY = 32;

	// phase names
	public static final String[] PHASE_NAMES = new String[] { "batch building", "incrementally building",
			"processing compilation unit", "resolving types defined in compilation unit",
			"analysing types defined in compilation unit", "generating unwoven code for type defined in compilation unit",
			"completing type bindings", "processing declare parents", "checking and setting imports", "connecting type hierarchy",
			"building fields and methods", "collecting itds and declares", "processing declare annotations",
			"weaving intertype declarations", "resolving pointcut declarations", "adding declare warning and errors",
			"validating @AspectJ annotations", "creating accessors for inlining", "adding @AspectJ annotations",
			"fixing super calls in ITDs in interface context", "fixing super calls in ITDs",
			"optimizing thisJoinPoint calls",

			// BACK END

			"weaving", "processing reweavable state", "processing type mungers", "weaving aspects", "weaving classes",
			"weaving type", "matching shadow", "implementing on shadow", "matching pointcut", "type munging with",
			"type munging for @AspectJ aspectOf" };

	// context stacks, one per thread
	private static Map<Thread, Stack<ContextStackEntry>> contextMap = Collections
			.synchronizedMap(new HashMap<Thread, Stack<ContextStackEntry>>());

	// single thread mode stack
	private static Stack<ContextStackEntry> contextStack = new Stack<ContextStackEntry>();

	// formatters, by phase id
	private static Map<Integer, ContextFormatter> formatterMap = new HashMap<Integer, ContextFormatter>();

	private static ContextFormatter defaultFormatter = new DefaultFormatter();

	private static boolean multiThreaded = true;

	/**
	 * this is a static service
	 */
	private CompilationAndWeavingContext() {
	}

	public static void reset() {
		if (!multiThreaded) {
			contextMap.clear();
			contextStack.clear();
			formatterMap.clear();
			nextTokenId = 1;
		} else {
			contextMap.remove(Thread.currentThread());
			// TODO what about formatterMap?
			// TODO what about nextTokenId?
		}
	}

	public static void setMultiThreaded(boolean mt) {
		multiThreaded = mt;
	}

	public static void registerFormatter(int phaseId, ContextFormatter aFormatter) {
		formatterMap.put(new Integer(phaseId), aFormatter);
	}

	/**
	 * Returns a string description of what the compiler/weaver is currently doing
	 */
	public static String getCurrentContext() {
		Stack<ContextStackEntry> contextStack = getContextStack();
		Stack<String> explanationStack = new Stack<String>();
		for (ContextStackEntry entry : contextStack) {
			Object data = entry.getData();
			if (data != null) {
				explanationStack.push(getFormatter(entry).formatEntry(entry.phaseId, data));
			}
		}
		StringBuffer sb = new StringBuffer();
		while (!explanationStack.isEmpty()) {
			sb.append("when ");
			sb.append(explanationStack.pop().toString());
			sb.append("\n");
		}
		return sb.toString();
	}

	public static ContextToken enteringPhase(int phaseId, Object data) {
		Stack<ContextStackEntry> contextStack = getContextStack();
		ContextTokenImpl nextToken = nextToken();
		contextStack.push(new ContextStackEntry(nextToken, phaseId, new WeakReference<Object>(data)));
		return nextToken;
	}

	/**
	 * Exit a phase, all stack entries from the one with the given token down will be removed.
	 */
	public static void leavingPhase(ContextToken aToken) {
		Stack contextStack = getContextStack();
		while (!contextStack.isEmpty()) {
			ContextStackEntry entry = (ContextStackEntry) contextStack.pop();
			if (entry.contextToken == aToken) {
				break;
			}
		}
	}

	/**
	 * Forget about the context for the current thread
	 */
	public static void resetForThread() {
		if (!multiThreaded) {
			return;
		}
		contextMap.remove(Thread.currentThread());
	}

	private static Stack<ContextStackEntry> getContextStack() {
		if (!multiThreaded) {
			return contextStack;
		} else {
			Stack<ContextStackEntry> contextStack = contextMap.get(Thread.currentThread());
			if (contextStack == null) {
				contextStack = new Stack<ContextStackEntry>();
				contextMap.put(Thread.currentThread(), contextStack);
			}
			return contextStack;
		}
	}

	private static ContextTokenImpl nextToken() {
		return new ContextTokenImpl(nextTokenId++);
	}

	private static ContextFormatter getFormatter(ContextStackEntry entry) {
		Integer key = new Integer(entry.phaseId);
		if (formatterMap.containsKey(key)) {
			return formatterMap.get(key);
		} else {
			return defaultFormatter;
		}
	}

	private static class ContextTokenImpl implements ContextToken {
		public int tokenId;

		public ContextTokenImpl(int id) {
			this.tokenId = id;
		}
	}

	// dumb data structure
	private static class ContextStackEntry {
		public ContextTokenImpl contextToken;
		public int phaseId;
		private WeakReference<Object> dataRef;

		public ContextStackEntry(ContextTokenImpl ct, int phase, WeakReference<Object> data) {
			this.contextToken = ct;
			this.phaseId = phase;
			this.dataRef = data;
		}

		public Object getData() {
			return dataRef.get();
		}

		public String toString() {
			Object data = getData();
			if (data == null) {
				return "referenced context entry has gone out of scope";
			} else {
				return CompilationAndWeavingContext.getFormatter(this).formatEntry(phaseId, data);
			}
		}
	}

	private static class DefaultFormatter implements ContextFormatter {

		public String formatEntry(int phaseId, Object data) {
			StringBuffer sb = new StringBuffer();
			sb.append(PHASE_NAMES[phaseId]);
			sb.append(" ");
			if (data instanceof char[]) {
				sb.append(new String((char[]) data));
			} else {
				try {
					sb.append(data.toString());
				} catch (RuntimeException ex) {
					// don't lose vital info because of bad toString
					sb.append("** broken toString in data object **");
				}
			}
			return sb.toString();
		}

	}
}