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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. if (!signature.getReturnType().matchesStatically(shadow.getSignature().getReturnType().resolve(world))) {
  104. // Covariance issue...
  105. // The reason we didn't match is that the type pattern for the pointcut (Car) doesn't match the
  106. // return type for the specific declaration at the shadow. (FastCar Sub.getCar())
  107. // XXX Put out another XLINT in this case?
  108. return;
  109. }
  110. // PR60015 - Don't report the warning if the declaring type is object and 'this' is an interface
  111. if (exactDeclaringType.isInterface(world) && shadowDeclaringType.equals(world.resolve("java.lang.Object"))) {
  112. return;
  113. }
  114. SignaturePattern nonConfusingPattern =
  115. new SignaturePattern(
  116. signature.getKind(),
  117. signature.getModifiers(),
  118. signature.getReturnType(),
  119. TypePattern.ANY,
  120. signature.getName(),
  121. signature.getParameterTypes(),
  122. signature.getThrowsPattern());
  123. if (nonConfusingPattern
  124. .matches(shadow.getSignature(), shadow.getIWorld())) {
  125. shadow.getIWorld().getLint().unmatchedSuperTypeInCall.signal(
  126. new String[] {
  127. shadow.getSignature().getDeclaringType().toString(),
  128. signature.getDeclaringType().toString()
  129. },
  130. this.getSourceLocation(),
  131. new ISourceLocation[] {shadow.getSourceLocation()} );
  132. }
  133. }
  134. public boolean equals(Object other) {
  135. if (!(other instanceof KindedPointcut)) return false;
  136. KindedPointcut o = (KindedPointcut)other;
  137. return o.kind == this.kind && o.signature.equals(this.signature);
  138. }
  139. public int hashCode() {
  140. int result = 17;
  141. result = 37*result + kind.hashCode();
  142. result = 37*result + signature.hashCode();
  143. return result;
  144. }
  145. public String toString() {
  146. StringBuffer buf = new StringBuffer();
  147. buf.append(kind.getSimpleName());
  148. buf.append("(");
  149. buf.append(signature.toString());
  150. buf.append(")");
  151. return buf.toString();
  152. }
  153. public void postRead(ResolvedTypeX enclosingType) {
  154. signature.postRead(enclosingType);
  155. }
  156. public void write(DataOutputStream s) throws IOException {
  157. s.writeByte(Pointcut.KINDED);
  158. kind.write(s);
  159. signature.write(s);
  160. writeLocation(s);
  161. }
  162. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  163. Shadow.Kind kind = Shadow.Kind.read(s);
  164. SignaturePattern sig = SignaturePattern.read(s, context);
  165. KindedPointcut ret = new KindedPointcut(kind, sig);
  166. ret.readLocation(context, s);
  167. return ret;
  168. }
  169. // XXX note: there is no namebinding in any kinded pointcut.
  170. // still might want to do something for better error messages
  171. // We want to do something here to make sure we don't sidestep the parameter
  172. // list in capturing type identifiers.
  173. public void resolveBindings(IScope scope, Bindings bindings) {
  174. if (kind == Shadow.Initialization) {
  175. // scope.getMessageHandler().handleMessage(
  176. // MessageUtil.error(
  177. // "initialization unimplemented in 1.1beta1",
  178. // this.getSourceLocation()));
  179. }
  180. signature = signature.resolveBindings(scope, bindings);
  181. if (kind == Shadow.ConstructorExecution) { // Bug fix 60936
  182. if (signature.getDeclaringType() != null) {
  183. World world = scope.getWorld();
  184. TypeX exactType = signature.getDeclaringType().getExactType();
  185. if (signature.getKind() == Member.CONSTRUCTOR &&
  186. !exactType.equals(ResolvedTypeX.MISSING) &&
  187. exactType.isInterface(world) &&
  188. !signature.getDeclaringType().isIncludeSubtypes()) {
  189. world.getLint().noInterfaceCtorJoinpoint.signal(exactType.toString(), getSourceLocation());
  190. }
  191. }
  192. }
  193. }
  194. public void resolveBindingsFromRTTI() {
  195. signature = signature.resolveBindingsFromRTTI();
  196. }
  197. public Test findResidue(Shadow shadow, ExposedState state) {
  198. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  199. }
  200. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  201. Pointcut ret = new KindedPointcut(kind, signature, bindings.getEnclosingAdvice());
  202. ret.copyLocationFrom(this);
  203. return ret;
  204. }
  205. public Shadow.Kind getKind() {
  206. return kind;
  207. }
  208. }