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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. int newIndex = arcIndex.get() - 1;
  129. if (newIndex > -1)
  130. arcIndex.set(newIndex);
  131. else
  132. arcIndex.remove();
  133. }
  134. else {
  135. this.arcs.add(arc);
  136. arcIndex.set(arcs.size() - 1);
  137. }
  138. }
  139. public Object proceed() throws Throwable {
  140. // when called from a before advice, but be a no-op
  141. if (arcs == null) {
  142. if (arc == null) {
  143. return null;
  144. } else {
  145. return arc.run(arc.getState());
  146. }
  147. } else {
  148. final AroundClosure ac = arcs.get(arcIndex.get());
  149. return ac.run(ac.getState());
  150. }
  151. }
  152. public Object proceed(Object[] adviceBindings) throws Throwable {
  153. // when called from a before advice, but be a no-op
  154. AroundClosure ac = arcs == null ? arc : arcs.get(arcIndex.get());
  155. if (ac == null) {
  156. return null;
  157. } else {
  158. // Based on the bit flags in the AroundClosure we can determine what to
  159. // expect in the adviceBindings array. We may or may not be expecting
  160. // the first value to be a new this or a new target... (see pr126167)
  161. int flags = ac.getFlags();
  162. boolean unset = (flags & 0x100000) != 0;
  163. boolean thisTargetTheSame = (flags & 0x010000) != 0;
  164. boolean hasThis = (flags & 0x001000) != 0;
  165. boolean bindsThis = (flags & 0x000100) != 0;
  166. boolean hasTarget = (flags & 0x000010) != 0;
  167. boolean bindsTarget = (flags & 0x000001) != 0;
  168. // state is always consistent with caller?,callee?,formals...,jp
  169. Object[] state = ac.getState();
  170. // these next two numbers can differ because some join points have a this and
  171. // target that are the same (eg. call) - and yet you can bind this and target
  172. // separately.
  173. // In the state array, [0] may be this, [1] may be target
  174. int firstArgumentIndexIntoAdviceBindings = 0;
  175. int firstArgumentIndexIntoState = 0;
  176. firstArgumentIndexIntoState += (hasThis ? 1 : 0);
  177. firstArgumentIndexIntoState += (hasTarget && !thisTargetTheSame ? 1 : 0);
  178. if (hasThis) {
  179. if (bindsThis) {
  180. // replace [0] (this)
  181. firstArgumentIndexIntoAdviceBindings = 1;
  182. state[0] = adviceBindings[0];
  183. } else {
  184. // leave state[0] alone, its OK
  185. }
  186. }
  187. if (hasTarget) {
  188. if (bindsTarget) {
  189. if (thisTargetTheSame) {
  190. // this and target are the same so replace state[0]
  191. firstArgumentIndexIntoAdviceBindings = 1 + (bindsThis ? 1 : 0);
  192. state[0] = adviceBindings[(bindsThis ? 1 : 0)];
  193. } else {
  194. // need to replace the target, and it is different to this, whether
  195. // that means replacing state[0] or state[1] depends on whether
  196. // the join point has a this
  197. // This previous variant doesn't seem to cope with only binding target at a joinpoint
  198. // which has both this and target. It forces you to supply this even if you didn't bind
  199. // it.
  200. // firstArgumentIndexIntoAdviceBindings = (hasThis ? 1 : 0) + 1;
  201. // state[hasThis ? 1 : 0] = adviceBindings[hasThis ? 1 : 0];
  202. int targetPositionInAdviceBindings = (hasThis && bindsThis) ? 1 : 0;
  203. firstArgumentIndexIntoAdviceBindings = ((hasThis&&bindsThis)?1:0)+((hasTarget&&bindsTarget&&!thisTargetTheSame)?1:0);
  204. state[hasThis ? 1 : 0] = adviceBindings[targetPositionInAdviceBindings];
  205. }
  206. } else {
  207. // leave state[0]/state[1] alone, they are OK
  208. }
  209. }
  210. // copy the rest across
  211. for (int i = firstArgumentIndexIntoAdviceBindings; i < adviceBindings.length; i++) {
  212. state[firstArgumentIndexIntoState + (i - firstArgumentIndexIntoAdviceBindings)] = adviceBindings[i];
  213. }
  214. // old code that did this, didnt allow this/target overriding
  215. // for (int i = state.length-2; i >= 0; i--) {
  216. // int formalIndex = (adviceBindings.length - 1) - (state.length-2) + i;
  217. // if (formalIndex >= 0 && formalIndex < adviceBindings.length) {
  218. // state[i] = adviceBindings[formalIndex];
  219. // }
  220. // }
  221. return ac.run(state);
  222. }
  223. }
  224. }