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

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