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 7.8KB

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