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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Map;
  18. import org.aspectj.bridge.IMessage;
  19. import org.aspectj.bridge.ISourceLocation;
  20. import org.aspectj.util.FuzzyBoolean;
  21. import org.aspectj.weaver.BCException;
  22. import org.aspectj.weaver.CompressingDataOutputStream;
  23. import org.aspectj.weaver.ISourceContext;
  24. import org.aspectj.weaver.IntMap;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.Shadow;
  27. import org.aspectj.weaver.UnresolvedType;
  28. import org.aspectj.weaver.VersionedDataInputStream;
  29. import org.aspectj.weaver.WeaverMessages;
  30. import org.aspectj.weaver.World;
  31. import org.aspectj.weaver.ast.Literal;
  32. import org.aspectj.weaver.ast.Test;
  33. /**
  34. * args(arguments)
  35. *
  36. * @author Erik Hilsdale
  37. * @author Jim Hugunin
  38. */
  39. public class ArgsPointcut extends NameBindingPointcut {
  40. private static final String ASPECTJ_JP_SIGNATURE_PREFIX = "Lorg/aspectj/lang/JoinPoint";
  41. private static final String ASPECTJ_SYNTHETIC_SIGNATURE_PREFIX = "Lorg/aspectj/runtime/internal/";
  42. private TypePatternList arguments;
  43. private String stringRepresentation;
  44. public ArgsPointcut(TypePatternList arguments) {
  45. this.arguments = arguments;
  46. this.pointcutKind = ARGS;
  47. this.stringRepresentation = "args" + arguments.toString() + "";
  48. }
  49. public TypePatternList getArguments() {
  50. return arguments;
  51. }
  52. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  53. ArgsPointcut ret = new ArgsPointcut(this.arguments.parameterizeWith(typeVariableMap, w));
  54. ret.copyLocationFrom(this);
  55. return ret;
  56. }
  57. public int couldMatchKinds() {
  58. return Shadow.ALL_SHADOW_KINDS_BITS; // empty args() matches jps with no args
  59. }
  60. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  61. return FuzzyBoolean.MAYBE;
  62. }
  63. protected FuzzyBoolean matchInternal(Shadow shadow) {
  64. ResolvedType[] argumentsToMatchAgainst = getArgumentsToMatchAgainst(shadow);
  65. FuzzyBoolean ret = arguments.matches(argumentsToMatchAgainst, TypePattern.DYNAMIC);
  66. return ret;
  67. }
  68. private ResolvedType[] getArgumentsToMatchAgainst(Shadow shadow) {
  69. if (shadow.isShadowForArrayConstructionJoinpoint()) {
  70. return shadow.getArgumentTypesForArrayConstructionShadow();
  71. }
  72. ResolvedType[] argumentsToMatchAgainst = shadow.getIWorld().resolve(shadow.getGenericArgTypes());
  73. // special treatment for adviceexecution which may have synthetic arguments we
  74. // want to ignore.
  75. if (shadow.getKind() == Shadow.AdviceExecution) {
  76. int numExtraArgs = 0;
  77. for (int i = 0; i < argumentsToMatchAgainst.length; i++) {
  78. String argumentSignature = argumentsToMatchAgainst[i].getSignature();
  79. if (argumentSignature.startsWith(ASPECTJ_JP_SIGNATURE_PREFIX)
  80. || argumentSignature.startsWith(ASPECTJ_SYNTHETIC_SIGNATURE_PREFIX)) {
  81. numExtraArgs++;
  82. } else {
  83. // normal arg after AJ type means earlier arg was NOT synthetic
  84. numExtraArgs = 0;
  85. }
  86. }
  87. if (numExtraArgs > 0) {
  88. int newArgLength = argumentsToMatchAgainst.length - numExtraArgs;
  89. ResolvedType[] argsSubset = new ResolvedType[newArgLength];
  90. System.arraycopy(argumentsToMatchAgainst, 0, argsSubset, 0, newArgLength);
  91. argumentsToMatchAgainst = argsSubset;
  92. }
  93. } else if (shadow.getKind() == Shadow.ConstructorExecution) {
  94. if (shadow.getMatchingSignature().getParameterTypes().length < argumentsToMatchAgainst.length) {
  95. // there are one or more synthetic args on the end, caused by non-public itd constructor
  96. int newArgLength = shadow.getMatchingSignature().getParameterTypes().length;
  97. ResolvedType[] argsSubset = new ResolvedType[newArgLength];
  98. System.arraycopy(argumentsToMatchAgainst, 0, argsSubset, 0, newArgLength);
  99. argumentsToMatchAgainst = argsSubset;
  100. }
  101. }
  102. return argumentsToMatchAgainst;
  103. }
  104. /*
  105. * (non-Javadoc)
  106. *
  107. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  108. */
  109. public List getBindingAnnotationTypePatterns() {
  110. return Collections.EMPTY_LIST;
  111. }
  112. /*
  113. * (non-Javadoc)
  114. *
  115. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  116. */
  117. public List getBindingTypePatterns() {
  118. List l = new ArrayList();
  119. TypePattern[] pats = arguments.getTypePatterns();
  120. for (int i = 0; i < pats.length; i++) {
  121. if (pats[i] instanceof BindingTypePattern) {
  122. l.add(pats[i]);
  123. }
  124. }
  125. return l;
  126. }
  127. public void write(CompressingDataOutputStream s) throws IOException {
  128. s.writeByte(Pointcut.ARGS);
  129. arguments.write(s);
  130. writeLocation(s);
  131. }
  132. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  133. ArgsPointcut ret = new ArgsPointcut(TypePatternList.read(s, context));
  134. ret.readLocation(context, s);
  135. return ret;
  136. }
  137. public boolean equals(Object other) {
  138. if (!(other instanceof ArgsPointcut)) {
  139. return false;
  140. }
  141. ArgsPointcut o = (ArgsPointcut) other;
  142. return o.arguments.equals(this.arguments);
  143. }
  144. public int hashCode() {
  145. return arguments.hashCode();
  146. }
  147. public void resolveBindings(IScope scope, Bindings bindings) {
  148. arguments.resolveBindings(scope, bindings, true, true);
  149. if (arguments.ellipsisCount > 1) {
  150. scope.message(IMessage.ERROR, this, "uses more than one .. in args (compiler limitation)");
  151. }
  152. }
  153. public void postRead(ResolvedType enclosingType) {
  154. arguments.postRead(enclosingType);
  155. }
  156. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  157. if (isDeclare(bindings.getEnclosingAdvice())) {
  158. // Enforce rule about which designators are supported in declare
  159. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.ARGS_IN_DECLARE),
  160. bindings.getEnclosingAdvice().getSourceLocation(), null);
  161. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  162. }
  163. TypePatternList args = arguments.resolveReferences(bindings);
  164. if (inAspect.crosscuttingMembers != null) {
  165. inAspect.crosscuttingMembers.exposeTypes(args.getExactTypes());
  166. }
  167. Pointcut ret = new ArgsPointcut(args);
  168. ret.copyLocationFrom(this);
  169. return ret;
  170. }
  171. private Test findResidueNoEllipsis(Shadow shadow, ExposedState state, TypePattern[] patterns) {
  172. ResolvedType[] argumentsToMatchAgainst = getArgumentsToMatchAgainst(shadow);
  173. int len = argumentsToMatchAgainst.length;
  174. // System.err.println("boudn to : " + len + ", " + patterns.length);
  175. if (patterns.length != len) {
  176. return Literal.FALSE;
  177. }
  178. Test ret = Literal.TRUE;
  179. for (int i = 0; i < len; i++) {
  180. UnresolvedType argType = shadow.getGenericArgTypes()[i];
  181. TypePattern type = patterns[i];
  182. ResolvedType argRTX = shadow.getIWorld().resolve(argType, true);
  183. if (!(type instanceof BindingTypePattern)) {
  184. if (argRTX.isMissing()) {
  185. shadow.getIWorld().getLint().cantFindType.signal(new String[] { WeaverMessages.format(
  186. WeaverMessages.CANT_FIND_TYPE_ARG_TYPE, argType.getName()) }, shadow.getSourceLocation(),
  187. new ISourceLocation[] { getSourceLocation() });
  188. // IMessage msg = new Message(
  189. // WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_ARG_TYPE,argType.getName()),
  190. // "",IMessage.ERROR,shadow.getSourceLocation(),null,new ISourceLocation[]{getSourceLocation()});
  191. // shadow.getIWorld().getMessageHandler().handleMessage(msg);
  192. }
  193. if (type.matchesInstanceof(argRTX).alwaysTrue()) {
  194. continue;
  195. }
  196. }
  197. World world = shadow.getIWorld();
  198. ResolvedType typeToExpose = type.getExactType().resolve(world);
  199. if (typeToExpose.isParameterizedType()) {
  200. boolean inDoubt = (type.matchesInstanceof(argRTX) == FuzzyBoolean.MAYBE);
  201. if (inDoubt && world.getLint().uncheckedArgument.isEnabled()) {
  202. String uncheckedMatchWith = typeToExpose.getSimpleBaseName();
  203. if (argRTX.isParameterizedType() && (argRTX.getRawType() == typeToExpose.getRawType())) {
  204. uncheckedMatchWith = argRTX.getSimpleName();
  205. }
  206. if (!isUncheckedArgumentWarningSuppressed()) {
  207. world.getLint().uncheckedArgument.signal(new String[] { typeToExpose.getSimpleName(), uncheckedMatchWith,
  208. typeToExpose.getSimpleBaseName(), shadow.toResolvedString(world) }, getSourceLocation(),
  209. new ISourceLocation[] { shadow.getSourceLocation() });
  210. }
  211. }
  212. }
  213. ret = Test.makeAnd(ret, exposeStateForVar(shadow.getArgVar(i), type, state, shadow.getIWorld()));
  214. }
  215. return ret;
  216. }
  217. /**
  218. * We need to find out if someone has put the @SuppressAjWarnings{"uncheckedArgument"} annotation somewhere. That somewhere is
  219. * going to be an a piece of advice that uses this pointcut. But how do we find it???
  220. *
  221. * @return
  222. */
  223. private boolean isUncheckedArgumentWarningSuppressed() {
  224. return false;
  225. }
  226. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  227. ResolvedType[] argsToMatch = getArgumentsToMatchAgainst(shadow);
  228. if (arguments.matches(argsToMatch, TypePattern.DYNAMIC).alwaysFalse()) {
  229. return Literal.FALSE;
  230. }
  231. int ellipsisCount = arguments.ellipsisCount;
  232. if (ellipsisCount == 0) {
  233. return findResidueNoEllipsis(shadow, state, arguments.getTypePatterns());
  234. } else if (ellipsisCount == 1) {
  235. TypePattern[] patternsWithEllipsis = arguments.getTypePatterns();
  236. TypePattern[] patternsWithoutEllipsis = new TypePattern[argsToMatch.length];
  237. int lenWithEllipsis = patternsWithEllipsis.length;
  238. int lenWithoutEllipsis = patternsWithoutEllipsis.length;
  239. // l1+1 >= l0
  240. int indexWithEllipsis = 0;
  241. int indexWithoutEllipsis = 0;
  242. while (indexWithoutEllipsis < lenWithoutEllipsis) {
  243. TypePattern p = patternsWithEllipsis[indexWithEllipsis++];
  244. if (p == TypePattern.ELLIPSIS) {
  245. int newLenWithoutEllipsis = lenWithoutEllipsis - (lenWithEllipsis - indexWithEllipsis);
  246. while (indexWithoutEllipsis < newLenWithoutEllipsis) {
  247. patternsWithoutEllipsis[indexWithoutEllipsis++] = TypePattern.ANY;
  248. }
  249. } else {
  250. patternsWithoutEllipsis[indexWithoutEllipsis++] = p;
  251. }
  252. }
  253. return findResidueNoEllipsis(shadow, state, patternsWithoutEllipsis);
  254. } else {
  255. throw new BCException("unimplemented");
  256. }
  257. }
  258. public String toString() {
  259. return this.stringRepresentation;
  260. }
  261. public Object accept(PatternNodeVisitor visitor, Object data) {
  262. return visitor.visit(this, data);
  263. }
  264. }