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.

ArgsPointcut.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  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. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.lang.reflect.Constructor;
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.Member;
  18. import java.lang.reflect.Method;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Set;
  25. import org.aspectj.bridge.IMessage;
  26. import org.aspectj.bridge.ISourceLocation;
  27. import org.aspectj.bridge.Message;
  28. import org.aspectj.lang.JoinPoint;
  29. import org.aspectj.lang.reflect.CodeSignature;
  30. import org.aspectj.util.FuzzyBoolean;
  31. import org.aspectj.weaver.BetaException;
  32. import org.aspectj.weaver.ISourceContext;
  33. import org.aspectj.weaver.IntMap;
  34. import org.aspectj.weaver.ResolvedTypeX;
  35. import org.aspectj.weaver.Shadow;
  36. import org.aspectj.weaver.TypeX;
  37. import org.aspectj.weaver.VersionedDataInputStream;
  38. import org.aspectj.weaver.WeaverMessages;
  39. import org.aspectj.weaver.ast.Literal;
  40. import org.aspectj.weaver.ast.Test;
  41. import org.aspectj.weaver.internal.tools.PointcutExpressionImpl;
  42. /**
  43. * args(arguments)
  44. *
  45. * @author Erik Hilsdale
  46. * @author Jim Hugunin
  47. */
  48. public class ArgsPointcut extends NameBindingPointcut {
  49. TypePatternList arguments;
  50. public ArgsPointcut(TypePatternList arguments) {
  51. this.arguments = arguments;
  52. this.pointcutKind = ARGS;
  53. }
  54. public Set couldMatchKinds() {
  55. return Shadow.ALL_SHADOW_KINDS; // empty args() matches jps with no args
  56. }
  57. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  58. return FuzzyBoolean.MAYBE;
  59. }
  60. protected FuzzyBoolean matchInternal(Shadow shadow) {
  61. FuzzyBoolean ret =
  62. arguments.matches(shadow.getIWorld().resolve(shadow.getArgTypes()), TypePattern.DYNAMIC);
  63. return ret;
  64. }
  65. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart jpsp) {
  66. FuzzyBoolean ret = arguments.matches(jp.getArgs(),TypePattern.DYNAMIC);
  67. // this may have given a false match (e.g. args(int) may have matched a call to doIt(Integer x)) due to boxing
  68. // check for this...
  69. if (ret == FuzzyBoolean.YES) {
  70. // are the sigs compatible too...
  71. CodeSignature sig = (CodeSignature)jp.getSignature();
  72. Class[] pTypes = sig.getParameterTypes();
  73. ret = checkSignatureMatch(pTypes);
  74. }
  75. return ret;
  76. }
  77. /**
  78. * @param ret
  79. * @param pTypes
  80. * @return
  81. */
  82. private FuzzyBoolean checkSignatureMatch(Class[] pTypes) {
  83. Collection tps = arguments.getExactTypes();
  84. int sigIndex = 0;
  85. for (Iterator iter = tps.iterator(); iter.hasNext();) {
  86. TypeX tp = (TypeX) iter.next();
  87. Class lookForClass = getPossiblyBoxed(tp);
  88. if (lookForClass != null) {
  89. boolean foundMatchInSig = false;
  90. while (sigIndex < pTypes.length && !foundMatchInSig) {
  91. if (pTypes[sigIndex++] == lookForClass) foundMatchInSig = true;
  92. }
  93. if (!foundMatchInSig) {
  94. return FuzzyBoolean.NO;
  95. }
  96. }
  97. }
  98. return FuzzyBoolean.YES;
  99. }
  100. /* (non-Javadoc)
  101. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  102. */
  103. public boolean matchesDynamically(Object thisObject, Object targetObject,
  104. Object[] args) {
  105. return (arguments.matches(args,TypePattern.DYNAMIC) == FuzzyBoolean.YES);
  106. }
  107. /* (non-Javadoc)
  108. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  109. */
  110. public FuzzyBoolean matchesStatically(String joinpointKind, Member member,
  111. Class thisClass, Class targetClass, Member withinCode) {
  112. Class[] paramTypes = new Class[0];
  113. if (member instanceof Method) {
  114. paramTypes = ((Method)member).getParameterTypes();
  115. } else if (member instanceof Constructor) {
  116. paramTypes = ((Constructor)member).getParameterTypes();
  117. } else if (member instanceof PointcutExpressionImpl.Handler){
  118. paramTypes = new Class[] {((PointcutExpressionImpl.Handler)member).getHandledExceptionType()};
  119. } else if (member instanceof Field) {
  120. if (joinpointKind.equals(Shadow.FieldGet.getName())) return FuzzyBoolean.NO; // no args here
  121. paramTypes = new Class[] {((Field)member).getType()};
  122. } else {
  123. return FuzzyBoolean.NO;
  124. }
  125. return arguments.matchesArgsPatternSubset(paramTypes);
  126. }
  127. private Class getPossiblyBoxed(TypeX tp) {
  128. Class ret = (Class) ExactTypePattern.primitiveTypesMap.get(tp.getName());
  129. if (ret == null) ret = (Class) ExactTypePattern.boxedPrimitivesMap.get(tp.getName());
  130. return ret;
  131. }
  132. /* (non-Javadoc)
  133. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  134. */
  135. public List getBindingAnnotationTypePatterns() {
  136. return Collections.EMPTY_LIST;
  137. }
  138. /* (non-Javadoc)
  139. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  140. */
  141. public List getBindingTypePatterns() {
  142. List l = new ArrayList();
  143. TypePattern[] pats = arguments.getTypePatterns();
  144. for (int i = 0; i < pats.length; i++) {
  145. if (pats[i] instanceof BindingTypePattern) {
  146. l.add(pats[i]);
  147. }
  148. }
  149. return l;
  150. }
  151. public void write(DataOutputStream s) throws IOException {
  152. s.writeByte(Pointcut.ARGS);
  153. arguments.write(s);
  154. writeLocation(s);
  155. }
  156. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  157. ArgsPointcut ret = new ArgsPointcut(TypePatternList.read(s, context));
  158. ret.readLocation(context, s);
  159. return ret;
  160. }
  161. public boolean equals(Object other) {
  162. if (!(other instanceof ArgsPointcut)) return false;
  163. ArgsPointcut o = (ArgsPointcut)other;
  164. return o.arguments.equals(this.arguments);
  165. }
  166. public int hashCode() {
  167. return arguments.hashCode();
  168. }
  169. public void resolveBindings(IScope scope, Bindings bindings) {
  170. arguments.resolveBindings(scope, bindings, true, true);
  171. if (arguments.ellipsisCount > 1) {
  172. scope.message(IMessage.ERROR, this,
  173. "uses more than one .. in args (compiler limitation)");
  174. }
  175. }
  176. public void resolveBindingsFromRTTI() {
  177. arguments.resolveBindingsFromRTTI(true, true);
  178. if (arguments.ellipsisCount > 1) {
  179. throw new UnsupportedOperationException("uses more than one .. in args (compiler limitation)");
  180. }
  181. }
  182. public void postRead(ResolvedTypeX enclosingType) {
  183. arguments.postRead(enclosingType);
  184. }
  185. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  186. if (isDeclare(bindings.getEnclosingAdvice())) {
  187. // Enforce rule about which designators are supported in declare
  188. inAspect.getWorld().showMessage(IMessage.ERROR,
  189. WeaverMessages.format(WeaverMessages.ARGS_IN_DECLARE),
  190. bindings.getEnclosingAdvice().getSourceLocation(), null);
  191. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  192. }
  193. TypePatternList args = arguments.resolveReferences(bindings);
  194. if (inAspect.crosscuttingMembers != null) {
  195. inAspect.crosscuttingMembers.exposeTypes(args.getExactTypes());
  196. }
  197. Pointcut ret = new ArgsPointcut(args);
  198. ret.copyLocationFrom(this);
  199. return ret;
  200. }
  201. private Test findResidueNoEllipsis(Shadow shadow, ExposedState state, TypePattern[] patterns) {
  202. int len = shadow.getArgCount();
  203. //System.err.println("boudn to : " + len + ", " + patterns.length);
  204. if (patterns.length != len) {
  205. return Literal.FALSE;
  206. }
  207. Test ret = Literal.TRUE;
  208. for (int i=0; i < len; i++) {
  209. TypeX argType = shadow.getArgType(i);
  210. TypePattern type = patterns[i];
  211. if (!(type instanceof BindingTypePattern)) {
  212. ResolvedTypeX argRTX = shadow.getIWorld().resolve(argType,true);
  213. if (argRTX == ResolvedTypeX.MISSING) {
  214. IMessage msg = new Message(
  215. WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_ARG_TYPE,argType.getName()),
  216. "",IMessage.ERROR,shadow.getSourceLocation(),null,new ISourceLocation[]{getSourceLocation()});
  217. }
  218. if (type.matchesInstanceof(argRTX).alwaysTrue()) {
  219. continue;
  220. }
  221. } else {
  222. BindingTypePattern btp = (BindingTypePattern)type;
  223. // Check if we have already bound something to this formal
  224. if ((state.get(btp.getFormalIndex())!=null) &&(lastMatchedShadowId != shadow.shadowId)) {
  225. // ISourceLocation isl = getSourceLocation();
  226. // Message errorMessage = new Message(
  227. // "Ambiguous binding of type "+type.getExactType().toString()+
  228. // " using args(..) at this line - formal is already bound"+
  229. // ". See secondary source location for location of args(..)",
  230. // shadow.getSourceLocation(),true,new ISourceLocation[]{getSourceLocation()});
  231. // shadow.getIWorld().getMessageHandler().handleMessage(errorMessage);
  232. state.setErroneousVar(btp.getFormalIndex());
  233. }
  234. }
  235. ret = Test.makeAnd(ret,
  236. exposeStateForVar(shadow.getArgVar(i), type, state,shadow.getIWorld()));
  237. }
  238. return ret;
  239. }
  240. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  241. if (arguments.matches(shadow.getIWorld().resolve(shadow.getArgTypes()), TypePattern.DYNAMIC).alwaysFalse()) {
  242. return Literal.FALSE;
  243. }
  244. int ellipsisCount = arguments.ellipsisCount;
  245. if (ellipsisCount == 0) {
  246. return findResidueNoEllipsis(shadow, state, arguments.getTypePatterns());
  247. } else if (ellipsisCount == 1) {
  248. TypePattern[] patternsWithEllipsis = arguments.getTypePatterns();
  249. TypePattern[] patternsWithoutEllipsis = new TypePattern[shadow.getArgCount()];
  250. int lenWithEllipsis = patternsWithEllipsis.length;
  251. int lenWithoutEllipsis = patternsWithoutEllipsis.length;
  252. // l1+1 >= l0
  253. int indexWithEllipsis = 0;
  254. int indexWithoutEllipsis = 0;
  255. while (indexWithoutEllipsis < lenWithoutEllipsis) {
  256. TypePattern p = patternsWithEllipsis[indexWithEllipsis++];
  257. if (p == TypePattern.ELLIPSIS) {
  258. int newLenWithoutEllipsis =
  259. lenWithoutEllipsis - (lenWithEllipsis-indexWithEllipsis);
  260. while (indexWithoutEllipsis < newLenWithoutEllipsis) {
  261. patternsWithoutEllipsis[indexWithoutEllipsis++] = TypePattern.ANY;
  262. }
  263. } else {
  264. patternsWithoutEllipsis[indexWithoutEllipsis++] = p;
  265. }
  266. }
  267. return findResidueNoEllipsis(shadow, state, patternsWithoutEllipsis);
  268. } else {
  269. throw new BetaException("unimplemented");
  270. }
  271. }
  272. public String toString() {
  273. return "args" + arguments.toString() + "";
  274. }
  275. }