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.

Advice.java 19KB

14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
13 anni fa
14 anni fa
14 anni fa
14 anni fa
12 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.ISourceLocation;
  17. import org.aspectj.weaver.patterns.AndPointcut;
  18. import org.aspectj.weaver.patterns.PerClause;
  19. import org.aspectj.weaver.patterns.Pointcut;
  20. import org.aspectj.weaver.patterns.TypePattern;
  21. public abstract class Advice extends ShadowMunger {
  22. protected AjAttribute.AdviceAttribute attribute;
  23. protected transient AdviceKind kind; // alias for attribute.getKind()
  24. protected Member signature;
  25. private boolean isAnnotationStyle;
  26. // not necessarily declaring aspect, this is a semantics change from 1.0
  27. protected ResolvedType concreteAspect; // null until after concretize
  28. // Just for Cflow*entry kinds
  29. protected List<ShadowMunger> innerCflowEntries = Collections.emptyList();
  30. protected int nFreeVars;
  31. protected TypePattern exceptionType; // just for Softener kind
  32. // if we are parameterized, these type may be different to the advice
  33. // signature types
  34. protected UnresolvedType[] bindingParameterTypes;
  35. protected boolean hasMatchedAtLeastOnce = false;
  36. // based on annotations on this advice
  37. protected List<Lint.Kind> suppressedLintKinds = null;
  38. public ISourceLocation lastReportedMonitorExitJoinpointLocation = null;
  39. public static Advice makeCflowEntry(World world, Pointcut entry, boolean isBelow, Member stackField, int nFreeVars,
  40. List<ShadowMunger> innerCflowEntries, ResolvedType inAspect) {
  41. Advice ret = world.createAdviceMunger(isBelow ? AdviceKind.CflowBelowEntry : AdviceKind.CflowEntry, entry, stackField, 0,
  42. entry, inAspect);
  43. ret.innerCflowEntries = innerCflowEntries;
  44. ret.nFreeVars = nFreeVars;
  45. ret.setDeclaringType(inAspect); // correct?
  46. return ret;
  47. }
  48. public static Advice makePerCflowEntry(World world, Pointcut entry, boolean isBelow, Member stackField, ResolvedType inAspect,
  49. List<ShadowMunger> innerCflowEntries) {
  50. Advice ret = world.createAdviceMunger(isBelow ? AdviceKind.PerCflowBelowEntry : AdviceKind.PerCflowEntry, entry,
  51. stackField, 0, entry, inAspect);
  52. ret.innerCflowEntries = innerCflowEntries;
  53. ret.concreteAspect = inAspect;
  54. return ret;
  55. }
  56. public static Advice makePerObjectEntry(World world, Pointcut entry, boolean isThis, ResolvedType inAspect) {
  57. Advice ret = world.createAdviceMunger(isThis ? AdviceKind.PerThisEntry : AdviceKind.PerTargetEntry, entry, null, 0, entry,
  58. inAspect);
  59. ret.concreteAspect = inAspect;
  60. return ret;
  61. }
  62. // PTWIMPL per type within entry advice is what initializes the aspect
  63. // instance in the matched type
  64. public static Advice makePerTypeWithinEntry(World world, Pointcut p, ResolvedType inAspect) {
  65. Advice ret = world.createAdviceMunger(AdviceKind.PerTypeWithinEntry, p, null, 0, p, inAspect);
  66. ret.concreteAspect = inAspect;
  67. return ret;
  68. }
  69. public boolean isAroundAdvice() {
  70. return attribute.getKind() == AdviceKind.Around;
  71. }
  72. public static Advice makeSoftener(World world, Pointcut entry, TypePattern exceptionType, ResolvedType inAspect,
  73. IHasSourceLocation loc) {
  74. Advice ret = world.createAdviceMunger(AdviceKind.Softener, entry, null, 0, loc, inAspect);
  75. ret.exceptionType = exceptionType;
  76. return ret;
  77. }
  78. public Advice(AjAttribute.AdviceAttribute attribute, Pointcut pointcut, Member signature) {
  79. super(pointcut, attribute.getStart(), attribute.getEnd(), attribute.getSourceContext(), ShadowMungerAdvice);
  80. this.attribute = attribute;
  81. this.isAnnotationStyle = signature != null && !signature.getName().startsWith("ajc$");
  82. this.kind = attribute.getKind(); // alias
  83. this.signature = signature;
  84. if (signature != null) {
  85. bindingParameterTypes = signature.getParameterTypes();
  86. } else {
  87. bindingParameterTypes = UnresolvedType.NONE;
  88. }
  89. }
  90. @Override
  91. public boolean match(Shadow shadow, World world) {
  92. if (super.match(shadow, world)) {
  93. if (shadow.getKind() == Shadow.ExceptionHandler) {
  94. if (kind.isAfter() || kind == AdviceKind.Around) {
  95. world.showMessage(IMessage.WARNING, WeaverMessages.format(WeaverMessages.ONLY_BEFORE_ON_HANDLER),
  96. getSourceLocation(), shadow.getSourceLocation());
  97. return false;
  98. }
  99. }
  100. if (shadow.getKind() == Shadow.SynchronizationLock || shadow.getKind() == Shadow.SynchronizationUnlock) {
  101. if (kind == AdviceKind.Around
  102. // Don't work, see comments in SynchronizationTests
  103. // && attribute.getProceedCallSignatures()!=null
  104. // && attribute.getProceedCallSignatures().length!=0
  105. ) {
  106. world.showMessage(IMessage.WARNING, WeaverMessages.format(WeaverMessages.NO_AROUND_ON_SYNCHRONIZATION),
  107. getSourceLocation(), shadow.getSourceLocation());
  108. return false;
  109. }
  110. }
  111. if (hasExtraParameter() && kind == AdviceKind.AfterReturning) {
  112. ResolvedType resolvedExtraParameterType = getExtraParameterType().resolve(world);
  113. ResolvedType shadowReturnType = shadow.getReturnType().resolve(world);
  114. boolean matches = (resolvedExtraParameterType.isConvertableFrom(shadowReturnType) && shadow.getKind()
  115. .hasReturnValue());
  116. if (matches && resolvedExtraParameterType.isParameterizedType()) {
  117. maybeIssueUncheckedMatchWarning(resolvedExtraParameterType, shadowReturnType, shadow, world);
  118. }
  119. return matches;
  120. } else if (hasExtraParameter() && kind == AdviceKind.AfterThrowing) { // pr119749
  121. ResolvedType exceptionType = getExtraParameterType().resolve(world);
  122. if (!exceptionType.isCheckedException() || exceptionType.getName().equals("java.lang.Exception")) { // pr292239
  123. return true;
  124. }
  125. UnresolvedType[] shadowThrows = shadow.getSignature().getExceptions(world);
  126. boolean matches = false;
  127. for (int i = 0; i < shadowThrows.length && !matches; i++) {
  128. ResolvedType type = shadowThrows[i].resolve(world);
  129. if (exceptionType.isAssignableFrom(type)) {
  130. matches = true;
  131. }
  132. }
  133. return matches;
  134. } else if (kind == AdviceKind.PerTargetEntry) {
  135. return shadow.hasTarget();
  136. } else if (kind == AdviceKind.PerThisEntry) {
  137. // Groovy Constructors have a strange switch statement in them - this switch statement can leave us in places where
  138. // the
  139. // instance is not initialized (a super ctor hasn't been called yet).
  140. // In these situations it isn't safe to do a perObjectBind, the instance is not initialized and cannot be passed
  141. // over.
  142. if (shadow.getEnclosingCodeSignature().getName().equals("<init>")) {
  143. if (world.resolve(shadow.getEnclosingType()).isGroovyObject()) {
  144. return false;
  145. }
  146. }
  147. return shadow.hasThis();
  148. } else if (kind == AdviceKind.Around) {
  149. if (shadow.getKind() == Shadow.PreInitialization) {
  150. world.showMessage(IMessage.WARNING, WeaverMessages.format(WeaverMessages.AROUND_ON_PREINIT),
  151. getSourceLocation(), shadow.getSourceLocation());
  152. return false;
  153. } else if (shadow.getKind() == Shadow.Initialization) {
  154. world.showMessage(IMessage.WARNING, WeaverMessages.format(WeaverMessages.AROUND_ON_INIT), getSourceLocation(),
  155. shadow.getSourceLocation());
  156. return false;
  157. } else if (shadow.getKind() == Shadow.StaticInitialization
  158. && shadow.getEnclosingType().resolve(world).isInterface()) {
  159. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.AROUND_ON_INTERFACE_STATICINIT, shadow
  160. .getEnclosingType().getName()), getSourceLocation(), shadow.getSourceLocation());
  161. return false;
  162. } else {
  163. // System.err.println(getSignature().getReturnType() +
  164. // " from " + shadow.getReturnType());
  165. if (getSignature().getReturnType().equals(UnresolvedType.VOID)) {
  166. if (!shadow.getReturnType().equals(UnresolvedType.VOID)) {
  167. String s = shadow.toString();
  168. String s2 = WeaverMessages.format(WeaverMessages.NON_VOID_RETURN, s);
  169. world.showMessage(IMessage.ERROR, s2, getSourceLocation(), shadow.getSourceLocation());
  170. return false;
  171. }
  172. } else if (getSignature().getReturnType().equals(UnresolvedType.OBJECT)) {
  173. return true;
  174. } else {
  175. ResolvedType shadowReturnType = shadow.getReturnType().resolve(world);
  176. ResolvedType adviceReturnType = getSignature().getGenericReturnType().resolve(world);
  177. if (shadowReturnType.isParameterizedType() && adviceReturnType.isRawType()) { // Set
  178. // <
  179. // Integer
  180. // >
  181. // and
  182. // Set
  183. ResolvedType shadowReturnGenericType = shadowReturnType.getGenericType(); // Set
  184. ResolvedType adviceReturnGenericType = adviceReturnType.getGenericType(); // Set
  185. if (shadowReturnGenericType.isAssignableFrom(adviceReturnGenericType)
  186. && world.getLint().uncheckedAdviceConversion.isEnabled()) {
  187. world.getLint().uncheckedAdviceConversion.signal(
  188. new String[] { shadow.toString(), shadowReturnType.getName(), adviceReturnType.getName() },
  189. shadow.getSourceLocation(), new ISourceLocation[] { getSourceLocation() });
  190. }
  191. } else if (!shadowReturnType.isAssignableFrom(adviceReturnType)) {
  192. // System.err.println(this + ", " + sourceContext +
  193. // ", " + start);
  194. world.showMessage(IMessage.ERROR,
  195. WeaverMessages.format(WeaverMessages.INCOMPATIBLE_RETURN_TYPE, shadow), getSourceLocation(),
  196. shadow.getSourceLocation());
  197. return false;
  198. }
  199. }
  200. }
  201. }
  202. return true;
  203. } else {
  204. return false;
  205. }
  206. }
  207. /**
  208. * In after returning advice if we are binding the extra parameter to a parameterized type we may not be able to do a type-safe
  209. * conversion.
  210. *
  211. * @param resolvedExtraParameterType the type in the after returning declaration
  212. * @param shadowReturnType the type at the shadow
  213. * @param world
  214. */
  215. private void maybeIssueUncheckedMatchWarning(ResolvedType afterReturningType, ResolvedType shadowReturnType, Shadow shadow,
  216. World world) {
  217. boolean inDoubt = !afterReturningType.isAssignableFrom(shadowReturnType);
  218. if (inDoubt && world.getLint().uncheckedArgument.isEnabled()) {
  219. String uncheckedMatchWith = afterReturningType.getSimpleBaseName();
  220. if (shadowReturnType.isParameterizedType() && (shadowReturnType.getRawType() == afterReturningType.getRawType())) {
  221. uncheckedMatchWith = shadowReturnType.getSimpleName();
  222. }
  223. if (!Utils.isSuppressing(getSignature().getAnnotations(), "uncheckedArgument")) {
  224. world.getLint().uncheckedArgument.signal(new String[] { afterReturningType.getSimpleName(), uncheckedMatchWith,
  225. afterReturningType.getSimpleBaseName(), shadow.toResolvedString(world) }, getSourceLocation(),
  226. new ISourceLocation[] { shadow.getSourceLocation() });
  227. }
  228. }
  229. }
  230. // ----
  231. public AdviceKind getKind() {
  232. return kind;
  233. }
  234. public Member getSignature() {
  235. return signature;
  236. }
  237. public boolean hasExtraParameter() {
  238. return (getExtraParameterFlags() & ExtraArgument) != 0;
  239. }
  240. protected int getExtraParameterFlags() {
  241. return attribute.getExtraParameterFlags();
  242. }
  243. protected int getExtraParameterCount() {
  244. return countOnes(getExtraParameterFlags() & ParameterMask);
  245. }
  246. public UnresolvedType[] getBindingParameterTypes() {
  247. return bindingParameterTypes;
  248. }
  249. public void setBindingParameterTypes(UnresolvedType[] types) {
  250. bindingParameterTypes = types;
  251. }
  252. public static int countOnes(int bits) {
  253. int ret = 0;
  254. while (bits != 0) {
  255. if ((bits & 1) != 0) {
  256. ret += 1;
  257. }
  258. bits = bits >> 1;
  259. }
  260. return ret;
  261. }
  262. public int getBaseParameterCount() {
  263. return getSignature().getParameterTypes().length - getExtraParameterCount();
  264. }
  265. public String[] getBaseParameterNames(World world) {
  266. String[] allNames = getSignature().getParameterNames(world);
  267. int extras = getExtraParameterCount();
  268. if (extras == 0) {
  269. return allNames;
  270. }
  271. String[] result = new String[getBaseParameterCount()];
  272. if (result.length >= 0) System.arraycopy(allNames, 0, result, 0, result.length);
  273. return result;
  274. }
  275. /**
  276. * Return the type of the 'extra argument'. For either after returning or after throwing advice, the extra argument will be the
  277. * returned value or the thrown exception respectively. With annotation style the user may declare the parameters in any order,
  278. * whereas for code style they are in a well defined order. So there is some extra complexity in here for annotation style that
  279. * looks up the correct parameter in the advice signature by name, based on the name specified in the annotation. If this fails
  280. * then we 'fallback' to guessing at positions, where the extra argument is presumed to come at the end.
  281. *
  282. * @return the type of the extraParameter
  283. */
  284. public UnresolvedType getExtraParameterType() {
  285. if (!hasExtraParameter()) {
  286. return ResolvedType.MISSING;
  287. }
  288. if (signature instanceof ResolvedMember) {
  289. ResolvedMember method = (ResolvedMember) signature;
  290. UnresolvedType[] parameterTypes = method.getGenericParameterTypes();
  291. if (getConcreteAspect().isAnnotationStyleAspect()) {
  292. // Examine the annotation to determine the parameter name then look it up in the parameters for the method
  293. String[] pnames = method.getParameterNames();
  294. if (pnames != null) {
  295. // It is worth attempting to look up the correct parameter
  296. AnnotationAJ[] annos = getSignature().getAnnotations();
  297. String parameterToLookup = null;
  298. if (annos != null && (getKind() == AdviceKind.AfterThrowing || getKind() == AdviceKind.AfterReturning)) {
  299. for (int i = 0; i < annos.length && parameterToLookup == null; i++) {
  300. AnnotationAJ anno = annos[i];
  301. String annosig = anno.getType().getSignature();
  302. if (annosig.equals("Lorg/aspectj/lang/annotation/AfterThrowing;")) {
  303. // the 'throwing' value in the annotation will name the parameter to bind to
  304. parameterToLookup = anno.getStringFormOfValue("throwing");
  305. } else if (annosig.equals("Lorg/aspectj/lang/annotation/AfterReturning;")) {
  306. // the 'returning' value in the annotation will name the parameter to bind to
  307. parameterToLookup = anno.getStringFormOfValue("returning");
  308. }
  309. }
  310. }
  311. if (parameterToLookup != null) {
  312. for (int i = 0; i < pnames.length; i++) {
  313. if (pnames[i].equals(parameterToLookup)) {
  314. return parameterTypes[i];
  315. }
  316. }
  317. }
  318. }
  319. // Don't think this code works so well... why isnt it getBaseParameterCount()-1 ?
  320. int baseParmCnt = getBaseParameterCount();
  321. // bug 122742 - if we're an annotation style aspect then one
  322. // of the extra parameters could be JoinPoint which we want
  323. // to ignore
  324. while ((baseParmCnt + 1 < parameterTypes.length)
  325. && (parameterTypes[baseParmCnt].equals(AjcMemberMaker.TYPEX_JOINPOINT)
  326. || parameterTypes[baseParmCnt].equals(AjcMemberMaker.TYPEX_STATICJOINPOINT) || parameterTypes[baseParmCnt]
  327. .equals(AjcMemberMaker.TYPEX_ENCLOSINGSTATICJOINPOINT))) {
  328. baseParmCnt++;
  329. }
  330. return parameterTypes[baseParmCnt];
  331. } else {
  332. return parameterTypes[getBaseParameterCount()];
  333. }
  334. } else {
  335. return signature.getParameterTypes()[getBaseParameterCount()];
  336. }
  337. }
  338. public UnresolvedType getDeclaringAspect() {
  339. return getOriginalSignature().getDeclaringType();
  340. }
  341. protected Member getOriginalSignature() {
  342. return signature;
  343. }
  344. protected String extraParametersToString() {
  345. if (getExtraParameterFlags() == 0) {
  346. return "";
  347. } else {
  348. return "(extraFlags: " + getExtraParameterFlags() + ")";
  349. }
  350. }
  351. @Override
  352. public Pointcut getPointcut() {
  353. return pointcut;
  354. }
  355. // ----
  356. /**
  357. * @param fromType is guaranteed to be a non-abstract aspect
  358. * @param clause has been concretized at a higher level
  359. */
  360. @Override
  361. public ShadowMunger concretize(ResolvedType fromType, World world, PerClause clause) {
  362. // assert !fromType.isAbstract();
  363. Pointcut p = pointcut.concretize(fromType, getDeclaringType(), signature.getArity(), this);
  364. if (clause != null) {
  365. Pointcut oldP = p;
  366. p = new AndPointcut(clause, p);
  367. p.copyLocationFrom(oldP);
  368. p.state = Pointcut.CONCRETE;
  369. // FIXME ? ATAJ copy unbound bindings to ignore
  370. p.m_ignoreUnboundBindingForNames = oldP.m_ignoreUnboundBindingForNames;
  371. }
  372. Advice munger = world.getWeavingSupport().createAdviceMunger(attribute, p, signature, fromType);
  373. munger.bindingParameterTypes = bindingParameterTypes;
  374. munger.setDeclaringType(getDeclaringType());
  375. // System.err.println("concretizing here " + p + " with clause " +
  376. // clause);
  377. return munger;
  378. }
  379. // ---- from object
  380. @Override
  381. public String toString() {
  382. StringBuilder sb = new StringBuilder();
  383. sb.append("(").append(getKind()).append(extraParametersToString());
  384. sb.append(": ").append(pointcut).append("->").append(signature).append(")");
  385. return sb.toString();
  386. // return "("
  387. // + getKind()
  388. // + extraParametersToString()
  389. // + ": "
  390. // + pointcut
  391. // + "->"
  392. // + signature
  393. // + ")";
  394. }
  395. // XXX this perhaps ought to take account of the other fields in advice ...
  396. @Override
  397. public boolean equals(Object other) {
  398. if (!(other instanceof Advice)) {
  399. return false;
  400. }
  401. Advice o = (Advice) other;
  402. return o.kind.equals(kind) && ((o.pointcut == null) ? (pointcut == null) : o.pointcut.equals(pointcut))
  403. && ((o.signature == null) ? (signature == null) : o.signature.equals(signature));
  404. // && (AsmManager.getDefault().getHandleProvider().dependsOnLocation() ? ((o.getSourceLocation() == null) ?
  405. // (getSourceLocation() == null)
  406. // : o.getSourceLocation().equals(getSourceLocation()))
  407. // : true) // pr134471 - remove when handles are improved
  408. // // to be independent of location
  409. // ;
  410. }
  411. private volatile int hashCode = 0;
  412. @Override
  413. public int hashCode() {
  414. if (hashCode == 0) {
  415. int result = 17;
  416. result = 37 * result + kind.hashCode();
  417. result = 37 * result + ((pointcut == null) ? 0 : pointcut.hashCode());
  418. result = 37 * result + ((signature == null) ? 0 : signature.hashCode());
  419. hashCode = result;
  420. }
  421. return hashCode;
  422. }
  423. // ---- fields
  424. public static final int ExtraArgument = 0x01;
  425. public static final int ThisJoinPoint = 0x02;
  426. public static final int ThisJoinPointStaticPart = 0x04;
  427. public static final int ThisEnclosingJoinPointStaticPart = 0x08;
  428. public static final int ParameterMask = 0x0f;
  429. // For an if pointcut, this indicates it is hard wired to access a constant of either true or false
  430. public static final int ConstantReference = 0x10;
  431. // When the above flag is set, this indicates whether it is true or false
  432. public static final int ConstantValue = 0x20;
  433. // public static final int CanInline = 0x40; // didnt appear to be getting used
  434. public static final int ThisAspectInstance = 0x40;
  435. // cant use 0x80 ! the value is written out as a byte and -1 has special meaning (-1 is 0x80...)
  436. // for testing only
  437. public void setLexicalPosition(int lexicalPosition) {
  438. start = lexicalPosition;
  439. }
  440. public boolean isAnnotationStyle() {
  441. return isAnnotationStyle;
  442. }
  443. public ResolvedType getConcreteAspect() {
  444. return concreteAspect;
  445. }
  446. public boolean hasMatchedSomething() {
  447. return hasMatchedAtLeastOnce;
  448. }
  449. public void setHasMatchedSomething(boolean hasMatchedSomething) {
  450. hasMatchedAtLeastOnce = hasMatchedSomething;
  451. }
  452. public abstract boolean hasDynamicTests();
  453. }