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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 java.util.HashSet;
  17. import java.util.Set;
  18. import org.aspectj.bridge.ISourceLocation;
  19. import org.aspectj.lang.JoinPoint;
  20. import org.aspectj.util.FuzzyBoolean;
  21. import org.aspectj.weaver.Checker;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.IntMap;
  24. import org.aspectj.weaver.Member;
  25. import org.aspectj.weaver.ResolvedMember;
  26. import org.aspectj.weaver.ResolvedTypeX;
  27. import org.aspectj.weaver.Shadow;
  28. import org.aspectj.weaver.ShadowMunger;
  29. import org.aspectj.weaver.TypeX;
  30. import org.aspectj.weaver.World;
  31. import org.aspectj.weaver.ast.Literal;
  32. import org.aspectj.weaver.ast.Test;
  33. public class KindedPointcut extends Pointcut {
  34. Shadow.Kind kind;
  35. SignaturePattern signature;
  36. private Set matchKinds;
  37. private ShadowMunger munger = null; // only set after concretization
  38. public KindedPointcut(
  39. Shadow.Kind kind,
  40. SignaturePattern signature) {
  41. this.kind = kind;
  42. this.signature = signature;
  43. this.pointcutKind = KINDED;
  44. this.matchKinds = new HashSet();
  45. matchKinds.add(kind);
  46. }
  47. public KindedPointcut(
  48. Shadow.Kind kind,
  49. SignaturePattern signature,
  50. ShadowMunger munger)
  51. {
  52. this(kind, signature);
  53. this.munger = munger;
  54. }
  55. /* (non-Javadoc)
  56. * @see org.aspectj.weaver.patterns.Pointcut#couldMatchKinds()
  57. */
  58. public Set couldMatchKinds() {
  59. return matchKinds;
  60. }
  61. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  62. if (info.getKind() != null) {
  63. if (info.getKind() != kind) return FuzzyBoolean.NO;
  64. }
  65. return FuzzyBoolean.MAYBE;
  66. }
  67. protected FuzzyBoolean matchInternal(Shadow shadow) {
  68. if (shadow.getKind() != kind) return FuzzyBoolean.NO;
  69. if (!signature.matches(shadow.getSignature(), shadow.getIWorld())){
  70. if(kind == Shadow.MethodCall) {
  71. warnOnConfusingSig(shadow);
  72. warnOnBridgeMethod(shadow);
  73. }
  74. return FuzzyBoolean.NO;
  75. }
  76. return FuzzyBoolean.YES;
  77. }
  78. private void warnOnBridgeMethod(Shadow shadow) {
  79. if (shadow.getIWorld().getLint().noJoinpointsForBridgeMethods.isEnabled()) {
  80. ResolvedMember rm = shadow.getSignature().resolve(shadow.getIWorld());
  81. if (rm!=null) {
  82. int shadowModifiers = shadow.getSignature().getModifiers(shadow.getIWorld());
  83. if (ResolvedTypeX.hasBridgeModifier(shadowModifiers)) {
  84. shadow.getIWorld().getLint().noJoinpointsForBridgeMethods.signal(new String[]{},getSourceLocation(),
  85. new ISourceLocation[]{shadow.getSourceLocation()});
  86. }
  87. }
  88. }
  89. }
  90. public FuzzyBoolean match(JoinPoint.StaticPart jpsp) {
  91. if (jpsp.getKind().equals(kind.getName())) {
  92. if (signature.matches(jpsp)) {
  93. return FuzzyBoolean.YES;
  94. }
  95. }
  96. return FuzzyBoolean.NO;
  97. }
  98. /* (non-Javadoc)
  99. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  100. */
  101. public boolean matchesDynamically(Object thisObject, Object targetObject,
  102. Object[] args) {
  103. return true;
  104. }
  105. /* (non-Javadoc)
  106. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  107. */
  108. public FuzzyBoolean matchesStatically(String joinpointKind,
  109. java.lang.reflect.Member member, Class thisClass,
  110. Class targetClass, java.lang.reflect.Member withinCode) {
  111. if (joinpointKind.equals(kind.getName())) {
  112. return FuzzyBoolean.fromBoolean(signature.matches(targetClass,member));
  113. }
  114. return FuzzyBoolean.NO;
  115. }
  116. private void warnOnConfusingSig(Shadow shadow) {
  117. // Don't do all this processing if we don't need to !
  118. if (!shadow.getIWorld().getLint().unmatchedSuperTypeInCall.isEnabled()) return;
  119. // no warnings for declare error/warning
  120. if (munger instanceof Checker) return;
  121. World world = shadow.getIWorld();
  122. // warning never needed if the declaring type is any
  123. TypeX exactDeclaringType = signature.getDeclaringType().getExactType();
  124. ResolvedTypeX shadowDeclaringType =
  125. shadow.getSignature().getDeclaringType().resolve(world);
  126. if (signature.getDeclaringType().isStar()
  127. || exactDeclaringType== ResolvedTypeX.MISSING)
  128. return;
  129. // warning not needed if match type couldn't ever be the declaring type
  130. if (!shadowDeclaringType.isAssignableFrom(exactDeclaringType)) {
  131. return;
  132. }
  133. // if the method in the declaring type is *not* visible to the
  134. // exact declaring type then warning not needed.
  135. int shadowModifiers = shadow.getSignature().getModifiers(world);
  136. if (!ResolvedTypeX
  137. .isVisible(
  138. shadowModifiers,
  139. shadowDeclaringType,
  140. exactDeclaringType.resolve(world))) {
  141. return;
  142. }
  143. if (!signature.getReturnType().matchesStatically(shadow.getSignature().getReturnType().resolve(world))) {
  144. // Covariance issue...
  145. // The reason we didn't match is that the type pattern for the pointcut (Car) doesn't match the
  146. // return type for the specific declaration at the shadow. (FastCar Sub.getCar())
  147. // XXX Put out another XLINT in this case?
  148. return;
  149. }
  150. // PR60015 - Don't report the warning if the declaring type is object and 'this' is an interface
  151. if (exactDeclaringType.isInterface(world) && shadowDeclaringType.equals(world.resolve("java.lang.Object"))) {
  152. return;
  153. }
  154. SignaturePattern nonConfusingPattern =
  155. new SignaturePattern(
  156. signature.getKind(),
  157. signature.getModifiers(),
  158. signature.getReturnType(),
  159. TypePattern.ANY,
  160. signature.getName(),
  161. signature.getParameterTypes(),
  162. signature.getThrowsPattern(),
  163. signature.getAnnotationPattern());
  164. if (nonConfusingPattern
  165. .matches(shadow.getSignature(), shadow.getIWorld())) {
  166. shadow.getIWorld().getLint().unmatchedSuperTypeInCall.signal(
  167. new String[] {
  168. shadow.getSignature().getDeclaringType().toString(),
  169. signature.getDeclaringType().toString()
  170. },
  171. this.getSourceLocation(),
  172. new ISourceLocation[] {shadow.getSourceLocation()} );
  173. }
  174. }
  175. public boolean equals(Object other) {
  176. if (!(other instanceof KindedPointcut)) return false;
  177. KindedPointcut o = (KindedPointcut)other;
  178. return o.kind == this.kind && o.signature.equals(this.signature);
  179. }
  180. public int hashCode() {
  181. int result = 17;
  182. result = 37*result + kind.hashCode();
  183. result = 37*result + signature.hashCode();
  184. return result;
  185. }
  186. public String toString() {
  187. StringBuffer buf = new StringBuffer();
  188. buf.append(kind.getSimpleName());
  189. buf.append("(");
  190. buf.append(signature.toString());
  191. buf.append(")");
  192. return buf.toString();
  193. }
  194. public void postRead(ResolvedTypeX enclosingType) {
  195. signature.postRead(enclosingType);
  196. }
  197. public void write(DataOutputStream s) throws IOException {
  198. s.writeByte(Pointcut.KINDED);
  199. kind.write(s);
  200. signature.write(s);
  201. writeLocation(s);
  202. }
  203. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  204. Shadow.Kind kind = Shadow.Kind.read(s);
  205. SignaturePattern sig = SignaturePattern.read(s, context);
  206. KindedPointcut ret = new KindedPointcut(kind, sig);
  207. ret.readLocation(context, s);
  208. return ret;
  209. }
  210. // XXX note: there is no namebinding in any kinded pointcut.
  211. // still might want to do something for better error messages
  212. // We want to do something here to make sure we don't sidestep the parameter
  213. // list in capturing type identifiers.
  214. public void resolveBindings(IScope scope, Bindings bindings) {
  215. if (kind == Shadow.Initialization) {
  216. // scope.getMessageHandler().handleMessage(
  217. // MessageUtil.error(
  218. // "initialization unimplemented in 1.1beta1",
  219. // this.getSourceLocation()));
  220. }
  221. signature = signature.resolveBindings(scope, bindings);
  222. if (kind == Shadow.ConstructorExecution) { // Bug fix 60936
  223. if (signature.getDeclaringType() != null) {
  224. World world = scope.getWorld();
  225. TypeX exactType = signature.getDeclaringType().getExactType();
  226. if (signature.getKind() == Member.CONSTRUCTOR &&
  227. !exactType.equals(ResolvedTypeX.MISSING) &&
  228. exactType.isInterface(world) &&
  229. !signature.getDeclaringType().isIncludeSubtypes()) {
  230. world.getLint().noInterfaceCtorJoinpoint.signal(exactType.toString(), getSourceLocation());
  231. }
  232. }
  233. }
  234. }
  235. public void resolveBindingsFromRTTI() {
  236. signature = signature.resolveBindingsFromRTTI();
  237. }
  238. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  239. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  240. }
  241. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  242. Pointcut ret = new KindedPointcut(kind, signature, bindings.getEnclosingAdvice());
  243. ret.copyLocationFrom(this);
  244. return ret;
  245. }
  246. public Shadow.Kind getKind() {
  247. return kind;
  248. }
  249. }