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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  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 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. /*
  153. * (non-Javadoc)
  154. *
  155. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  156. */
  157. public List getBindingAnnotationTypePatterns() {
  158. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  159. List l = new ArrayList();
  160. l.add(annotationTypePattern);
  161. return l;
  162. } else {
  163. return Collections.EMPTY_LIST;
  164. }
  165. }
  166. /*
  167. * (non-Javadoc)
  168. *
  169. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  170. */
  171. public List getBindingTypePatterns() {
  172. return Collections.EMPTY_LIST;
  173. }
  174. /*
  175. * (non-Javadoc)
  176. *
  177. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  178. */
  179. public void write(CompressingDataOutputStream s) throws IOException {
  180. s.writeByte(Pointcut.ATWITHINCODE);
  181. annotationTypePattern.write(s);
  182. writeLocation(s);
  183. }
  184. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  185. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  186. WithinCodeAnnotationPointcut ret = new WithinCodeAnnotationPointcut((ExactAnnotationTypePattern) type);
  187. ret.readLocation(context, s);
  188. return ret;
  189. }
  190. public boolean equals(Object other) {
  191. if (!(other instanceof WithinCodeAnnotationPointcut)) {
  192. return false;
  193. }
  194. WithinCodeAnnotationPointcut o = (WithinCodeAnnotationPointcut) other;
  195. return o.annotationTypePattern.equals(this.annotationTypePattern);
  196. }
  197. public int hashCode() {
  198. int result = 17;
  199. result = 23 * result + annotationTypePattern.hashCode();
  200. return result;
  201. }
  202. private void buildDeclarationText() {
  203. StringBuffer buf = new StringBuffer();
  204. buf.append("@withincode(");
  205. String annPatt = annotationTypePattern.toString();
  206. buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
  207. buf.append(")");
  208. this.declarationText = buf.toString();
  209. }
  210. public String toString() {
  211. return this.declarationText;
  212. }
  213. public Object accept(PatternNodeVisitor visitor, Object data) {
  214. return visitor.visit(this, data);
  215. }
  216. }