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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 (Object munger : mungers) {
  130. ConcreteTypeMunger typeMunger = (ConcreteTypeMunger) munger;
  131. if (typeMunger.getMunger() instanceof NewFieldTypeMunger) {
  132. ResolvedMember fakerm = typeMunger.getSignature();
  133. if (fakerm.equals(member)) {
  134. ResolvedMember ajcMethod = AjcMemberMaker.interFieldInitializer(fakerm, typeMunger.getAspectType());
  135. ResolvedMember rmm = findMethod(typeMunger.getAspectType(), ajcMethod);
  136. toMatchAgainst = rmm;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. annotationTypePattern.resolve(shadow.getIWorld());
  144. return annotationTypePattern.matches(toMatchAgainst);
  145. }
  146. private ResolvedMember findMethod(ResolvedType aspectType, ResolvedMember ajcMethod) {
  147. ResolvedMember decMethods[] = aspectType.getDeclaredMethods();
  148. for (ResolvedMember member : decMethods) {
  149. if (member.equals(ajcMethod)) {
  150. return member;
  151. }
  152. }
  153. return null;
  154. }
  155. /*
  156. * (non-Javadoc)
  157. *
  158. * @see org.aspectj.weaver.patterns.Pointcut#resolveBindings(org.aspectj.weaver.patterns.IScope,
  159. * org.aspectj.weaver.patterns.Bindings)
  160. */
  161. @Override
  162. protected void resolveBindings(IScope scope, Bindings bindings) {
  163. if (!scope.getWorld().isInJava5Mode()) {
  164. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.ATANNOTATION_ONLY_SUPPORTED_AT_JAVA5_LEVEL),
  165. getSourceLocation()));
  166. return;
  167. }
  168. annotationTypePattern = (ExactAnnotationTypePattern) annotationTypePattern.resolveBindings(scope, bindings, true);
  169. // must be either a Var, or an annotation type pattern
  170. }
  171. /*
  172. * (non-Javadoc)
  173. *
  174. * @see org.aspectj.weaver.patterns.Pointcut#concretize1(org.aspectj.weaver.ResolvedType, org.aspectj.weaver.IntMap)
  175. */
  176. @Override
  177. protected Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  178. ExactAnnotationTypePattern newType = (ExactAnnotationTypePattern) annotationTypePattern.remapAdviceFormals(bindings);
  179. Pointcut ret = new AnnotationPointcut(newType, bindings.getEnclosingAdvice());
  180. ret.copyLocationFrom(this);
  181. return ret;
  182. }
  183. @Override
  184. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  185. if (annotationTypePattern instanceof BindingAnnotationFieldTypePattern) {
  186. if (shadow.getKind() != Shadow.MethodExecution) {
  187. shadow.getIWorld()
  188. .getMessageHandler()
  189. .handleMessage(
  190. MessageUtil
  191. .error("Annotation field binding is only supported at method-execution join points (compiler limitation)",
  192. getSourceLocation()));
  193. return Literal.TRUE; // exit quickly, error will prevent weaving
  194. }
  195. BindingAnnotationFieldTypePattern btp = (BindingAnnotationFieldTypePattern) annotationTypePattern;
  196. ResolvedType formalType = btp.getFormalType().resolve(shadow.getIWorld());
  197. UnresolvedType annoType = btp.getAnnotationType();
  198. // TODO 2 need to sort out appropriate creation of the AnnotationAccessFieldVar - what happens for
  199. // reflective (ReflectionShadow) access to types?
  200. Var var = shadow.getKindedAnnotationVar(annoType);
  201. if (var == null) {
  202. throw new BCException("Unexpected problem locating annotation at join point '" + shadow + "'");
  203. }
  204. state.set(btp.getFormalIndex(), var.getAccessorForValue(formalType, btp.formalName));
  205. } else if (annotationTypePattern instanceof BindingAnnotationTypePattern) {
  206. BindingAnnotationTypePattern btp = (BindingAnnotationTypePattern) annotationTypePattern;
  207. UnresolvedType annotationType = btp.getAnnotationType();
  208. Var var = shadow.getKindedAnnotationVar(annotationType);
  209. // At this point, var *could* be null. The only reason this could happen (if we aren't failing...)
  210. // is if another binding annotation designator elsewhere in the pointcut is going to expose the annotation
  211. // eg. (execution(* a*(..)) && @annotation(foo)) || (execution(* b*(..)) && @this(foo))
  212. // where sometimes @annotation will be providing the value, and sometimes
  213. // @this will be providing the value (see pr138223)
  214. // If we are here for other indecipherable reasons (it's not the case above...) then
  215. // you might want to uncomment this next bit of code to collect the diagnostics
  216. // if (var == null) throw new BCException("Impossible! annotation=["+annotationType+
  217. // "] shadow=["+shadow+" at "+shadow.getSourceLocation()+
  218. // "] pointcut is at ["+getSourceLocation()+"]");
  219. if (var == null) {
  220. if (matchInternal(shadow).alwaysTrue()) {
  221. return Literal.TRUE;
  222. } else {
  223. return Literal.FALSE;
  224. }
  225. }
  226. state.set(btp.getFormalIndex(), var);
  227. }
  228. if (matchInternal(shadow).alwaysTrue()) {
  229. return Literal.TRUE;
  230. } else {
  231. return Literal.FALSE;
  232. }
  233. }
  234. /*
  235. * (non-Javadoc)
  236. *
  237. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  238. */
  239. @Override
  240. public List<BindingPattern> getBindingAnnotationTypePatterns() {
  241. if (annotationTypePattern instanceof BindingPattern) { // BindingAnnotationTypePattern) {
  242. List<BindingPattern> l = new ArrayList<>();
  243. l.add((BindingPattern)annotationTypePattern);
  244. return l;
  245. } else {
  246. return Collections.emptyList();
  247. }
  248. }
  249. /*
  250. * (non-Javadoc)
  251. *
  252. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  253. */
  254. @Override
  255. public List<BindingTypePattern> getBindingTypePatterns() {
  256. return Collections.emptyList();
  257. }
  258. /*
  259. * (non-Javadoc)
  260. *
  261. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  262. */
  263. @Override
  264. public void write(CompressingDataOutputStream s) throws IOException {
  265. s.writeByte(Pointcut.ANNOTATION);
  266. annotationTypePattern.write(s);
  267. writeLocation(s);
  268. }
  269. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  270. AnnotationTypePattern type = AnnotationTypePattern.read(s, context);
  271. AnnotationPointcut ret = new AnnotationPointcut((ExactAnnotationTypePattern) type);
  272. ret.readLocation(context, s);
  273. return ret;
  274. }
  275. @Override
  276. public boolean equals(Object other) {
  277. if (!(other instanceof AnnotationPointcut)) {
  278. return false;
  279. }
  280. AnnotationPointcut o = (AnnotationPointcut) other;
  281. return o.annotationTypePattern.equals(this.annotationTypePattern);
  282. }
  283. @Override
  284. public int hashCode() {
  285. int result = 17;
  286. result = 37 * result + annotationTypePattern.hashCode();
  287. return result;
  288. }
  289. public void buildDeclarationText() {
  290. StringBuffer buf = new StringBuffer();
  291. buf.append("@annotation(");
  292. String annPatt = annotationTypePattern.toString();
  293. buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
  294. buf.append(")");
  295. this.declarationText = buf.toString();
  296. }
  297. @Override
  298. public String toString() {
  299. return this.declarationText;
  300. }
  301. @Override
  302. public Object accept(PatternNodeVisitor visitor, Object data) {
  303. return visitor.visit(this, data);
  304. }
  305. }