您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WithinCodeAnnotationPointcut.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.HashSet;
  16. import java.util.List;
  17. import java.util.Set;
  18. import org.aspectj.util.FuzzyBoolean;
  19. import org.aspectj.weaver.AnnotatedElement;
  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.ResolvedTypeX;
  26. import org.aspectj.weaver.Shadow;
  27. import org.aspectj.weaver.ShadowMunger;
  28. import org.aspectj.weaver.TypeX;
  29. import org.aspectj.weaver.VersionedDataInputStream;
  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
  37. * Window - Preferences - Java - Code Style - Code Templates
  38. */
  39. public class WithinCodeAnnotationPointcut extends NameBindingPointcut {
  40. private ExactAnnotationTypePattern annotationTypePattern;
  41. private ShadowMunger munger = null; // only set after concretization
  42. private static final Set matchedShadowKinds = new HashSet();
  43. static {
  44. matchedShadowKinds.addAll(Shadow.ALL_SHADOW_KINDS);
  45. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  46. if (Shadow.SHADOW_KINDS[i].isEnclosingKind())
  47. matchedShadowKinds.remove(Shadow.SHADOW_KINDS[i]);
  48. }
  49. }
  50. public WithinCodeAnnotationPointcut(ExactAnnotationTypePattern type) {
  51. super();
  52. this.annotationTypePattern = type;
  53. this.pointcutKind = Pointcut.ANNOTATION;
  54. }
  55. public WithinCodeAnnotationPointcut(ExactAnnotationTypePattern type, ShadowMunger munger) {
  56. this(type);
  57. this.munger = munger;
  58. }
  59. public Set couldMatchKinds() {
  60. return matchedShadowKinds;
  61. }
  62. /* (non-Javadoc)
  63. * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
  64. */
  65. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  66. return FuzzyBoolean.MAYBE;
  67. }
  68. /* (non-Javadoc)
  69. * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
  70. */
  71. protected FuzzyBoolean matchInternal(Shadow shadow) {
  72. AnnotatedElement toMatchAgainst = null;
  73. Member member = shadow.getEnclosingCodeSignature();
  74. ResolvedMember rMember = member.resolve(shadow.getIWorld());
  75. if (rMember == null) {
  76. if (member.getName().startsWith(NameMangler.PREFIX)) {
  77. return FuzzyBoolean.NO;
  78. }
  79. shadow.getIWorld().getLint().unresolvableMember.signal(member.toString(), getSourceLocation());
  80. return FuzzyBoolean.NO;
  81. }
  82. annotationTypePattern.resolve(shadow.getIWorld());
  83. return annotationTypePattern.matches(rMember);
  84. }
  85. /* (non-Javadoc)
  86. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings)
  87. */
  88. protected void resolveBindings(IScope scope, Bindings bindings) {
  89. annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope,bindings,true);
  90. // must be either a Var, or an annotation type pattern
  91. }
  92. /* (non-Javadoc)
  93. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindingsFromRTTI()
  94. */
  95. protected void resolveBindingsFromRTTI() {
  96. // TODO Auto-generated method stub
  97. }
  98. /* (non-Javadoc)
  99. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedTypeX, org.aspectj.weaver.IntMap)
  100. */
  101. protected Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  102. ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
  103. Pointcut ret = new WithinCodeAnnotationPointcut(newType, bindings.getEnclosingAdvice());
  104. ret.copyLocationFrom(this);
  105. return ret;
  106. }
  107. /* (non-Javadoc)
  108. * @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
  109. */
  110. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  111. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  112. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern)annotationTypePattern;
  113. TypeX annotationType = btp.annotationType;
  114. Var var = shadow.getWithinCodeAnnotationVar(annotationType);
  115. if (var == null) return Literal.FALSE;
  116. // Check if we have already bound something to this formal
  117. if ((state.get(btp.getFormalIndex())!=null) &&(lastMatchedShadowId == shadow.shadowId)) {
  118. // ISourceLocation pcdSloc = getSourceLocation();
  119. // ISourceLocation shadowSloc = shadow.getSourceLocation();
  120. // Message errorMessage = new Message(
  121. // "Cannot use @pointcut to match at this location and bind a formal to type '"+var.getType()+
  122. // "' - the formal is already bound to type '"+state.get(btp.getFormalIndex()).getType()+"'"+
  123. // ". The secondary source location points to the problematic binding.",
  124. // shadowSloc,true,new ISourceLocation[]{pcdSloc});
  125. // shadow.getIWorld().getMessageHandler().handleMessage(errorMessage);
  126. state.setErroneousVar(btp.getFormalIndex());
  127. }
  128. state.set(btp.getFormalIndex(),var);
  129. }
  130. return Literal.TRUE;
  131. }
  132. /* (non-Javadoc)
  133. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  134. */
  135. public List getBindingAnnotationTypePatterns() {
  136. if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  137. List l = new ArrayList();
  138. l.add(annotationTypePattern);
  139. return l;
  140. } else return Collections.EMPTY_LIST;
  141. }
  142. /* (non-Javadoc)
  143. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  144. */
  145. public List getBindingTypePatterns() {
  146. return Collections.EMPTY_LIST;
  147. }
  148. /* (non-Javadoc)
  149. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  150. */
  151. public void write(DataOutputStream s) throws IOException {
  152. s.writeByte(Pointcut.ATWITHINCODE);
  153. annotationTypePattern.write(s);
  154. writeLocation(s);
  155. }
  156. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  157. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  158. WithinCodeAnnotationPointcut ret = new WithinCodeAnnotationPointcut((ExactAnnotationTypePattern)type);
  159. ret.readLocation(context, s);
  160. return ret;
  161. }
  162. public boolean equals(Object other) {
  163. if (!(other instanceof WithinCodeAnnotationPointcut)) return false;
  164. WithinCodeAnnotationPointcut o = (WithinCodeAnnotationPointcut)other;
  165. return o.annotationTypePattern.equals(this.annotationTypePattern);
  166. }
  167. public int hashCode() {
  168. int result = 17;
  169. result = 23*result + annotationTypePattern.hashCode();
  170. return result;
  171. }
  172. public String toString() {
  173. StringBuffer buf = new StringBuffer();
  174. buf.append("@withincode(");
  175. buf.append(annotationTypePattern.toString());
  176. buf.append(")");
  177. return buf.toString();
  178. }
  179. }