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.

WithinAnnotationPointcut.java 7.5KB

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