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.

AnnotationPointcut.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 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. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.AjcMemberMaker;
  19. import org.aspectj.weaver.AnnotatedElement;
  20. import org.aspectj.weaver.BCException;
  21. import org.aspectj.weaver.CompressingDataOutputStream;
  22. import org.aspectj.weaver.ConcreteTypeMunger;
  23. import org.aspectj.weaver.ISourceContext;
  24. import org.aspectj.weaver.IntMap;
  25. import org.aspectj.weaver.Member;
  26. import org.aspectj.weaver.NameMangler;
  27. import org.aspectj.weaver.NewFieldTypeMunger;
  28. import org.aspectj.weaver.ResolvedMember;
  29. import org.aspectj.weaver.ResolvedType;
  30. import org.aspectj.weaver.Shadow;
  31. import org.aspectj.weaver.ShadowMunger;
  32. import org.aspectj.weaver.UnresolvedType;
  33. import org.aspectj.weaver.VersionedDataInputStream;
  34. import org.aspectj.weaver.WeaverMessages;
  35. import org.aspectj.weaver.World;
  36. import org.aspectj.weaver.ast.Literal;
  37. import org.aspectj.weaver.ast.Test;
  38. import org.aspectj.weaver.ast.Var;
  39. /**
  40. * (at)Annotation((at)Foo) or (at)Annotation(foo)<br>
  41. * <p>
  42. * Matches any join point where the subject of the join point has an annotation matching the annotationTypePattern:
  43. *
  44. * <br>
  45. * Join Point Kind - Subject <br>
  46. * ================================ <br>
  47. * method call - the target method <br>
  48. * method execution - the method <br>
  49. * constructor call - the constructor <br>
  50. * constructor execution - the constructor <br>
  51. * get - the target field <br>
  52. * set - the target field <br>
  53. * adviceexecution - the advice <br>
  54. * initialization - the constructor <br>
  55. * preinitialization - the constructor <br>
  56. * staticinitialization - the type being initialized <br>
  57. * handler - the declared type of the handled exception <br>
  58. */
  59. public class AnnotationPointcut extends NameBindingPointcut {
  60. private ExactAnnotationTypePattern annotationTypePattern;
  61. private String declarationText;
  62. public AnnotationPointcut(ExactAnnotationTypePattern type) {
  63. super();
  64. this.annotationTypePattern = type;
  65. this.pointcutKind = Pointcut.ANNOTATION;
  66. buildDeclarationText();
  67. }
  68. public AnnotationPointcut(ExactAnnotationTypePattern type, ShadowMunger munger) {
  69. this(type);
  70. buildDeclarationText();
  71. }
  72. public ExactAnnotationTypePattern getAnnotationTypePattern() {
  73. return annotationTypePattern;
  74. }
  75. @Override
  76. public int couldMatchKinds() {
  77. return Shadow.ALL_SHADOW_KINDS_BITS;
  78. }
  79. @Override
  80. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  81. AnnotationPointcut ret = new AnnotationPointcut((ExactAnnotationTypePattern) annotationTypePattern.parameterizeWith(
  82. typeVariableMap, w));
  83. ret.copyLocationFrom(this);
  84. return ret;
  85. }
  86. /*
  87. * (non-Javadoc)
  88. *
  89. * @see org.aspectj.weaver.patterns.Pointcut#fastMatch(org.aspectj.weaver.patterns.FastMatchInfo)
  90. */
  91. @Override
  92. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  93. if (info.getKind() == Shadow.StaticInitialization) {
  94. return annotationTypePattern.fastMatches(info.getType());
  95. } else {
  96. return FuzzyBoolean.MAYBE;
  97. }
  98. }
  99. /*
  100. * (non-Javadoc)
  101. *
  102. * @see org.aspectj.weaver.patterns.Pointcut#match(org.aspectj.weaver.Shadow)
  103. */
  104. @Override
  105. protected FuzzyBoolean matchInternal(Shadow shadow) {
  106. AnnotatedElement toMatchAgainst = null;
  107. Member member = shadow.getSignature();
  108. ResolvedMember rMember = member.resolve(shadow.getIWorld());
  109. if (rMember == null) {
  110. if (member.getName().startsWith(NameMangler.PREFIX)) {
  111. return FuzzyBoolean.NO;
  112. }
  113. shadow.getIWorld().getLint().unresolvableMember.signal(member.toString(), getSourceLocation());
  114. return FuzzyBoolean.NO;
  115. }
  116. Shadow.Kind kind = shadow.getKind();
  117. if (kind == Shadow.StaticInitialization) {
  118. toMatchAgainst = rMember.getDeclaringType().resolve(shadow.getIWorld());
  119. } else if ((kind == Shadow.ExceptionHandler)) {
  120. toMatchAgainst = rMember.getParameterTypes()[0].resolve(shadow.getIWorld());
  121. } else {
  122. toMatchAgainst = rMember;
  123. // FIXME asc I'd like to get rid of this bit of logic altogether, shame ITD fields don't have an effective sig attribute
  124. // FIXME asc perf cache the result of discovering the member that contains the real annotations
  125. if (rMember.isAnnotatedElsewhere()) {
  126. if (kind == Shadow.FieldGet || kind == Shadow.FieldSet) {
  127. // FIXME asc should include supers with getInterTypeMungersIncludingSupers ?
  128. List<ConcreteTypeMunger> mungers = rMember.getDeclaringType().resolve(shadow.getIWorld()).getInterTypeMungers();
  129. for (ConcreteTypeMunger typeMunger : mungers) {
  130. if (typeMunger.getMunger() instanceof NewFieldTypeMunger) {
  131. ResolvedMember fakerm = typeMunger.getSignature();
  132. if (fakerm.equals(member)) {
  133. ResolvedMember ajcMethod = AjcMemberMaker.interFieldInitializer(fakerm, typeMunger.getAspectType());
  134. ResolvedMember rmm = findMethod(typeMunger.getAspectType(), ajcMethod);
  135. toMatchAgainst = rmm;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. annotationTypePattern.resolve(shadow.getIWorld());
  143. return annotationTypePattern.matches(toMatchAgainst);
  144. }
  145. private ResolvedMember findMethod(ResolvedType aspectType, ResolvedMember ajcMethod) {
  146. ResolvedMember decMethods[] = aspectType.getDeclaredMethods();
  147. for (ResolvedMember member : decMethods) {
  148. if (member.equals(ajcMethod)) {
  149. return member;
  150. }
  151. }
  152. return null;
  153. }
  154. /*
  155. * (non-Javadoc)
  156. *
  157. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope,
  158. * org.aspectj.weaver.patterns.Bindings)
  159. */
  160. @Override
  161. protected void resolveBindings(IScope scope, Bindings bindings) {
  162. if (!scope.getWorld().isInJava5Mode()) {
  163. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.ATANNOTATION_ONLY_SUPPORTED_AT_JAVA5_LEVEL),
  164. getSourceLocation()));
  165. return;
  166. }
  167. annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope, bindings, true);
  168. // must be either a Var, or an annotation type pattern
  169. }
  170. /*
  171. * (non-Javadoc)
  172. *
  173. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedType, org.aspectj.weaver.IntMap)
  174. */
  175. @Override
  176. protected Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  177. ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
  178. Pointcut ret = new AnnotationPointcut(newType, bindings.getEnclosingAdvice());
  179. ret.copyLocationFrom(this);
  180. return ret;
  181. }
  182. @Override
  183. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  184. if (annotationTypePattern instanceof BindingAnnotationFieldTypePattern) {
  185. if (shadow.getKind() != Shadow.MethodExecution) {
  186. shadow.getIWorld()
  187. .getMessageHandler()
  188. .handleMessage(
  189. MessageUtil
  190. .error("Annotation field binding is only supported at method-execution join points (compiler limitation)",
  191. getSourceLocation()));
  192. return Literal.TRUE; // exit quickly, error will prevent weaving
  193. }
  194. BindingAnnotationFieldTypePattern btp = (BindingAnnotationFieldTypePattern) annotationTypePattern;
  195. ResolvedType formalType = btp.getFormalType().resolve(shadow.getIWorld());
  196. UnresolvedType annoType = btp.getAnnotationType();
  197. // TODO 2 need to sort out appropriate creation of the AnnotationAccessFieldVar - what happens for
  198. // reflective (ReflectionShadow) access to types?
  199. Var var = shadow.getKindedAnnotationVar(annoType);
  200. if (var == null) {
  201. throw new BCException("Unexpected problem locating annotation at join point '" + shadow + "'");
  202. }
  203. state.set(btp.getFormalIndex(), var.getAccessorForValue(formalType, btp.formalName));
  204. } else if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  205. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern) annotationTypePattern;
  206. UnresolvedType annotationType = btp.getAnnotationType();
  207. Var var = shadow.getKindedAnnotationVar(annotationType);
  208. // At this point, var *could* be null. The only reason this could happen (if we aren't failing...)
  209. // is if another binding annotation designator elsewhere in the pointcut is going to expose the annotation
  210. // eg. (execution(* a*(..)) && @annotation(foo)) || (execution(* b*(..)) && @this(foo))
  211. // where sometimes @annotation will be providing the value, and sometimes
  212. // @this will be providing the value (see pr138223)
  213. // If we are here for other indecipherable reasons (it's not the case above...) then
  214. // you might want to uncomment this next bit of code to collect the diagnostics
  215. // if (var == null) throw new BCException("Impossible! annotation=["+annotationType+
  216. // "] shadow=["+shadow+" at "+shadow.getSourceLocation()+
  217. // "] pointcut is at ["+getSourceLocation()+"]");
  218. if (var == null) {
  219. if (matchInternal(shadow).alwaysTrue()) {
  220. return Literal.TRUE;
  221. } else {
  222. return Literal.FALSE;
  223. }
  224. }
  225. state.set(btp.getFormalIndex(), var);
  226. }
  227. if (matchInternal(shadow).alwaysTrue()) {
  228. return Literal.TRUE;
  229. } else {
  230. return Literal.FALSE;
  231. }
  232. }
  233. /*
  234. * (non-Javadoc)
  235. *
  236. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  237. */
  238. @Override
  239. public List<BindingPattern> getBindingAnnotationTypePatterns() {
  240. if (annotationTypePattern instanceof BindingPattern) { // BindingAnnotationTypePattern) {
  241. List<BindingPattern> l = new ArrayList<>();
  242. l.add((BindingPattern)annotationTypePattern);
  243. return l;
  244. } else {
  245. return Collections.emptyList();
  246. }
  247. }
  248. /*
  249. * (non-Javadoc)
  250. *
  251. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  252. */
  253. @Override
  254. public List<BindingTypePattern> getBindingTypePatterns() {
  255. return Collections.emptyList();
  256. }
  257. /*
  258. * (non-Javadoc)
  259. *
  260. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  261. */
  262. @Override
  263. public void write(CompressingDataOutputStream s) throws IOException {
  264. s.writeByte(Pointcut.ANNOTATION);
  265. annotationTypePattern.write(s);
  266. writeLocation(s);
  267. }
  268. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  269. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  270. AnnotationPointcut ret = new AnnotationPointcut((ExactAnnotationTypePattern) type);
  271. ret.readLocation(context, s);
  272. return ret;
  273. }
  274. @Override
  275. public boolean equals(Object other) {
  276. if (!(other instanceof AnnotationPointcut)) {
  277. return false;
  278. }
  279. AnnotationPointcut o = (AnnotationPointcut) other;
  280. return o.annotationTypePattern.equals(this.annotationTypePattern);
  281. }
  282. @Override
  283. public int hashCode() {
  284. int result = 17;
  285. result = 37 * result + annotationTypePattern.hashCode();
  286. return result;
  287. }
  288. public void buildDeclarationText() {
  289. StringBuilder buf = new StringBuilder();
  290. buf.append("@annotation(");
  291. String annPatt = annotationTypePattern.toString();
  292. buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
  293. buf.append(")");
  294. this.declarationText = buf.toString();
  295. }
  296. @Override
  297. public String toString() {
  298. return this.declarationText;
  299. }
  300. @Override
  301. public Object accept(PatternNodeVisitor visitor, Object data) {
  302. return visitor.visit(this, data);
  303. }
  304. }