You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CompilationAndWeavingContext.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.bridge.context;
  13. import java.lang.ref.WeakReference;
  14. import java.util.HashMap;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. import java.util.Stack;
  18. /**
  19. * @author colyer
  20. * This class is responsible for tracking progress through the various phases of
  21. * compilation and weaving. When an exception occurs (or a message is issued, if
  22. * desired), you can ask this class for a "stack trace" that gives information about
  23. * what the compiler was doing at the time. The trace will say something like:
  24. *
  25. * when matching pointcut xyz
  26. * when matching shadow sss
  27. * when weaving type ABC
  28. * when weaving shadow mungers
  29. *
  30. * Since we can't use ThreadLocal (have to work on 1.3), we use a
  31. * map from Thread -> ContextStack.
  32. */
  33. public class CompilationAndWeavingContext {
  34. private static int nextTokenId = 1;
  35. // unique constants for the different phases that can be registered
  36. // "FRONT END"
  37. public static final int BATCH_BUILD = 0;
  38. public static final int INCREMENTAL_BUILD = 1;
  39. public static final int PROCESSING_COMPILATION_UNIT = 2;
  40. public static final int RESOLVING_COMPILATION_UNIT = 3;
  41. public static final int ANALYSING_COMPILATION_UNIT = 4;
  42. public static final int GENERATING_UNWOVEN_CODE_FOR_COMPILATION_UNIT = 5;
  43. public static final int COMPLETING_TYPE_BINDINGS = 6;
  44. public static final int PROCESSING_DECLARE_PARENTS = 7;
  45. public static final int CHECK_AND_SET_IMPORTS = 8;
  46. public static final int CONNECTING_TYPE_HIERARCHY = 9;
  47. public static final int BUILDING_FIELDS_AND_METHODS = 10;
  48. public static final int COLLECTING_ITDS_AND_DECLARES = 11;
  49. public static final int PROCESSING_DECLARE_ANNOTATIONS = 12;
  50. public static final int WEAVING_INTERTYPE_DECLARATIONS = 13;
  51. public static final int RESOLVING_POINTCUT_DECLARATIONS = 14;
  52. public static final int ADDING_DECLARE_WARNINGS_AND_ERRORS = 15;
  53. public static final int VALIDATING_AT_ASPECTJ_ANNOTATIONS = 16;
  54. public static final int ACCESS_FOR_INLINE = 17;
  55. public static final int ADDING_AT_ASPECTJ_ANNOTATIONS = 18;
  56. public static final int FIXING_SUPER_CALLS_IN_ITDS = 19;
  57. public static final int FIXING_SUPER_CALLS = 20;
  58. public static final int OPTIMIZING_THIS_JOIN_POINT_CALLS = 21;
  59. // "BACK END"
  60. public static final int WEAVING = 22;
  61. public static final int PROCESSING_REWEAVABLE_STATE = 23;
  62. public static final int PROCESSING_TYPE_MUNGERS = 24;
  63. public static final int WEAVING_ASPECTS = 25;
  64. public static final int WEAVING_CLASSES = 26;
  65. public static final int WEAVING_TYPE = 27;
  66. public static final int MATCHING_SHADOW = 28;
  67. public static final int IMPLEMENTING_ON_SHADOW = 29;
  68. public static final int MATCHING_POINTCUT = 30;
  69. public static final int MUNGING_WITH = 31;
  70. public static final int PROCESSING_ATASPECTJTYPE_MUNGERS_ONLY = 32;
  71. // phase names
  72. public static final String[] PHASE_NAMES = new String[] {
  73. "batch building",
  74. "incrementally building",
  75. "processing compilation unit",
  76. "resolving types defined in compilation unit",
  77. "analysing types defined in compilation unit",
  78. "generating unwoven code for type defined in compilation unit",
  79. "completing type bindings",
  80. "processing declare parents",
  81. "checking and setting imports",
  82. "connecting type hierarchy",
  83. "building fields and methods",
  84. "collecting itds and declares",
  85. "processing declare annotations",
  86. "weaving intertype declarations",
  87. "resolving pointcut declarations",
  88. "adding declare warning and errors",
  89. "validating @AspectJ annotations",
  90. "creating accessors for inlining",
  91. "adding @AspectJ annotations",
  92. "fixing super calls in ITDs in interface context",
  93. "fixing super calls in ITDs",
  94. "optimizing thisJoinPoint calls",
  95. // BACK END
  96. "weaving",
  97. "processing reweavable state",
  98. "processing type mungers",
  99. "weaving aspects",
  100. "weaving classes",
  101. "weaving type",
  102. "matching shadow",
  103. "implementing on shadow",
  104. "matching pointcut",
  105. "type munging with",
  106. "type munging for @AspectJ aspectOf"
  107. };
  108. // context stacks, one per thread
  109. private static Map contextMap = new HashMap();
  110. // single thread mode stack
  111. private static Stack contextStack = new Stack();
  112. // formatters, by phase id
  113. private static Map formatterMap = new HashMap();
  114. private static ContextFormatter defaultFormatter = new DefaultFormatter();
  115. private static boolean multiThreaded = true;
  116. /**
  117. * this is a static service
  118. */
  119. private CompilationAndWeavingContext() {
  120. }
  121. // for testing...
  122. public static void reset() {
  123. contextMap = new HashMap();
  124. contextStack = new Stack();
  125. formatterMap = new HashMap();
  126. nextTokenId = 1;
  127. }
  128. public static void setMultiThreaded(boolean mt) { multiThreaded = mt; }
  129. public static void registerFormatter(int phaseId, ContextFormatter aFormatter) {
  130. formatterMap.put(new Integer(phaseId),aFormatter);
  131. }
  132. /**
  133. * Returns a string description of what the compiler/weaver is currently doing
  134. */
  135. public static String getCurrentContext() {
  136. Stack contextStack = getContextStack();
  137. Stack explanationStack = new Stack();
  138. for (Iterator iter = contextStack.iterator(); iter.hasNext();) {
  139. ContextStackEntry entry = (ContextStackEntry) iter.next();
  140. Object data = entry.getData();
  141. if (data != null) {
  142. explanationStack.push(getFormatter(entry).formatEntry(entry.phaseId,data));
  143. }
  144. }
  145. StringBuffer sb = new StringBuffer();
  146. while (!explanationStack.isEmpty()) {
  147. sb.append("when ");
  148. sb.append(explanationStack.pop().toString());
  149. sb.append("\n");
  150. }
  151. return sb.toString();
  152. }
  153. public static ContextToken enteringPhase(int phaseId, Object data) {
  154. Stack contextStack = getContextStack();
  155. ContextTokenImpl nextToken = nextToken();
  156. contextStack.push(new ContextStackEntry(nextToken,phaseId,new WeakReference(data)));
  157. return nextToken;
  158. }
  159. /**
  160. * Exit a phase, all stack entries from the one with the given token
  161. * down will be removed.
  162. */
  163. public static void leavingPhase(ContextToken aToken) {
  164. Stack contextStack = getContextStack();
  165. while (!contextStack.isEmpty()) {
  166. ContextStackEntry entry = (ContextStackEntry) contextStack.pop();
  167. if (entry.contextToken == aToken) break;
  168. }
  169. }
  170. private static Stack getContextStack() {
  171. if (!multiThreaded) {
  172. return contextStack;
  173. }
  174. else {
  175. if (contextMap.containsKey(Thread.currentThread())) {
  176. return (Stack) contextMap.get(Thread.currentThread());
  177. } else {
  178. Stack contextStack = new Stack();
  179. contextMap.put(Thread.currentThread(),contextStack);
  180. return contextStack;
  181. }
  182. }
  183. }
  184. private static ContextTokenImpl nextToken() {
  185. return new ContextTokenImpl(nextTokenId++);
  186. }
  187. private static ContextFormatter getFormatter(ContextStackEntry entry) {
  188. Integer key = new Integer(entry.phaseId);
  189. if (formatterMap.containsKey(key)) {
  190. return (ContextFormatter) formatterMap.get(key);
  191. } else {
  192. return defaultFormatter;
  193. }
  194. }
  195. private static class ContextTokenImpl implements ContextToken {
  196. public int tokenId;
  197. public ContextTokenImpl(int id) { this.tokenId = id; }
  198. }
  199. // dumb data structure
  200. private static class ContextStackEntry {
  201. public ContextTokenImpl contextToken;
  202. public int phaseId;
  203. private WeakReference dataRef;
  204. public ContextStackEntry(ContextTokenImpl ct, int phase, WeakReference data) {
  205. this.contextToken = ct;
  206. this.phaseId = phase;
  207. this.dataRef = data;
  208. }
  209. public Object getData() {
  210. return dataRef.get();
  211. }
  212. public String toString() {
  213. Object data = getData();
  214. if (data == null) {
  215. return "referenced context entry has gone out of scope";
  216. } else {
  217. return CompilationAndWeavingContext.getFormatter(this).formatEntry(phaseId, data);
  218. }
  219. }
  220. }
  221. private static class DefaultFormatter implements ContextFormatter {
  222. public String formatEntry(int phaseId, Object data) {
  223. StringBuffer sb = new StringBuffer();
  224. sb.append(PHASE_NAMES[phaseId]);
  225. sb.append(" ");
  226. if (data instanceof char[]) {
  227. sb.append(new String((char[])data));
  228. } else {
  229. try {
  230. sb.append(data.toString());
  231. } catch (RuntimeException ex) {
  232. // don't lose vital info because of bad toString
  233. sb.append("** broken toString in data object **");
  234. }
  235. }
  236. return sb.toString();
  237. }
  238. }
  239. }