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.

JoinPointImpl.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.runtime.reflect;
  14. import java.util.Stack;
  15. import org.aspectj.lang.JoinPoint;
  16. import org.aspectj.lang.ProceedingJoinPoint;
  17. import org.aspectj.lang.Signature;
  18. import org.aspectj.lang.reflect.SourceLocation;
  19. import org.aspectj.runtime.internal.AroundClosure;
  20. class JoinPointImpl implements ProceedingJoinPoint {
  21. static class StaticPartImpl implements JoinPoint.StaticPart {
  22. String kind;
  23. Signature signature;
  24. SourceLocation sourceLocation;
  25. private int id;
  26. public StaticPartImpl(int id, String kind, Signature signature, SourceLocation sourceLocation) {
  27. this.kind = kind;
  28. this.signature = signature;
  29. this.sourceLocation = sourceLocation;
  30. this.id = id;
  31. }
  32. public int getId() {
  33. return id;
  34. }
  35. public String getKind() {
  36. return kind;
  37. }
  38. public Signature getSignature() {
  39. return signature;
  40. }
  41. public SourceLocation getSourceLocation() {
  42. return sourceLocation;
  43. }
  44. String toString(StringMaker sm) {
  45. StringBuilder buf = new StringBuilder();
  46. buf.append(sm.makeKindName(getKind()));
  47. buf.append("(");
  48. buf.append(((SignatureImpl) getSignature()).toString(sm));
  49. buf.append(")");
  50. return buf.toString();
  51. }
  52. public final String toString() {
  53. return toString(StringMaker.middleStringMaker);
  54. }
  55. public final String toShortString() {
  56. return toString(StringMaker.shortStringMaker);
  57. }
  58. public final String toLongString() {
  59. return toString(StringMaker.longStringMaker);
  60. }
  61. }
  62. static class EnclosingStaticPartImpl extends StaticPartImpl implements EnclosingStaticPart {
  63. public EnclosingStaticPartImpl(int count, String kind, Signature signature, SourceLocation sourceLocation) {
  64. super(count, kind, signature, sourceLocation);
  65. }
  66. }
  67. static class InheritableThreadLocalAroundClosureStack extends InheritableThreadLocal<Stack<AroundClosure>> {
  68. @Override
  69. protected Stack<AroundClosure> initialValue() {
  70. return new Stack<>();
  71. }
  72. @Override
  73. protected Stack<AroundClosure> childValue(Stack<AroundClosure> parentValue) {
  74. return (Stack<AroundClosure>) parentValue.clone();
  75. }
  76. }
  77. Object _this;
  78. Object target;
  79. Object[] args;
  80. org.aspectj.lang.JoinPoint.StaticPart staticPart;
  81. public JoinPointImpl(org.aspectj.lang.JoinPoint.StaticPart staticPart, Object _this, Object target, Object[] args) {
  82. this.staticPart = staticPart;
  83. this._this = _this;
  84. this.target = target;
  85. this.args = args;
  86. }
  87. public Object getThis() {
  88. return _this;
  89. }
  90. public Object getTarget() {
  91. return target;
  92. }
  93. public Object[] getArgs() {
  94. if (args == null) {
  95. args = new Object[0];
  96. }
  97. Object[] argsCopy = new Object[args.length];
  98. System.arraycopy(args, 0, argsCopy, 0, args.length);
  99. return argsCopy;
  100. }
  101. public org.aspectj.lang.JoinPoint.StaticPart getStaticPart() {
  102. return staticPart;
  103. }
  104. public String getKind() {
  105. return staticPart.getKind();
  106. }
  107. public Signature getSignature() {
  108. return staticPart.getSignature();
  109. }
  110. public SourceLocation getSourceLocation() {
  111. return staticPart.getSourceLocation();
  112. }
  113. public final String toString() {
  114. return staticPart.toString();
  115. }
  116. public final String toShortString() {
  117. return staticPart.toShortString();
  118. }
  119. public final String toLongString() {
  120. return staticPart.toLongString();
  121. }
  122. // To proceed we need a closure to proceed on. Generated code
  123. // will either be using arc or arcs but not both. arcs being non-null
  124. // indicates it is in use (even if an empty stack)
  125. private AroundClosure arc = null;
  126. private InheritableThreadLocalAroundClosureStack arcs = null;
  127. public void set$AroundClosure(AroundClosure arc) {
  128. this.arc = arc;
  129. }
  130. public void stack$AroundClosure(AroundClosure arc) {
  131. // If input parameter arc is null this is the 'unlink' call from AroundClosure
  132. if (arcs == null) {
  133. arcs = new InheritableThreadLocalAroundClosureStack();
  134. }
  135. if (arc==null) {
  136. this.arcs.get().pop();
  137. } else {
  138. this.arcs.get().push(arc);
  139. }
  140. }
  141. public Object proceed() throws Throwable {
  142. // when called from a before advice, but be a no-op
  143. if (arcs == null) {
  144. if (arc == null) {
  145. return null;
  146. } else {
  147. return arc.run(arc.getState());
  148. }
  149. } else {
  150. final AroundClosure ac = arcs.get().peek();
  151. return ac.run(ac.getState());
  152. }
  153. }
  154. public Object proceed(Object[] adviceBindings) throws Throwable {
  155. // when called from a before advice, but be a no-op
  156. AroundClosure ac = null;
  157. if (arcs == null) {
  158. ac = arc;
  159. } else {
  160. ac = arcs.get().peek();
  161. }
  162. if (ac == null) {
  163. return null;
  164. } else {
  165. // Based on the bit flags in the AroundClosure we can determine what to
  166. // expect in the adviceBindings array. We may or may not be expecting
  167. // the first value to be a new this or a new target... (see pr126167)
  168. int flags = ac.getFlags();
  169. boolean unset = (flags & 0x100000) != 0;
  170. boolean thisTargetTheSame = (flags & 0x010000) != 0;
  171. boolean hasThis = (flags & 0x001000) != 0;
  172. boolean bindsThis = (flags & 0x000100) != 0;
  173. boolean hasTarget = (flags & 0x000010) != 0;
  174. boolean bindsTarget = (flags & 0x000001) != 0;
  175. // state is always consistent with caller?,callee?,formals...,jp
  176. Object[] state = ac.getState();
  177. // these next two numbers can differ because some join points have a this and
  178. // target that are the same (eg. call) - and yet you can bind this and target
  179. // separately.
  180. // In the state array, [0] may be this, [1] may be target
  181. int firstArgumentIndexIntoAdviceBindings = 0;
  182. int firstArgumentIndexIntoState = 0;
  183. firstArgumentIndexIntoState += (hasThis ? 1 : 0);
  184. firstArgumentIndexIntoState += (hasTarget && !thisTargetTheSame ? 1 : 0);
  185. if (hasThis) {
  186. if (bindsThis) {
  187. // replace [0] (this)
  188. firstArgumentIndexIntoAdviceBindings = 1;
  189. state[0] = adviceBindings[0];
  190. } else {
  191. // leave state[0] alone, its OK
  192. }
  193. }
  194. if (hasTarget) {
  195. if (bindsTarget) {
  196. if (thisTargetTheSame) {
  197. // this and target are the same so replace state[0]
  198. firstArgumentIndexIntoAdviceBindings = 1 + (bindsThis ? 1 : 0);
  199. state[0] = adviceBindings[(bindsThis ? 1 : 0)];
  200. } else {
  201. // need to replace the target, and it is different to this, whether
  202. // that means replacing state[0] or state[1] depends on whether
  203. // the join point has a this
  204. // This previous variant doesn't seem to cope with only binding target at a joinpoint
  205. // which has both this and target. It forces you to supply this even if you didn't bind
  206. // it.
  207. // firstArgumentIndexIntoAdviceBindings = (hasThis ? 1 : 0) + 1;
  208. // state[hasThis ? 1 : 0] = adviceBindings[hasThis ? 1 : 0];
  209. int targetPositionInAdviceBindings = (hasThis && bindsThis) ? 1 : 0;
  210. firstArgumentIndexIntoAdviceBindings = ((hasThis&&bindsThis)?1:0)+((hasTarget&&bindsTarget&&!thisTargetTheSame)?1:0);
  211. state[hasThis ? 1 : 0] = adviceBindings[targetPositionInAdviceBindings];
  212. }
  213. } else {
  214. // leave state[0]/state[1] alone, they are OK
  215. }
  216. }
  217. // copy the rest across
  218. for (int i = firstArgumentIndexIntoAdviceBindings; i < adviceBindings.length; i++) {
  219. state[firstArgumentIndexIntoState + (i - firstArgumentIndexIntoAdviceBindings)] = adviceBindings[i];
  220. }
  221. // old code that did this, didnt allow this/target overriding
  222. // for (int i = state.length-2; i >= 0; i--) {
  223. // int formalIndex = (adviceBindings.length - 1) - (state.length-2) + i;
  224. // if (formalIndex >= 0 && formalIndex < adviceBindings.length) {
  225. // state[i] = adviceBindings[formalIndex];
  226. // }
  227. // }
  228. return ac.run(state);
  229. }
  230. }
  231. }