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.

KindedPointcut.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import org.aspectj.bridge.ISourceLocation;
  17. import org.aspectj.lang.JoinPoint;
  18. import org.aspectj.util.FuzzyBoolean;
  19. import org.aspectj.weaver.Checker;
  20. import org.aspectj.weaver.ISourceContext;
  21. import org.aspectj.weaver.IntMap;
  22. import org.aspectj.weaver.Member;
  23. import org.aspectj.weaver.ResolvedMember;
  24. import org.aspectj.weaver.ResolvedTypeX;
  25. import org.aspectj.weaver.Shadow;
  26. import org.aspectj.weaver.ShadowMunger;
  27. import org.aspectj.weaver.TypeX;
  28. import org.aspectj.weaver.World;
  29. import org.aspectj.weaver.ast.Literal;
  30. import org.aspectj.weaver.ast.Test;
  31. public class KindedPointcut extends Pointcut {
  32. Shadow.Kind kind;
  33. SignaturePattern signature;
  34. private ShadowMunger munger = null; // only set after concretization
  35. public KindedPointcut(
  36. Shadow.Kind kind,
  37. SignaturePattern signature) {
  38. this.kind = kind;
  39. this.signature = signature;
  40. }
  41. public KindedPointcut(
  42. Shadow.Kind kind,
  43. SignaturePattern signature,
  44. ShadowMunger munger)
  45. {
  46. this(kind, signature);
  47. this.munger = munger;
  48. }
  49. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  50. if (info.getKind() != null) {
  51. if (info.getKind() != kind) return FuzzyBoolean.NO;
  52. }
  53. return FuzzyBoolean.MAYBE;
  54. }
  55. public FuzzyBoolean match(Shadow shadow) {
  56. if (shadow.getKind() != kind) return FuzzyBoolean.NO;
  57. if (!signature.matches(shadow.getSignature(), shadow.getIWorld())){
  58. if(kind == Shadow.MethodCall) {
  59. warnOnConfusingSig(shadow);
  60. int shadowModifiers = shadow.getSignature().getModifiers(shadow.getIWorld());
  61. if (ResolvedTypeX.hasBridgeModifier(shadowModifiers)) {
  62. shadow.getIWorld().getLint().noJoinpointsForBridgeMethods.signal(new String[]{},getSourceLocation(),
  63. new ISourceLocation[]{shadow.getSourceLocation()});
  64. }
  65. }
  66. return FuzzyBoolean.NO;
  67. }
  68. return FuzzyBoolean.YES;
  69. }
  70. public FuzzyBoolean match(JoinPoint.StaticPart jpsp) {
  71. if (jpsp.getKind().equals(kind.getName())) {
  72. if (signature.matches(jpsp)) {
  73. return FuzzyBoolean.YES;
  74. }
  75. }
  76. return FuzzyBoolean.NO;
  77. }
  78. private void warnOnConfusingSig(Shadow shadow) {
  79. // no warnings for declare error/warning
  80. if (munger instanceof Checker) return;
  81. World world = shadow.getIWorld();
  82. // warning never needed if the declaring type is any
  83. TypeX exactDeclaringType = signature.getDeclaringType().getExactType();
  84. ResolvedTypeX shadowDeclaringType =
  85. shadow.getSignature().getDeclaringType().resolve(world);
  86. if (signature.getDeclaringType().isStar()
  87. || exactDeclaringType== ResolvedTypeX.MISSING)
  88. return;
  89. // warning not needed if match type couldn't ever be the declaring type
  90. if (!shadowDeclaringType.isAssignableFrom(exactDeclaringType)) {
  91. return;
  92. }
  93. // if the method in the declaring type is *not* visible to the
  94. // exact declaring type then warning not needed.
  95. int shadowModifiers = shadow.getSignature().getModifiers(world);
  96. if (!ResolvedTypeX
  97. .isVisible(
  98. shadowModifiers,
  99. shadowDeclaringType,
  100. exactDeclaringType.resolve(world))) {
  101. return;
  102. }
  103. // PR60015 - Don't report the warning if the declaring type is object and 'this' is an interface
  104. if (exactDeclaringType.isInterface(world) && shadowDeclaringType.equals(world.resolve("java.lang.Object"))) {
  105. return;
  106. }
  107. SignaturePattern nonConfusingPattern =
  108. new SignaturePattern(
  109. signature.getKind(),
  110. signature.getModifiers(),
  111. signature.getReturnType(),
  112. TypePattern.ANY,
  113. signature.getName(),
  114. signature.getParameterTypes(),
  115. signature.getThrowsPattern());
  116. if (nonConfusingPattern
  117. .matches(shadow.getSignature(), shadow.getIWorld())) {
  118. shadow.getIWorld().getLint().unmatchedSuperTypeInCall.signal(
  119. new String[] {
  120. shadow.getSignature().getDeclaringType().toString(),
  121. signature.getDeclaringType().toString()
  122. },
  123. this.getSourceLocation(),
  124. new ISourceLocation[] {shadow.getSourceLocation()} );
  125. }
  126. }
  127. public boolean equals(Object other) {
  128. if (!(other instanceof KindedPointcut)) return false;
  129. KindedPointcut o = (KindedPointcut)other;
  130. return o.kind == this.kind && o.signature.equals(this.signature);
  131. }
  132. public int hashCode() {
  133. int result = 17;
  134. result = 37*result + kind.hashCode();
  135. result = 37*result + signature.hashCode();
  136. return result;
  137. }
  138. public String toString() {
  139. StringBuffer buf = new StringBuffer();
  140. buf.append(kind.getSimpleName());
  141. buf.append("(");
  142. buf.append(signature.toString());
  143. buf.append(")");
  144. return buf.toString();
  145. }
  146. public void postRead(ResolvedTypeX enclosingType) {
  147. signature.postRead(enclosingType);
  148. }
  149. public void write(DataOutputStream s) throws IOException {
  150. s.writeByte(Pointcut.KINDED);
  151. kind.write(s);
  152. signature.write(s);
  153. writeLocation(s);
  154. }
  155. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  156. Shadow.Kind kind = Shadow.Kind.read(s);
  157. SignaturePattern sig = SignaturePattern.read(s, context);
  158. KindedPointcut ret = new KindedPointcut(kind, sig);
  159. ret.readLocation(context, s);
  160. return ret;
  161. }
  162. // XXX note: there is no namebinding in any kinded pointcut.
  163. // still might want to do something for better error messages
  164. // We want to do something here to make sure we don't sidestep the parameter
  165. // list in capturing type identifiers.
  166. public void resolveBindings(IScope scope, Bindings bindings) {
  167. if (kind == Shadow.Initialization) {
  168. // scope.getMessageHandler().handleMessage(
  169. // MessageUtil.error(
  170. // "initialization unimplemented in 1.1beta1",
  171. // this.getSourceLocation()));
  172. }
  173. signature = signature.resolveBindings(scope, bindings);
  174. if (kind == Shadow.ConstructorExecution) { // Bug fix 60936
  175. if (signature.getDeclaringType() != null) {
  176. World world = scope.getWorld();
  177. TypeX exactType = signature.getDeclaringType().getExactType();
  178. if (signature.getKind() == Member.CONSTRUCTOR &&
  179. !exactType.equals(ResolvedTypeX.MISSING) &&
  180. exactType.isInterface(world) &&
  181. !signature.getDeclaringType().isIncludeSubtypes()) {
  182. world.getLint().noInterfaceCtorJoinpoint.signal(exactType.toString(), getSourceLocation());
  183. }
  184. }
  185. }
  186. }
  187. public void resolveBindingsFromRTTI() {
  188. signature = signature.resolveBindingsFromRTTI();
  189. }
  190. public Test findResidue(Shadow shadow, ExposedState state) {
  191. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  192. }
  193. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  194. Pointcut ret = new KindedPointcut(kind, signature, bindings.getEnclosingAdvice());
  195. ret.copyLocationFrom(this);
  196. return ret;
  197. }
  198. public Shadow.Kind getKind() {
  199. return kind;
  200. }
  201. }