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.6KB

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