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.0KB

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