Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ArgsPointcut.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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<String,UnresolvedType> 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 (ResolvedType resolvedType : argumentsToMatchAgainst) {
  78. String argumentSignature = resolvedType.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. public List<BindingPattern> getBindingAnnotationTypePatterns() {
  105. return Collections.emptyList();
  106. }
  107. public List<BindingTypePattern> getBindingTypePatterns() {
  108. List<BindingTypePattern> l = new ArrayList<>();
  109. TypePattern[] pats = arguments.getTypePatterns();
  110. for (TypePattern pat : pats) {
  111. if (pat instanceof BindingTypePattern) {
  112. l.add((BindingTypePattern) pat);
  113. }
  114. }
  115. return l;
  116. }
  117. public void write(CompressingDataOutputStream s) throws IOException {
  118. s.writeByte(Pointcut.ARGS);
  119. arguments.write(s);
  120. writeLocation(s);
  121. }
  122. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  123. ArgsPointcut ret = new ArgsPointcut(TypePatternList.read(s, context));
  124. ret.readLocation(context, s);
  125. return ret;
  126. }
  127. public boolean equals(Object other) {
  128. if (!(other instanceof ArgsPointcut)) {
  129. return false;
  130. }
  131. ArgsPointcut o = (ArgsPointcut) other;
  132. return o.arguments.equals(this.arguments);
  133. }
  134. public int hashCode() {
  135. return arguments.hashCode();
  136. }
  137. public void resolveBindings(IScope scope, Bindings bindings) {
  138. arguments.resolveBindings(scope, bindings, true, true);
  139. if (arguments.ellipsisCount > 1) {
  140. scope.message(IMessage.ERROR, this, "uses more than one .. in args (compiler limitation)");
  141. }
  142. }
  143. public void postRead(ResolvedType enclosingType) {
  144. arguments.postRead(enclosingType);
  145. }
  146. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  147. if (isDeclare(bindings.getEnclosingAdvice())) {
  148. // Enforce rule about which designators are supported in declare
  149. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.ARGS_IN_DECLARE),
  150. bindings.getEnclosingAdvice().getSourceLocation(), null);
  151. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  152. }
  153. TypePatternList args = arguments.resolveReferences(bindings);
  154. if (inAspect.crosscuttingMembers != null) {
  155. inAspect.crosscuttingMembers.exposeTypes(args.getExactTypes());
  156. }
  157. Pointcut ret = new ArgsPointcut(args);
  158. ret.copyLocationFrom(this);
  159. return ret;
  160. }
  161. private Test findResidueNoEllipsis(Shadow shadow, ExposedState state, TypePattern[] patterns) {
  162. ResolvedType[] argumentsToMatchAgainst = getArgumentsToMatchAgainst(shadow);
  163. int len = argumentsToMatchAgainst.length;
  164. // System.err.println("boudn to : " + len + ", " + patterns.length);
  165. if (patterns.length != len) {
  166. return Literal.FALSE;
  167. }
  168. Test ret = Literal.TRUE;
  169. for (int i = 0; i < len; i++) {
  170. UnresolvedType argType = shadow.getGenericArgTypes()[i];
  171. TypePattern type = patterns[i];
  172. ResolvedType argRTX = shadow.getIWorld().resolve(argType, true);
  173. if (!(type instanceof BindingTypePattern)) {
  174. if (argRTX.isMissing()) {
  175. shadow.getIWorld().getLint().cantFindType.signal(new String[] { WeaverMessages.format(
  176. WeaverMessages.CANT_FIND_TYPE_ARG_TYPE, argType.getName()) }, shadow.getSourceLocation(),
  177. new ISourceLocation[] { getSourceLocation() });
  178. // IMessage msg = new Message(
  179. // WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_ARG_TYPE,argType.getName()),
  180. // "",IMessage.ERROR,shadow.getSourceLocation(),null,new ISourceLocation[]{getSourceLocation()});
  181. // shadow.getIWorld().getMessageHandler().handleMessage(msg);
  182. }
  183. if (type.matchesInstanceof(argRTX).alwaysTrue()) {
  184. continue;
  185. }
  186. }
  187. World world = shadow.getIWorld();
  188. ResolvedType typeToExpose = type.getExactType().resolve(world);
  189. if (typeToExpose.isParameterizedType()) {
  190. boolean inDoubt = (type.matchesInstanceof(argRTX) == FuzzyBoolean.MAYBE);
  191. if (inDoubt && world.getLint().uncheckedArgument.isEnabled()) {
  192. String uncheckedMatchWith = typeToExpose.getSimpleBaseName();
  193. if (argRTX.isParameterizedType() && (argRTX.getRawType() == typeToExpose.getRawType())) {
  194. uncheckedMatchWith = argRTX.getSimpleName();
  195. }
  196. if (!isUncheckedArgumentWarningSuppressed()) {
  197. world.getLint().uncheckedArgument.signal(new String[] { typeToExpose.getSimpleName(), uncheckedMatchWith,
  198. typeToExpose.getSimpleBaseName(), shadow.toResolvedString(world) }, getSourceLocation(),
  199. new ISourceLocation[] { shadow.getSourceLocation() });
  200. }
  201. }
  202. }
  203. ret = Test.makeAnd(ret, exposeStateForVar(shadow.getArgVar(i), type, state, shadow.getIWorld()));
  204. }
  205. return ret;
  206. }
  207. /**
  208. * We need to find out if someone has put the @SuppressAjWarnings{"uncheckedArgument"} annotation somewhere. That somewhere is
  209. * going to be an a piece of advice that uses this pointcut. But how do we find it???
  210. *
  211. * @return
  212. */
  213. private boolean isUncheckedArgumentWarningSuppressed() {
  214. return false;
  215. }
  216. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  217. ResolvedType[] argsToMatch = getArgumentsToMatchAgainst(shadow);
  218. if (arguments.matches(argsToMatch, TypePattern.DYNAMIC).alwaysFalse()) {
  219. return Literal.FALSE;
  220. }
  221. int ellipsisCount = arguments.ellipsisCount;
  222. if (ellipsisCount == 0) {
  223. return findResidueNoEllipsis(shadow, state, arguments.getTypePatterns());
  224. } else if (ellipsisCount == 1) {
  225. TypePattern[] patternsWithEllipsis = arguments.getTypePatterns();
  226. TypePattern[] patternsWithoutEllipsis = new TypePattern[argsToMatch.length];
  227. int lenWithEllipsis = patternsWithEllipsis.length;
  228. int lenWithoutEllipsis = patternsWithoutEllipsis.length;
  229. // l1+1 >= l0
  230. int indexWithEllipsis = 0;
  231. int indexWithoutEllipsis = 0;
  232. while (indexWithoutEllipsis < lenWithoutEllipsis) {
  233. TypePattern p = patternsWithEllipsis[indexWithEllipsis++];
  234. if (p == TypePattern.ELLIPSIS) {
  235. int newLenWithoutEllipsis = lenWithoutEllipsis - (lenWithEllipsis - indexWithEllipsis);
  236. while (indexWithoutEllipsis < newLenWithoutEllipsis) {
  237. patternsWithoutEllipsis[indexWithoutEllipsis++] = TypePattern.ANY;
  238. }
  239. } else {
  240. patternsWithoutEllipsis[indexWithoutEllipsis++] = p;
  241. }
  242. }
  243. return findResidueNoEllipsis(shadow, state, patternsWithoutEllipsis);
  244. } else {
  245. throw new BCException("unimplemented");
  246. }
  247. }
  248. public String toString() {
  249. return this.stringRepresentation;
  250. }
  251. public Object accept(PatternNodeVisitor visitor, Object data) {
  252. return visitor.visit(this, data);
  253. }
  254. }