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.

WithinCodeAnnotationPointcut.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  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. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.BCException;
  19. import org.aspectj.weaver.CompressingDataOutputStream;
  20. import org.aspectj.weaver.ISourceContext;
  21. import org.aspectj.weaver.IntMap;
  22. import org.aspectj.weaver.Member;
  23. import org.aspectj.weaver.NameMangler;
  24. import org.aspectj.weaver.ResolvedMember;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.Shadow;
  27. import org.aspectj.weaver.ShadowMunger;
  28. import org.aspectj.weaver.UnresolvedType;
  29. import org.aspectj.weaver.VersionedDataInputStream;
  30. import org.aspectj.weaver.WeaverMessages;
  31. import org.aspectj.weaver.World;
  32. import org.aspectj.weaver.ast.Literal;
  33. import org.aspectj.weaver.ast.Test;
  34. import org.aspectj.weaver.ast.Var;
  35. /**
  36. * @author colyer
  37. *
  38. * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
  39. * Templates
  40. */
  41. public class WithinCodeAnnotationPointcut extends NameBindingPointcut {
  42. private ExactAnnotationTypePattern annotationTypePattern;
  43. private String declarationText;
  44. private static final int matchedShadowKinds;
  45. static {
  46. int flags = Shadow.ALL_SHADOW_KINDS_BITS;
  47. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  48. if (Shadow.SHADOW_KINDS[i].isEnclosingKind()) {
  49. flags -= Shadow.SHADOW_KINDS[i].bit;
  50. }
  51. }
  52. matchedShadowKinds = flags;
  53. }
  54. public WithinCodeAnnotationPointcut(ExactAnnotationTypePattern type) {
  55. super();
  56. this.annotationTypePattern = type;
  57. this.pointcutKind = Pointcut.ATWITHINCODE;
  58. buildDeclarationText();
  59. }
  60. public WithinCodeAnnotationPointcut(ExactAnnotationTypePattern type, ShadowMunger munger) {
  61. this(type);
  62. this.pointcutKind = Pointcut.ATWITHINCODE;
  63. }
  64. public ExactAnnotationTypePattern getAnnotationTypePattern() {
  65. return annotationTypePattern;
  66. }
  67. public int couldMatchKinds() {
  68. return matchedShadowKinds;
  69. }
  70. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  71. WithinCodeAnnotationPointcut ret = new WithinCodeAnnotationPointcut((ExactAnnotationTypePattern) this.annotationTypePattern
  72. .parameterizeWith(typeVariableMap, w));
  73. ret.copyLocationFrom(this);
  74. return ret;
  75. }
  76. /*
  77. * (non-Javadoc)
  78. *
  79. * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
  80. */
  81. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  82. return FuzzyBoolean.MAYBE;
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
  88. */
  89. protected FuzzyBoolean matchInternal(Shadow shadow) {
  90. Member member = shadow.getEnclosingCodeSignature();
  91. ResolvedMember rMember = member.resolve(shadow.getIWorld());
  92. if (rMember == null) {
  93. if (member.getName().startsWith(NameMangler.PREFIX)) {
  94. return FuzzyBoolean.NO;
  95. }
  96. shadow.getIWorld().getLint().unresolvableMember.signal(member.toString(), getSourceLocation());
  97. return FuzzyBoolean.NO;
  98. }
  99. annotationTypePattern.resolve(shadow.getIWorld());
  100. return annotationTypePattern.matches(rMember);
  101. }
  102. /*
  103. * (non-Javadoc)
  104. *
  105. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope,
  106. * org.aspectj.weaver.patterns.Bindings)
  107. */
  108. protected void resolveBindings(IScope scope, Bindings bindings) {
  109. if (!scope.getWorld().isInJava5Mode()) {
  110. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.ATWITHINCODE_ONLY_SUPPORTED_AT_JAVA5_LEVEL),
  111. getSourceLocation()));
  112. return;
  113. }
  114. annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope, bindings, true);
  115. // must be either a Var, or an annotation type pattern
  116. }
  117. /*
  118. * (non-Javadoc)
  119. *
  120. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedType, org.aspectj.weaver.IntMap)
  121. */
  122. protected Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  123. ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
  124. Pointcut ret = new WithinCodeAnnotationPointcut(newType, bindings.getEnclosingAdvice());
  125. ret.copyLocationFrom(this);
  126. return ret;
  127. }
  128. /*
  129. * (non-Javadoc)
  130. *
  131. * @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
  132. */
  133. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  134. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  135. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern) annotationTypePattern;
  136. UnresolvedType annotationType = btp.annotationType;
  137. Var var = shadow.getWithinCodeAnnotationVar(annotationType);
  138. // This should not happen, we shouldn't have gotten this far
  139. // if we weren't going to find the annotation
  140. if (var == null) {
  141. throw new BCException("Impossible! annotation=[" + annotationType + "] shadow=[" + shadow + " at "
  142. + shadow.getSourceLocation() + "] pointcut is at [" + getSourceLocation() + "]");
  143. }
  144. state.set(btp.getFormalIndex(), var);
  145. }
  146. if (matchInternal(shadow).alwaysTrue()) {
  147. return Literal.TRUE;
  148. } else {
  149. return Literal.FALSE;
  150. }
  151. }
  152. public List<BindingPattern> getBindingAnnotationTypePatterns() {
  153. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  154. List<BindingPattern> l = new ArrayList<>();
  155. l.add((BindingPattern)annotationTypePattern);
  156. return l;
  157. } else {
  158. return Collections.emptyList();
  159. }
  160. }
  161. public List<BindingTypePattern> getBindingTypePatterns() {
  162. return Collections.emptyList();
  163. }
  164. public void write(CompressingDataOutputStream s) throws IOException {
  165. s.writeByte(Pointcut.ATWITHINCODE);
  166. annotationTypePattern.write(s);
  167. writeLocation(s);
  168. }
  169. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  170. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  171. WithinCodeAnnotationPointcut ret = new WithinCodeAnnotationPointcut((ExactAnnotationTypePattern) type);
  172. ret.readLocation(context, s);
  173. return ret;
  174. }
  175. public boolean equals(Object other) {
  176. if (!(other instanceof WithinCodeAnnotationPointcut)) {
  177. return false;
  178. }
  179. WithinCodeAnnotationPointcut o = (WithinCodeAnnotationPointcut) other;
  180. return o.annotationTypePattern.equals(this.annotationTypePattern);
  181. }
  182. public int hashCode() {
  183. int result = 17;
  184. result = 23 * result + annotationTypePattern.hashCode();
  185. return result;
  186. }
  187. private void buildDeclarationText() {
  188. StringBuilder buf = new StringBuilder();
  189. buf.append("@withincode(");
  190. String annPatt = annotationTypePattern.toString();
  191. buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
  192. buf.append(")");
  193. this.declarationText = buf.toString();
  194. }
  195. public String toString() {
  196. return this.declarationText;
  197. }
  198. public Object accept(PatternNodeVisitor visitor, Object data) {
  199. return visitor.visit(this, data);
  200. }
  201. @Override
  202. public Object traverse(PatternNodeVisitor visitor, Object data) {
  203. Object ret = accept(visitor, data);
  204. if (this.annotationTypePattern != null)
  205. this.annotationTypePattern.traverse(visitor, ret);
  206. return ret;
  207. }
  208. }