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.

CflowPointcut.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.lang.reflect.Modifier;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.util.FileUtil;
  21. import org.aspectj.util.FuzzyBoolean;
  22. import org.aspectj.weaver.Advice;
  23. import org.aspectj.weaver.CompressingDataOutputStream;
  24. import org.aspectj.weaver.CrosscuttingMembers;
  25. import org.aspectj.weaver.ISourceContext;
  26. import org.aspectj.weaver.IntMap;
  27. import org.aspectj.weaver.Member;
  28. import org.aspectj.weaver.NameMangler;
  29. import org.aspectj.weaver.ResolvedMember;
  30. import org.aspectj.weaver.ResolvedMemberImpl;
  31. import org.aspectj.weaver.ResolvedPointcutDefinition;
  32. import org.aspectj.weaver.ResolvedType;
  33. import org.aspectj.weaver.Shadow;
  34. import org.aspectj.weaver.ShadowMunger;
  35. import org.aspectj.weaver.UnresolvedType;
  36. import org.aspectj.weaver.VersionedDataInputStream;
  37. import org.aspectj.weaver.WeaverMessages;
  38. import org.aspectj.weaver.World;
  39. import org.aspectj.weaver.ast.Test;
  40. public class CflowPointcut extends Pointcut {
  41. private final Pointcut entry; // The pointcut inside the cflow() that
  42. // represents the 'entry' point
  43. boolean isBelow;// Is this cflowbelow?
  44. private int[] freeVars;
  45. /**
  46. * Used to indicate that we're in the context of a cflow when concretizing if's
  47. *
  48. * Will be removed or replaced with something better when we handle this as a non-error
  49. */
  50. public static final ResolvedPointcutDefinition CFLOW_MARKER = new ResolvedPointcutDefinition(null, 0, null,
  51. UnresolvedType.NONE, Pointcut.makeMatchesNothing(Pointcut.RESOLVED));
  52. public CflowPointcut(Pointcut entry, boolean isBelow, int[] freeVars) {
  53. // System.err.println("Building cflow pointcut "+entry.toString());
  54. this.entry = entry;
  55. this.isBelow = isBelow;
  56. this.freeVars = freeVars;
  57. pointcutKind = CFLOW;
  58. }
  59. /**
  60. * @return Returns true is this is a cflowbelow pointcut
  61. */
  62. public boolean isCflowBelow() {
  63. return isBelow;
  64. }
  65. public int couldMatchKinds() {
  66. return Shadow.ALL_SHADOW_KINDS_BITS;
  67. }
  68. // enh 76055
  69. public Pointcut getEntry() {
  70. return entry;
  71. }
  72. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  73. return FuzzyBoolean.MAYBE;
  74. }
  75. protected FuzzyBoolean matchInternal(Shadow shadow) {
  76. // ??? this is not maximally efficient
  77. return FuzzyBoolean.MAYBE;
  78. }
  79. public void write(CompressingDataOutputStream s) throws IOException {
  80. s.writeByte(Pointcut.CFLOW);
  81. entry.write(s);
  82. s.writeBoolean(isBelow);
  83. FileUtil.writeIntArray(freeVars, s);
  84. writeLocation(s);
  85. }
  86. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  87. CflowPointcut ret = new CflowPointcut(Pointcut.read(s, context), s.readBoolean(), FileUtil.readIntArray(s));
  88. ret.readLocation(context, s);
  89. return ret;
  90. }
  91. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  92. CflowPointcut ret = new CflowPointcut(entry.parameterizeWith(typeVariableMap, w), isBelow, freeVars);
  93. ret.copyLocationFrom(this);
  94. return ret;
  95. }
  96. public void resolveBindings(IScope scope, Bindings bindings) {
  97. if (bindings == null) {
  98. entry.resolveBindings(scope, null);
  99. entry.state = RESOLVED;
  100. freeVars = new int[0];
  101. } else {
  102. // ??? for if's sake we might need to be more careful here
  103. Bindings entryBindings = new Bindings(bindings.size());
  104. entry.resolveBindings(scope, entryBindings);
  105. entry.state = RESOLVED;
  106. freeVars = entryBindings.getUsedFormals();
  107. bindings.mergeIn(entryBindings, scope);
  108. }
  109. }
  110. public boolean equals(Object other) {
  111. if (!(other instanceof CflowPointcut)) {
  112. return false;
  113. }
  114. CflowPointcut o = (CflowPointcut) other;
  115. return o.entry.equals(entry) && o.isBelow == isBelow;
  116. }
  117. public int hashCode() {
  118. int result = 17;
  119. result = 37 * result + entry.hashCode();
  120. result = 37 * result + (isBelow ? 0 : 1);
  121. return result;
  122. }
  123. public String toString() {
  124. return "cflow" + (isBelow ? "below" : "") + "(" + entry + ")";
  125. }
  126. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  127. throw new RuntimeException("unimplemented - did concretization fail?");
  128. }
  129. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  130. // the pointcut is marked as CONCRETE after returning from this
  131. // call - so we can't skip concretization
  132. // if (this.entry.state == Pointcut.SYMBOLIC) {
  133. // // too early to concretize, return unchanged
  134. // return this;
  135. // }
  136. // Enforce rule about which designators are supported in declare
  137. if (isDeclare(bindings.getEnclosingAdvice())) {
  138. inAspect.getWorld().showMessage(IMessage.ERROR,
  139. WeaverMessages.format(WeaverMessages.CFLOW_IN_DECLARE, isBelow ? "below" : ""),
  140. bindings.getEnclosingAdvice().getSourceLocation(), null);
  141. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  142. }
  143. // make this remap from formal positions to arrayIndices
  144. IntMap entryBindings = new IntMap();
  145. if (freeVars != null) {
  146. for (int i = 0, len = freeVars.length; i < len; i++) {
  147. int freeVar = freeVars[i];
  148. // int formalIndex = bindings.get(freeVar);
  149. entryBindings.put(freeVar, i);
  150. }
  151. }
  152. entryBindings.copyContext(bindings);
  153. // System.out.println(this + " bindings: " + entryBindings);
  154. World world = inAspect.getWorld();
  155. Pointcut concreteEntry;
  156. ResolvedType concreteAspect = bindings.getConcreteAspect();
  157. CrosscuttingMembers xcut = concreteAspect.crosscuttingMembers;
  158. Collection<ShadowMunger> previousCflowEntries = xcut.getCflowEntries();
  159. entryBindings.pushEnclosingDefinition(CFLOW_MARKER);
  160. // This block concretizes the pointcut within the cflow pointcut
  161. try {
  162. concreteEntry = entry.concretize(inAspect, declaringType, entryBindings);
  163. } finally {
  164. entryBindings.popEnclosingDefinitition();
  165. }
  166. List<ShadowMunger> innerCflowEntries = new ArrayList<ShadowMunger>(xcut.getCflowEntries());
  167. innerCflowEntries.removeAll(previousCflowEntries);
  168. // Four routes of interest through this code (did I hear someone say
  169. // refactor??)
  170. // 1) no state in the cflow - we can use a counter *and* we have seen
  171. // this pointcut
  172. // before - so use the same counter as before.
  173. // 2) no state in the cflow - we can use a counter, but this is the
  174. // first time
  175. // we have seen this pointcut, so build the infrastructure.
  176. // 3) state in the cflow - we need to use a stack *and* we have seen
  177. // this pointcut
  178. // before - so share the stack.
  179. // 4) state in the cflow - we need to use a stack, but this is the first
  180. // time
  181. // we have seen this pointcut, so build the infrastructure.
  182. if (freeVars == null || freeVars.length == 0) { // No state, so don't
  183. // use a stack, use a
  184. // counter.
  185. ResolvedMember localCflowField = null;
  186. Object field = getCflowfield(xcut, concreteEntry, concreteAspect, "counter");
  187. // Check if we have already got a counter for this cflow pointcut
  188. if (field != null) {
  189. localCflowField = (ResolvedMember) field; // Use the one we
  190. // already have
  191. } else {
  192. // Create a counter field in the aspect
  193. localCflowField = new ResolvedMemberImpl(Member.FIELD, concreteAspect, Modifier.STATIC | Modifier.PUBLIC
  194. | Modifier.FINAL, NameMangler.cflowCounter(xcut), UnresolvedType.forName(NameMangler.CFLOW_COUNTER_TYPE)
  195. .getSignature());
  196. // Create type munger to add field to the aspect
  197. concreteAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().makeCflowCounterFieldAdder(
  198. localCflowField));
  199. // Create shadow munger to push stuff onto the stack
  200. concreteAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makeCflowEntry(world, concreteEntry, isBelow,
  201. localCflowField, freeVars == null ? 0 : freeVars.length, innerCflowEntries, inAspect));
  202. putCflowfield(xcut, concreteEntry, concreteAspect, localCflowField, "counter"); // Remember
  203. // it
  204. }
  205. Pointcut ret = new ConcreteCflowPointcut(concreteAspect, localCflowField, null, true);
  206. ret.copyLocationFrom(this);
  207. return ret;
  208. } else {
  209. List slots = new ArrayList();
  210. for (int i = 0, len = freeVars.length; i < len; i++) {
  211. int freeVar = freeVars[i];
  212. // we don't need to keep state that isn't actually exposed to
  213. // advice
  214. // ??? this means that we will store some state that we won't
  215. // actually use, optimize this later
  216. if (!bindings.hasKey(freeVar)) {
  217. continue;
  218. }
  219. int formalIndex = bindings.get(freeVar);
  220. // We need to look in the right place for the type of the
  221. // formal. Suppose the advice looks like this:
  222. // before(String s): somePointcut(*,s)
  223. // where the first argument in somePointcut is of type Number
  224. // for free variable 0 we want to ask the pointcut for the type
  225. // of its first argument, if we only
  226. // ask the advice for the type of its first argument then we'll
  227. // get the wrong type (pr86903)
  228. ResolvedPointcutDefinition enclosingDef = bindings.peekEnclosingDefinition();
  229. ResolvedType formalType = null;
  230. // Is there a useful enclosing pointcut?
  231. if (enclosingDef != null && enclosingDef.getParameterTypes().length > 0) {
  232. formalType = enclosingDef.getParameterTypes()[freeVar].resolve(world);
  233. } else {
  234. formalType = bindings.getAdviceSignature().getParameterTypes()[formalIndex].resolve(world);
  235. }
  236. ConcreteCflowPointcut.Slot slot = new ConcreteCflowPointcut.Slot(formalIndex, formalType, i);
  237. slots.add(slot);
  238. }
  239. ResolvedMember localCflowField = null;
  240. Object field = getCflowfield(xcut, concreteEntry, concreteAspect, "stack");
  241. if (field != null) {
  242. localCflowField = (ResolvedMember) field;
  243. } else {
  244. localCflowField = new ResolvedMemberImpl(Member.FIELD, concreteAspect, Modifier.STATIC | Modifier.PUBLIC
  245. | Modifier.FINAL, NameMangler.cflowStack(xcut), UnresolvedType.forName(NameMangler.CFLOW_STACK_TYPE)
  246. .getSignature());
  247. // System.out.println("adding field to: " + inAspect + " field "
  248. // + cflowField);
  249. // add field and initializer to inAspect
  250. // XXX and then that info above needs to be mapped down here to
  251. // help with
  252. // XXX getting the exposed state right
  253. concreteAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makeCflowEntry(world, concreteEntry, isBelow,
  254. localCflowField, freeVars.length, innerCflowEntries, inAspect));
  255. concreteAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport()
  256. .makeCflowStackFieldAdder(localCflowField));
  257. putCflowfield(xcut, concreteEntry, concreteAspect, localCflowField, "stack");
  258. }
  259. Pointcut ret = new ConcreteCflowPointcut(concreteAspect, localCflowField, slots, false);
  260. ret.copyLocationFrom(this);
  261. return ret;
  262. }
  263. }
  264. private String getKey(Pointcut p, ResolvedType a, String stackOrCounter) {
  265. StringBuffer sb = new StringBuffer();
  266. sb.append(a.getName());
  267. sb.append("::");
  268. sb.append(p.toString());
  269. sb.append("::");
  270. sb.append(stackOrCounter);
  271. return sb.toString();
  272. }
  273. private Object getCflowfield(CrosscuttingMembers xcut, Pointcut pcutkey, ResolvedType concreteAspect, String stackOrCounter) {
  274. String key = getKey(pcutkey, concreteAspect, stackOrCounter);
  275. Object o = null;
  276. if (isBelow) {
  277. o = xcut.getCflowBelowFields().get(key);
  278. } else {
  279. o = xcut.getCflowFields().get(key);
  280. }
  281. // System.err.println("Retrieving for key "+key+" returning "+o);
  282. return o;
  283. }
  284. private void putCflowfield(CrosscuttingMembers xcut, Pointcut pcutkey, ResolvedType concreteAspect, Object o,
  285. String stackOrCounter) {
  286. String key = getKey(pcutkey, concreteAspect, stackOrCounter);
  287. // System.err.println("Storing cflow field for key"+key);
  288. if (isBelow) {
  289. xcut.getCflowBelowFields().put(key, o);
  290. } else {
  291. xcut.getCflowFields().put(key, o);
  292. }
  293. }
  294. public Object accept(PatternNodeVisitor visitor, Object data) {
  295. return visitor.visit(this, data);
  296. }
  297. }