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.

ArgsAnnotationPointcut.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.ISourceLocation;
  19. import org.aspectj.bridge.Message;
  20. import org.aspectj.util.FuzzyBoolean;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.IntMap;
  23. import org.aspectj.weaver.ResolvedTypeX;
  24. import org.aspectj.weaver.Shadow;
  25. import org.aspectj.weaver.TypeX;
  26. import org.aspectj.weaver.VersionedDataInputStream;
  27. import org.aspectj.weaver.WeaverMessages;
  28. import org.aspectj.weaver.ast.Literal;
  29. import org.aspectj.weaver.ast.Test;
  30. /**
  31. * @author colyer
  32. *
  33. * TODO To change the template for this generated type comment go to
  34. * Window - Preferences - Java - Code Style - Code Templates
  35. */
  36. public class ArgsAnnotationPointcut extends NameBindingPointcut {
  37. private AnnotationPatternList arguments;
  38. /**
  39. *
  40. */
  41. public ArgsAnnotationPointcut(AnnotationPatternList arguments) {
  42. super();
  43. this.arguments = arguments;
  44. }
  45. public Set couldMatchKinds() {
  46. return Shadow.ALL_SHADOW_KINDS; // empty args() matches jps with no args
  47. }
  48. /* (non-Javadoc)
  49. * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
  50. */
  51. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  52. return FuzzyBoolean.MAYBE;
  53. }
  54. /* (non-Javadoc)
  55. * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
  56. */
  57. protected FuzzyBoolean matchInternal(Shadow shadow) {
  58. arguments.resolve(shadow.getIWorld());
  59. FuzzyBoolean ret =
  60. arguments.matches(shadow.getIWorld().resolve(shadow.getArgTypes()));
  61. return ret;
  62. }
  63. /* (non-Javadoc)
  64. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope, org.aspectj.weaver.patterns.Bindings)
  65. */
  66. protected void resolveBindings(IScope scope, Bindings bindings) {
  67. arguments.resolveBindings(scope, bindings, true);
  68. if (arguments.ellipsisCount > 1) {
  69. scope.message(IMessage.ERROR, this,
  70. "uses more than one .. in args (compiler limitation)");
  71. }
  72. }
  73. /* (non-Javadoc)
  74. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindingsFromRTTI()
  75. */
  76. protected void resolveBindingsFromRTTI() {
  77. // TODO Auto-generated method stub
  78. }
  79. /* (non-Javadoc)
  80. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedTypeX, org.aspectj.weaver.IntMap)
  81. */
  82. protected Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  83. if (isDeclare(bindings.getEnclosingAdvice())) {
  84. // Enforce rule about which designators are supported in declare
  85. inAspect.getWorld().showMessage(IMessage.ERROR,
  86. WeaverMessages.format(WeaverMessages.ARGS_IN_DECLARE),
  87. bindings.getEnclosingAdvice().getSourceLocation(), null);
  88. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  89. }
  90. AnnotationPatternList list = arguments.resolveReferences(bindings);
  91. Pointcut ret = new ArgsAnnotationPointcut(list);
  92. ret.copyLocationFrom(this);
  93. return ret;
  94. }
  95. /* (non-Javadoc)
  96. * @see org.aspectj.weaver.patterns.Pointcut#findResidue(org.aspectj.weaver.Shadow, org.aspectj.weaver.patterns.ExposedState)
  97. */
  98. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  99. int len = shadow.getArgCount();
  100. // do some quick length tests first
  101. int numArgsMatchedByEllipsis = (len + arguments.ellipsisCount) - arguments.size();
  102. if (numArgsMatchedByEllipsis < 0) return Literal.FALSE; // should never happen
  103. if ((numArgsMatchedByEllipsis > 0) && (arguments.ellipsisCount == 0)) {
  104. return Literal.FALSE; // should never happen
  105. }
  106. // now work through the args and the patterns, skipping at ellipsis
  107. Test ret = Literal.TRUE;
  108. int argsIndex = 0;
  109. for (int i = 0; i < arguments.size(); i++) {
  110. if (arguments.get(i) == AnnotationTypePattern.ELLIPSIS) {
  111. // match ellipsisMatchCount args
  112. argsIndex += numArgsMatchedByEllipsis;
  113. } else if (arguments.get(i) == AnnotationTypePattern.ANY) {
  114. argsIndex++;
  115. } else {
  116. // match the argument type at argsIndex with the ExactAnnotationTypePattern
  117. // we know it is exact because nothing else is allowed in args
  118. ExactAnnotationTypePattern ap = (ExactAnnotationTypePattern)arguments.get(i);
  119. TypeX argType = shadow.getArgType(argsIndex);
  120. ResolvedTypeX rArgType = argType.resolve(shadow.getIWorld());
  121. if (rArgType == ResolvedTypeX.MISSING) {
  122. IMessage msg = new Message(
  123. WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_ARG_TYPE,argType.getName()),
  124. "",IMessage.ERROR,shadow.getSourceLocation(),null,new ISourceLocation[]{getSourceLocation()});
  125. }
  126. if (ap.matches(rArgType).alwaysTrue()) {
  127. argsIndex++;
  128. continue;
  129. } else {
  130. // we need a test...
  131. // TODO: binding
  132. ResolvedTypeX rAnnType = ap.annotationType.resolve(shadow.getIWorld());
  133. ret = Test.makeAnd(ret,Test.makeHasAnnotation(shadow.getArgVar(argsIndex),rAnnType));
  134. argsIndex++;
  135. }
  136. }
  137. }
  138. return ret;
  139. }
  140. /* (non-Javadoc)
  141. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  142. */
  143. public List getBindingAnnotationTypePatterns() {
  144. List l = new ArrayList();
  145. AnnotationTypePattern[] pats = arguments.getAnnotationPatterns();
  146. for (int i = 0; i < pats.length; i++) {
  147. if (pats[i] instanceof BindingAnnotationTypePattern) {
  148. l.add(pats[i]);
  149. }
  150. }
  151. return l;
  152. }
  153. /* (non-Javadoc)
  154. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  155. */
  156. public List getBindingTypePatterns() {
  157. return Collections.EMPTY_LIST;
  158. }
  159. /* (non-Javadoc)
  160. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  161. */
  162. public void write(DataOutputStream s) throws IOException {
  163. s.writeByte(Pointcut.ATARGS);
  164. arguments.write(s);
  165. writeLocation(s);
  166. }
  167. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  168. AnnotationPatternList annotationPatternList = AnnotationPatternList.read(s,context);
  169. ArgsAnnotationPointcut ret = new ArgsAnnotationPointcut(annotationPatternList);
  170. ret.readLocation(context, s);
  171. return ret;
  172. }
  173. /* (non-Javadoc)
  174. * @see java.lang.Object#equals(java.lang.Object)
  175. */
  176. public boolean equals(Object obj) {
  177. if (!(obj instanceof ArgsAnnotationPointcut)) return false;
  178. ArgsAnnotationPointcut other = (ArgsAnnotationPointcut) obj;
  179. return other.arguments.equals(arguments);
  180. }
  181. /* (non-Javadoc)
  182. * @see java.lang.Object#hashCode()
  183. */
  184. public int hashCode() {
  185. return 17 + 37*arguments.hashCode();
  186. }
  187. /* (non-Javadoc)
  188. * @see java.lang.Object#toString()
  189. */
  190. public String toString() {
  191. StringBuffer buf = new StringBuffer("@args");
  192. buf.append(arguments.toString());
  193. return buf.toString();
  194. }
  195. }