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

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