Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ThisOrTargetPointcut.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.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. * Corresponds to target or this pcd.
  35. *
  36. * <p>
  37. * type is initially a WildTypePattern. If it stays that way, it's a this(Foo) type deal. however, the resolveBindings method may
  38. * convert it to a BindingTypePattern, in which case, it's a this(foo) type deal.
  39. *
  40. * @author Erik Hilsdale
  41. * @author Jim Hugunin
  42. */
  43. public class ThisOrTargetPointcut extends NameBindingPointcut {
  44. private boolean isThis;
  45. private TypePattern typePattern;
  46. private String declarationText;
  47. private static final int thisKindSet;
  48. private static final int targetKindSet;
  49. static {
  50. int thisFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  51. int targFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  52. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  53. Shadow.Kind kind = Shadow.SHADOW_KINDS[i];
  54. if (kind.neverHasThis()) {
  55. thisFlags -= kind.bit;
  56. }
  57. if (kind.neverHasTarget()) {
  58. targFlags -= kind.bit;
  59. }
  60. }
  61. thisKindSet = thisFlags;
  62. targetKindSet = targFlags;
  63. }
  64. public boolean isBinding() {
  65. return (typePattern instanceof BindingTypePattern);
  66. }
  67. public ThisOrTargetPointcut(boolean isThis, TypePattern type) {
  68. this.isThis = isThis;
  69. this.typePattern = type;
  70. this.pointcutKind = THIS_OR_TARGET;
  71. this.declarationText = (isThis ? "this(" : "target(") + type + ")";
  72. }
  73. public TypePattern getType() {
  74. return typePattern;
  75. }
  76. public boolean isThis() {
  77. return isThis;
  78. }
  79. @Override
  80. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  81. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, typePattern.parameterizeWith(typeVariableMap, w));
  82. ret.copyLocationFrom(this);
  83. return ret;
  84. }
  85. @Override
  86. public int couldMatchKinds() {
  87. return isThis ? thisKindSet : targetKindSet;
  88. }
  89. @Override
  90. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  91. return FuzzyBoolean.MAYBE;
  92. }
  93. private boolean couldMatch(Shadow shadow) {
  94. return isThis ? shadow.hasThis() : shadow.hasTarget();
  95. }
  96. @Override
  97. protected FuzzyBoolean matchInternal(Shadow shadow) {
  98. if (!couldMatch(shadow)) {
  99. return FuzzyBoolean.NO;
  100. }
  101. UnresolvedType typeToMatch = isThis ? shadow.getThisType() : shadow.getTargetType();
  102. // optimization for case of this(Object) or target(Object)
  103. // works for an ExactTypePattern (and we know there are no annotations to match here of course)
  104. if (typePattern.getExactType().equals(ResolvedType.OBJECT)) {
  105. return FuzzyBoolean.YES;
  106. }
  107. return typePattern.matches(typeToMatch.resolve(shadow.getIWorld()), TypePattern.DYNAMIC);
  108. }
  109. @Override
  110. public void write(CompressingDataOutputStream s) throws IOException {
  111. s.writeByte(Pointcut.THIS_OR_TARGET);
  112. s.writeBoolean(isThis);
  113. typePattern.write(s);
  114. writeLocation(s);
  115. }
  116. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  117. boolean isThis = s.readBoolean();
  118. TypePattern type = TypePattern.read(s, context);
  119. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, type);
  120. ret.readLocation(context, s);
  121. return ret;
  122. }
  123. @Override
  124. public void resolveBindings(IScope scope, Bindings bindings) {
  125. typePattern = typePattern.resolveBindings(scope, bindings, true, true);
  126. // look for parameterized type patterns which are not supported...
  127. HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
  128. typePattern.traverse(visitor, null);
  129. if (visitor.wellHasItThen/* ? */()) {
  130. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.THIS_AND_TARGET_DONT_SUPPORT_PARAMETERS),
  131. getSourceLocation()));
  132. }
  133. // ??? handle non-formal
  134. }
  135. @Override
  136. public void postRead(ResolvedType enclosingType) {
  137. typePattern.postRead(enclosingType);
  138. }
  139. @Override
  140. public List<BindingPattern> getBindingAnnotationTypePatterns() {
  141. return Collections.emptyList();
  142. }
  143. @Override
  144. public List<BindingTypePattern> getBindingTypePatterns() {
  145. if (typePattern instanceof BindingTypePattern) {
  146. List<BindingTypePattern> l = new ArrayList<>();
  147. l.add((BindingTypePattern)typePattern);
  148. return l;
  149. } else {
  150. return Collections.emptyList();
  151. }
  152. }
  153. @Override
  154. public boolean equals(Object other) {
  155. if (!(other instanceof ThisOrTargetPointcut)) {
  156. return false;
  157. }
  158. ThisOrTargetPointcut o = (ThisOrTargetPointcut) other;
  159. return o.isThis == this.isThis && o.typePattern.equals(this.typePattern);
  160. }
  161. @Override
  162. public int hashCode() {
  163. int result = 17;
  164. result = 37 * result + (isThis ? 0 : 1);
  165. result = 37 * result + typePattern.hashCode();
  166. return result;
  167. }
  168. @Override
  169. public String toString() {
  170. return declarationText;
  171. }
  172. /**
  173. * Residue is the remainder of the pointcut match that couldn't be performed with the purely static information at compile time
  174. * and this method returns the residue of a pointcut at a particular shadow.
  175. */
  176. @Override
  177. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  178. if (!couldMatch(shadow)) {
  179. return Literal.FALSE;
  180. }
  181. // if no preference is specified, just say TRUE which means no residue
  182. if (typePattern == TypePattern.ANY) {
  183. return Literal.TRUE;
  184. }
  185. Var var = isThis ? shadow.getThisVar() : shadow.getTargetVar();
  186. return exposeStateForVar(var, typePattern, state, shadow.getIWorld());
  187. }
  188. @Override
  189. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  190. if (isDeclare(bindings.getEnclosingAdvice())) {
  191. // Enforce rule about which designators are supported in declare
  192. inAspect.getWorld().showMessage(IMessage.ERROR,
  193. WeaverMessages.format(WeaverMessages.THIS_OR_TARGET_IN_DECLARE, isThis ? "this" : "target"),
  194. bindings.getEnclosingAdvice().getSourceLocation(), null);
  195. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  196. }
  197. TypePattern newType = typePattern.remapAdviceFormals(bindings);
  198. if (inAspect.crosscuttingMembers != null) {
  199. inAspect.crosscuttingMembers.exposeType(newType.getExactType());
  200. }
  201. Pointcut ret = new ThisOrTargetPointcut(isThis, newType);
  202. ret.copyLocationFrom(this);
  203. return ret;
  204. }
  205. @Override
  206. public Object accept(PatternNodeVisitor visitor, Object data) {
  207. return visitor.visit(this, data);
  208. }
  209. }