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 7.6KB

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