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.

ThisOrTargetPointcut.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Map;
  18. import org.aspectj.bridge.IMessage;
  19. import org.aspectj.bridge.MessageUtil;
  20. import org.aspectj.util.FuzzyBoolean;
  21. import org.aspectj.weaver.CompressingDataOutputStream;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.IntMap;
  24. import org.aspectj.weaver.ResolvedType;
  25. import org.aspectj.weaver.Shadow;
  26. import org.aspectj.weaver.UnresolvedType;
  27. import org.aspectj.weaver.VersionedDataInputStream;
  28. import org.aspectj.weaver.WeaverMessages;
  29. import org.aspectj.weaver.World;
  30. import org.aspectj.weaver.ast.Literal;
  31. import org.aspectj.weaver.ast.Test;
  32. import org.aspectj.weaver.ast.Var;
  33. //
  34. /**
  35. * Corresponds to target or this pcd.
  36. *
  37. * <p>
  38. * type is initially a WildTypePattern. If it stays that way, it's a this(Foo) type deal. however, the resolveBindings method may
  39. * convert it to a BindingTypePattern, in which case, it's a this(foo) type deal.
  40. *
  41. * @author Erik Hilsdale
  42. * @author Jim Hugunin
  43. */
  44. public class ThisOrTargetPointcut extends NameBindingPointcut {
  45. private boolean isThis;
  46. private TypePattern typePattern;
  47. private String declarationText;
  48. private boolean optional = false;
  49. private static final int thisKindSet;
  50. private static final int targetKindSet;
  51. static {
  52. int thisFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  53. int targFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  54. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  55. Shadow.Kind kind = Shadow.SHADOW_KINDS[i];
  56. if (kind.neverHasThis()) {
  57. thisFlags -= kind.bit;
  58. }
  59. if (kind.neverHasTarget()) {
  60. targFlags -= kind.bit;
  61. }
  62. }
  63. thisKindSet = thisFlags;
  64. targetKindSet = targFlags;
  65. }
  66. public boolean isBinding() {
  67. return (typePattern instanceof BindingTypePattern);
  68. }
  69. public ThisOrTargetPointcut(boolean isThis, TypePattern type, boolean optional) {
  70. this.isThis = isThis;
  71. this.typePattern = type;
  72. this.pointcutKind = THIS_OR_TARGET;
  73. this.optional = optional;
  74. this.declarationText = (isThis ? "this(" : "target(") + type + ")";
  75. }
  76. public TypePattern getType() {
  77. return typePattern;
  78. }
  79. public boolean isThis() {
  80. return isThis;
  81. }
  82. @Override
  83. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  84. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, typePattern.parameterizeWith(typeVariableMap, w),optional);
  85. ret.copyLocationFrom(this);
  86. return ret;
  87. }
  88. @Override
  89. public int couldMatchKinds() {
  90. return isThis ? thisKindSet : targetKindSet;
  91. }
  92. @Override
  93. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  94. return FuzzyBoolean.MAYBE;
  95. }
  96. private boolean couldMatch(Shadow shadow) {
  97. return optional || (isThis ? shadow.hasThis() : shadow.hasTarget()) ;
  98. }
  99. @Override
  100. protected FuzzyBoolean matchInternal(Shadow shadow) {
  101. if (!couldMatch(shadow)) {
  102. return FuzzyBoolean.NO;
  103. }
  104. if (isThis?shadow.hasThis():shadow.hasTarget()) {
  105. UnresolvedType typeToMatch = isThis ? shadow.getThisType() : shadow.getTargetType();
  106. // optimization for case of this(Object) or target(Object)
  107. // works for an ExactTypePattern (and we know there are no annotations to match here of course)
  108. if (typePattern.getExactType().equals(ResolvedType.OBJECT)) {
  109. return FuzzyBoolean.YES;
  110. }
  111. return typePattern.matches(typeToMatch.resolve(shadow.getIWorld()), TypePattern.DYNAMIC);
  112. } else { // assert optional
  113. return FuzzyBoolean.YES;
  114. }
  115. }
  116. @Override
  117. public void write(CompressingDataOutputStream s) throws IOException {
  118. if (optional) {
  119. s.writeByte(Pointcut.THIS_OR_TARGET_WITH_OPTIONAL);
  120. } else {
  121. s.writeByte(Pointcut.THIS_OR_TARGET);
  122. }
  123. s.writeBoolean(isThis);
  124. typePattern.write(s);
  125. writeLocation(s);
  126. }
  127. public static Pointcut read(VersionedDataInputStream s, ISourceContext context, boolean optional) throws IOException {
  128. boolean isThis = s.readBoolean();
  129. TypePattern type = TypePattern.read(s, context);
  130. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, type, optional);
  131. ret.readLocation(context, s);
  132. return ret;
  133. }
  134. @Override
  135. public void resolveBindings(IScope scope, Bindings bindings) {
  136. typePattern = typePattern.resolveBindings(scope, bindings, true, true);
  137. // look for parameterized type patterns which are not supported...
  138. HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
  139. typePattern.traverse(visitor, null);
  140. if (visitor.wellHasItThen/* ? */()) {
  141. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.THIS_AND_TARGET_DONT_SUPPORT_PARAMETERS),
  142. getSourceLocation()));
  143. }
  144. // ??? handle non-formal
  145. }
  146. @Override
  147. public void postRead(ResolvedType enclosingType) {
  148. typePattern.postRead(enclosingType);
  149. }
  150. /*
  151. * (non-Javadoc)
  152. *
  153. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  154. */
  155. @Override
  156. public List<BindingAnnotationTypePattern> getBindingAnnotationTypePatterns() {
  157. return Collections.emptyList();
  158. }
  159. /*
  160. * (non-Javadoc)
  161. *
  162. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  163. */
  164. @Override
  165. public List<BindingTypePattern> getBindingTypePatterns() {
  166. if (typePattern instanceof BindingTypePattern) {
  167. List<BindingTypePattern> l = new ArrayList<BindingTypePattern>();
  168. l.add((BindingTypePattern)typePattern);
  169. return l;
  170. } else {
  171. return Collections.emptyList();
  172. }
  173. }
  174. @Override
  175. public boolean equals(Object other) {
  176. if (!(other instanceof ThisOrTargetPointcut)) {
  177. return false;
  178. }
  179. ThisOrTargetPointcut o = (ThisOrTargetPointcut) other;
  180. return o.isThis == this.isThis && o.typePattern.equals(this.typePattern);
  181. }
  182. @Override
  183. public int hashCode() {
  184. int result = 17;
  185. result = 37 * result + (isThis ? 0 : 1);
  186. result = 37 * result + typePattern.hashCode();
  187. return result;
  188. }
  189. @Override
  190. public String toString() {
  191. return declarationText;
  192. }
  193. /**
  194. * Residue is the remainder of the pointcut match that couldn't be performed with the purely static information at compile time
  195. * and this method returns the residue of a pointcut at a particular shadow.
  196. */
  197. @Override
  198. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  199. if (!couldMatch(shadow)) {
  200. return Literal.FALSE;
  201. }
  202. // if no preference is specified, just say TRUE which means no residue
  203. if (typePattern == TypePattern.ANY) {
  204. return Literal.TRUE;
  205. }
  206. if (isThis?shadow.hasThis():shadow.hasTarget()) {
  207. Var var = isThis ? shadow.getThisVar() : shadow.getTargetVar();
  208. return exposeStateForVar(var, typePattern, state, shadow.getIWorld());
  209. } else { // assert optional
  210. Var v = new NullVar(typePattern.getExactType().resolve(shadow.getIWorld()));
  211. // return exposeStateForVar(var, typePattern, state, shadow.getIWorld());
  212. if (typePattern instanceof BindingTypePattern) {
  213. BindingTypePattern b = (BindingTypePattern)typePattern;
  214. state.set(b.getFormalIndex(), v);
  215. }
  216. // ResolvedType myType = typePattern.getExactType().resolve(world);
  217. // if (myType.isParameterizedType()) {
  218. // // unchecked warning already issued...
  219. // myType = (ResolvedType) myType.getRawType();
  220. // }
  221. // return Test.makeInstanceof(var, myType.resolve(world));
  222. return Literal.TRUE;
  223. // return exposeStateForVar(null, typePattern, state, shadow.getIWorld());
  224. }
  225. }
  226. public static class NullVar extends Var {
  227. public NullVar(ResolvedType variableType) {
  228. super(variableType);
  229. // TODO Auto-generated constructor stub
  230. }
  231. }
  232. @Override
  233. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  234. if (isDeclare(bindings.getEnclosingAdvice())) {
  235. // Enforce rule about which designators are supported in declare
  236. inAspect.getWorld().showMessage(IMessage.ERROR,
  237. WeaverMessages.format(WeaverMessages.THIS_OR_TARGET_IN_DECLARE, isThis ? "this" : "target"),
  238. bindings.getEnclosingAdvice().getSourceLocation(), null);
  239. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  240. }
  241. TypePattern newType = typePattern.remapAdviceFormals(bindings);
  242. if (inAspect.crosscuttingMembers != null) {
  243. inAspect.crosscuttingMembers.exposeType(newType.getExactType());
  244. }
  245. Pointcut ret = new ThisOrTargetPointcut(isThis, newType, optional);
  246. ret.copyLocationFrom(this);
  247. return ret;
  248. }
  249. @Override
  250. public Object accept(PatternNodeVisitor visitor, Object data) {
  251. return visitor.visit(this, data);
  252. }
  253. }