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

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