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.

AdviceImpl.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.internal.lang.reflect;
  13. import java.lang.reflect.Method;
  14. import java.lang.reflect.Type;
  15. import org.aspectj.lang.annotation.AdviceName;
  16. import org.aspectj.lang.reflect.Advice;
  17. import org.aspectj.lang.reflect.AdviceKind;
  18. import org.aspectj.lang.reflect.AjType;
  19. import org.aspectj.lang.reflect.AjTypeSystem;
  20. import org.aspectj.lang.reflect.PointcutExpression;
  21. /**
  22. * @author colyer
  23. *
  24. */
  25. public class AdviceImpl implements Advice {
  26. private static final String AJC_INTERNAL = "org.aspectj.runtime.internal";
  27. private final AdviceKind kind;
  28. private final Method adviceMethod;
  29. private PointcutExpression pointcutExpression;
  30. private boolean hasExtraParam = false;
  31. private Type[] genericParameterTypes;
  32. private AjType[] parameterTypes;
  33. private AjType[] exceptionTypes;
  34. protected AdviceImpl(Method method, String pointcut, AdviceKind type) {
  35. this.kind = type;
  36. this.adviceMethod = method;
  37. this.pointcutExpression = new PointcutExpressionImpl(pointcut);
  38. }
  39. protected AdviceImpl(Method method, String pointcut, AdviceKind type, String extraParamName) {
  40. this(method,pointcut,type);
  41. this.hasExtraParam = true;
  42. }
  43. public AjType getDeclaringType() {
  44. return AjTypeSystem.getAjType(adviceMethod.getDeclaringClass());
  45. }
  46. public Type[] getGenericParameterTypes() {
  47. if (this.genericParameterTypes == null) {
  48. Type[] genTypes = adviceMethod.getGenericParameterTypes();
  49. int syntheticCount = 0;
  50. for (Type t : genTypes) {
  51. if (t instanceof Class) {
  52. if (((Class)t).getPackage().getName().equals(AJC_INTERNAL)) syntheticCount++;
  53. }
  54. }
  55. this.genericParameterTypes = new Type[genTypes.length - syntheticCount];
  56. for (int i = 0; i < genericParameterTypes.length; i++) {
  57. if (genTypes[i] instanceof Class) {
  58. this.genericParameterTypes[i] = AjTypeSystem.getAjType((Class<?>)genTypes[i]);
  59. } else {
  60. this.genericParameterTypes[i] = genTypes[i];
  61. }
  62. }
  63. }
  64. return this.genericParameterTypes;
  65. }
  66. public AjType<?>[] getParameterTypes() {
  67. if (this.parameterTypes == null) {
  68. Class<?>[] ptypes = adviceMethod.getParameterTypes();
  69. int syntheticCount = 0;
  70. for(Class<?> c : ptypes) {
  71. if (c.getPackage().getName().equals(AJC_INTERNAL)) syntheticCount++;
  72. }
  73. this.parameterTypes = new AjType<?>[ptypes.length - syntheticCount];
  74. for (int i = 0; i < parameterTypes.length; i++) {
  75. this.parameterTypes[i] = AjTypeSystem.getAjType(ptypes[i]);
  76. }
  77. }
  78. return this.parameterTypes;
  79. }
  80. public AjType<?>[] getExceptionTypes() {
  81. if (this.exceptionTypes == null) {
  82. Class<?>[] exTypes = adviceMethod.getExceptionTypes();
  83. this.exceptionTypes = new AjType<?>[exTypes.length];
  84. for (int i = 0; i < exTypes.length; i++) {
  85. this.exceptionTypes[i] = AjTypeSystem.getAjType(exTypes[i]);
  86. }
  87. }
  88. return this.exceptionTypes;
  89. }
  90. public AdviceKind getKind() {
  91. return kind;
  92. }
  93. public String getName() {
  94. String adviceName = adviceMethod.getName();
  95. if (adviceName.startsWith("ajc$")) {
  96. adviceName = "";
  97. AdviceName name = adviceMethod.getAnnotation(AdviceName.class);
  98. if (name != null) adviceName = name.value();
  99. }
  100. return adviceName;
  101. }
  102. public PointcutExpression getPointcutExpression() {
  103. return pointcutExpression;
  104. }
  105. public String toString() {
  106. StringBuffer sb = new StringBuffer();
  107. if (getName().length() > 0) {
  108. sb.append("@AdviceName(\"");
  109. sb.append(getName());
  110. sb.append("\") ");
  111. }
  112. if (getKind() == AdviceKind.AROUND) {
  113. sb.append(adviceMethod.getGenericReturnType().toString());
  114. sb.append(" ");
  115. }
  116. switch(getKind()) {
  117. case AFTER:
  118. sb.append("after(");
  119. break;
  120. case AFTER_RETURNING:
  121. sb.append("after(");
  122. break;
  123. case AFTER_THROWING:
  124. sb.append("after(");
  125. break;
  126. case AROUND:
  127. sb.append("around(");
  128. break;
  129. case BEFORE:
  130. sb.append("before(");
  131. break;
  132. }
  133. AjType<?>[] ptypes = getParameterTypes();
  134. int len = ptypes.length;
  135. if (hasExtraParam) len--;
  136. for (int i = 0; i < len; i++) {
  137. sb.append(ptypes[i].getName());
  138. if (i+1 < len) sb.append(",");
  139. }
  140. sb.append(") ");
  141. switch(getKind()) {
  142. case AFTER_RETURNING:
  143. sb.append("returning");
  144. if (hasExtraParam) {
  145. sb.append("(");
  146. sb.append(ptypes[len-1].getName());
  147. sb.append(") ");
  148. }
  149. case AFTER_THROWING:
  150. sb.append("throwing");
  151. if (hasExtraParam) {
  152. sb.append("(");
  153. sb.append(ptypes[len-1].getName());
  154. sb.append(") ");
  155. }
  156. default: // no-op
  157. }
  158. AjType<?>[] exTypes = getExceptionTypes();
  159. if (exTypes.length > 0) {
  160. sb.append("throws ");
  161. for (int i = 0; i < exTypes.length; i++) {
  162. sb.append(exTypes[i].getName());
  163. if (i+1 < exTypes.length) sb.append(",");
  164. }
  165. sb.append(" ");
  166. }
  167. sb.append(": ");
  168. sb.append(getPointcutExpression().asString());
  169. return sb.toString();
  170. }
  171. }