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.

AnnotationPointcut.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import java.io.DataOutputStream;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Collections;
  15. import java.util.List;
  16. import java.util.Set;
  17. import org.aspectj.bridge.IMessage;
  18. import org.aspectj.bridge.MessageUtil;
  19. import org.aspectj.util.FuzzyBoolean;
  20. import org.aspectj.weaver.AnnotatedElement;
  21. import org.aspectj.weaver.BCException;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.IntMap;
  24. import org.aspectj.weaver.Member;
  25. import org.aspectj.weaver.NameMangler;
  26. import org.aspectj.weaver.ResolvedMember;
  27. import org.aspectj.weaver.ResolvedTypeX;
  28. import org.aspectj.weaver.Shadow;
  29. import org.aspectj.weaver.ShadowMunger;
  30. import org.aspectj.weaver.TypeX;
  31. import org.aspectj.weaver.VersionedDataInputStream;
  32. import org.aspectj.weaver.ast.Literal;
  33. import org.aspectj.weaver.ast.Test;
  34. import org.aspectj.weaver.ast.Var;
  35. /**
  36. * @annotation(@Foo) or @annotation(foo)
  37. *
  38. * Matches any join point where the subject of the join point has an
  39. * annotation matching the annotationTypePattern:
  40. *
  41. * Join Point Kind Subject
  42. * ================================
  43. * method call the target method
  44. * method execution the method
  45. * constructor call the constructor
  46. * constructor execution the constructor
  47. * get the target field
  48. * set the target field
  49. * adviceexecution the advice
  50. * initialization the constructor
  51. * preinitialization the constructor
  52. * staticinitialization the type being initialized
  53. * handler the declared type of the handled exception
  54. */
  55. public class AnnotationPointcut extends NameBindingPointcut {
  56. private ExactAnnotationTypePattern annotationTypePattern;
  57. private ShadowMunger munger = null; // only set after concretization
  58. public AnnotationPointcut(ExactAnnotationTypePattern type) {
  59. super();
  60. this.annotationTypePattern = type;
  61. this.pointcutKind = Pointcut.ANNOTATION;
  62. }
  63. public AnnotationPointcut(ExactAnnotationTypePattern type, ShadowMunger munger) {
  64. this(type);
  65. this.munger = munger;
  66. }
  67. public Set couldMatchKinds() {
  68. return Shadow.ALL_SHADOW_KINDS;
  69. }
  70. /* (non-Javadoc)
  71. * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
  72. */
  73. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  74. if (info.getKind() == Shadow.StaticInitialization) {
  75. return annotationTypePattern.fastMatches(info.getType());
  76. } else {
  77. return FuzzyBoolean.MAYBE;
  78. }
  79. }
  80. /* (non-Javadoc)
  81. * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
  82. */
  83. protected FuzzyBoolean matchInternal(Shadow shadow) {
  84. AnnotatedElement toMatchAgainst = null;
  85. Member member = shadow.getSignature();
  86. ResolvedMember rMember = member.resolve(shadow.getIWorld());
  87. if (rMember == null) {
  88. if (member.getName().startsWith(NameMangler.PREFIX)) {
  89. return FuzzyBoolean.NO;
  90. }
  91. shadow.getIWorld().getLint().unresolvableMember.signal(member.toString(), getSourceLocation());
  92. return FuzzyBoolean.NO;
  93. }
  94. Shadow.Kind kind = shadow.getKind();
  95. if (kind == Shadow.StaticInitialization) {
  96. toMatchAgainst = rMember.getDeclaringType().resolve(shadow.getIWorld());
  97. } else if ( (kind == Shadow.ExceptionHandler)) {
  98. toMatchAgainst = rMember.getParameterTypes()[0].resolve(shadow.getIWorld());
  99. } else {
  100. toMatchAgainst = rMember;
  101. }
  102. annotationTypePattern.resolve(shadow.getIWorld());
  103. return annotationTypePattern.matches(toMatchAgainst);
  104. }
  105. /* (non-Javadoc)
  106. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings)
  107. */
  108. protected void resolveBindings(IScope scope, Bindings bindings) {
  109. annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope,bindings,true);
  110. // must be either a Var, or an annotation type pattern
  111. }
  112. /* (non-Javadoc)
  113. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindingsFromRTTI()
  114. */
  115. protected void resolveBindingsFromRTTI() {
  116. // TODO Auto-generated method stub
  117. }
  118. /* (non-Javadoc)
  119. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedTypeX, org.aspectj.weaver.IntMap)
  120. */
  121. protected Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  122. ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
  123. Pointcut ret = new AnnotationPointcut(newType, bindings.getEnclosingAdvice());
  124. ret.copyLocationFrom(this);
  125. return ret;
  126. }
  127. /* (non-Javadoc)
  128. * @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
  129. */
  130. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  131. if (shadow.getKind()!=Shadow.MethodCall &&
  132. shadow.getKind()!=Shadow.ConstructorCall &&
  133. shadow.getKind()!=Shadow.ConstructorExecution &&
  134. shadow.getKind()!=Shadow.MethodExecution &&
  135. shadow.getKind()!=Shadow.FieldSet &&
  136. shadow.getKind()!=Shadow.FieldGet &&
  137. shadow.getKind()!=Shadow.StaticInitialization &&
  138. shadow.getKind()!=Shadow.PreInitialization &&
  139. shadow.getKind()!=Shadow.AdviceExecution &&
  140. shadow.getKind()!=Shadow.Initialization &&
  141. shadow.getKind()!=Shadow.ExceptionHandler
  142. ) {
  143. IMessage lim = MessageUtil.error("Binding not supported in @pcds (1.5.0 M2 limitation) for "+shadow.getKind()+" join points, see: " +
  144. getSourceLocation());
  145. shadow.getIWorld().getMessageHandler().handleMessage(lim);
  146. throw new BCException("Binding not supported in @pcds (1.5.0 M2 limitation) for "+shadow.getKind()+" join points, see: " +
  147. getSourceLocation());
  148. }
  149. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  150. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern)annotationTypePattern;
  151. TypeX annotationType = btp.annotationType;
  152. Var var = shadow.getKindedAnnotationVar(annotationType);
  153. // This should not happen, we shouldn't have gotten this var
  154. // if we weren't going to find the annotation
  155. if (var == null) throw new BCException("Impossible! annotation=["+annotationType+
  156. "] shadow=["+shadow+" at "+shadow.getSourceLocation()+
  157. "] pointcut is at ["+getSourceLocation()+"]");//return Literal.FALSE;
  158. // Check if we have already bound something to this formal
  159. if ((state.get(btp.getFormalIndex())!=null) &&(lastMatchedShadowId == shadow.shadowId)) {
  160. // ISourceLocation pcdSloc = getSourceLocation();
  161. // ISourceLocation shadowSloc = shadow.getSourceLocation();
  162. // Message errorMessage = new Message(
  163. // "Cannot use @pointcut to match at this location and bind a formal to type '"+var.getType()+
  164. // "' - the formal is already bound to type '"+state.get(btp.getFormalIndex()).getType()+"'"+
  165. // ". The secondary source location points to the problematic binding.",
  166. // shadowSloc,true,new ISourceLocation[]{pcdSloc});
  167. // shadow.getIWorld().getMessageHandler().handleMessage(errorMessage);
  168. state.setErroneousVar(btp.getFormalIndex());
  169. }
  170. state.set(btp.getFormalIndex(),var);
  171. }
  172. return Literal.TRUE;
  173. }
  174. /* (non-Javadoc)
  175. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  176. */
  177. public List getBindingAnnotationTypePatterns() {
  178. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  179. List l = new ArrayList();
  180. l.add(annotationTypePattern);
  181. return l;
  182. } else return Collections.EMPTY_LIST;
  183. }
  184. /* (non-Javadoc)
  185. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  186. */
  187. public List getBindingTypePatterns() {
  188. return Collections.EMPTY_LIST;
  189. }
  190. /* (non-Javadoc)
  191. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  192. */
  193. public void write(DataOutputStream s) throws IOException {
  194. s.writeByte(Pointcut.ANNOTATION);
  195. annotationTypePattern.write(s);
  196. writeLocation(s);
  197. }
  198. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  199. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  200. AnnotationPointcut ret = new AnnotationPointcut((ExactAnnotationTypePattern)type);
  201. ret.readLocation(context, s);
  202. return ret;
  203. }
  204. public boolean equals(Object other) {
  205. if (!(other instanceof AnnotationPointcut)) return false;
  206. AnnotationPointcut o = (AnnotationPointcut)other;
  207. return o.annotationTypePattern.equals(this.annotationTypePattern);
  208. }
  209. public int hashCode() {
  210. int result = 17;
  211. result = 37*result + annotationTypePattern.hashCode();
  212. return result;
  213. }
  214. public String toString() {
  215. StringBuffer buf = new StringBuffer();
  216. buf.append("@annotation(");
  217. buf.append(annotationTypePattern.toString());
  218. buf.append(")");
  219. return buf.toString();
  220. }
  221. }