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

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