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.

ExactAnnotationTypePattern.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.HashMap;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  19. import org.aspectj.weaver.AnnotatedElement;
  20. import org.aspectj.weaver.AnnotationAJ;
  21. import org.aspectj.weaver.BCException;
  22. import org.aspectj.weaver.CompressingDataOutputStream;
  23. import org.aspectj.weaver.ISourceContext;
  24. import org.aspectj.weaver.ReferenceType;
  25. import org.aspectj.weaver.ResolvedMember;
  26. import org.aspectj.weaver.ResolvedType;
  27. import org.aspectj.weaver.TypeVariableReference;
  28. import org.aspectj.weaver.UnresolvedType;
  29. import org.aspectj.weaver.VersionedDataInputStream;
  30. import org.aspectj.weaver.WeaverMessages;
  31. import org.aspectj.weaver.World;
  32. /**
  33. * Matches an annotation of a given type
  34. */
  35. public class ExactAnnotationTypePattern extends AnnotationTypePattern {
  36. protected UnresolvedType annotationType;
  37. protected String formalName;
  38. protected boolean resolved = false;
  39. protected boolean bindingPattern = false;
  40. private Map<String, String> annotationValues;
  41. // OPTIMIZE is annotationtype really unresolved???? surely it is resolved by
  42. // now...
  43. public ExactAnnotationTypePattern(UnresolvedType annotationType, Map<String, String> annotationValues) {
  44. this.annotationType = annotationType;
  45. this.annotationValues = annotationValues;
  46. this.resolved = (annotationType instanceof ResolvedType);
  47. }
  48. // Used when deserializing, values will be added
  49. private ExactAnnotationTypePattern(UnresolvedType annotationType) {
  50. this.annotationType = annotationType;
  51. this.resolved = (annotationType instanceof ResolvedType);
  52. }
  53. protected ExactAnnotationTypePattern(String formalName) {
  54. this.formalName = formalName;
  55. this.resolved = false;
  56. this.bindingPattern = true;
  57. // will be turned into BindingAnnotationTypePattern during resolution
  58. }
  59. public ResolvedType getResolvedAnnotationType() {
  60. if (!resolved) {
  61. throw new IllegalStateException("I need to be resolved first!");
  62. }
  63. return (ResolvedType) annotationType;
  64. }
  65. public UnresolvedType getAnnotationType() {
  66. return annotationType;
  67. }
  68. public Map<String, String> getAnnotationValues() {
  69. return annotationValues;
  70. }
  71. @Override
  72. public FuzzyBoolean fastMatches(AnnotatedElement annotated) {
  73. if (annotated.hasAnnotation(annotationType) && annotationValues == null) {
  74. return FuzzyBoolean.YES;
  75. } else {
  76. // could be inherited, but we don't know that until we are
  77. // resolved, and we're not yet...
  78. return FuzzyBoolean.MAYBE;
  79. }
  80. }
  81. @Override
  82. public FuzzyBoolean matches(AnnotatedElement annotated) {
  83. return matches(annotated, null);
  84. }
  85. @Override
  86. public FuzzyBoolean matches(AnnotatedElement annotated, ResolvedType[] parameterAnnotations) {
  87. if (!isForParameterAnnotationMatch()) {
  88. boolean checkSupers = false;
  89. if (getResolvedAnnotationType().isInheritedAnnotation()) {
  90. if (annotated instanceof ResolvedType) {
  91. checkSupers = true;
  92. }
  93. }
  94. if (annotated.hasAnnotation(annotationType)) {
  95. if (annotationType instanceof ReferenceType) {
  96. ReferenceType rt = (ReferenceType) annotationType;
  97. if (rt.getRetentionPolicy() != null && rt.getRetentionPolicy().equals("SOURCE")) {
  98. rt.getWorld()
  99. .getMessageHandler()
  100. .handleMessage(
  101. MessageUtil.warn(WeaverMessages.format(WeaverMessages.NO_MATCH_BECAUSE_SOURCE_RETENTION,
  102. annotationType, annotated), getSourceLocation()));
  103. return FuzzyBoolean.NO;
  104. }
  105. }
  106. // Are we also matching annotation values?
  107. if (annotationValues != null) {
  108. AnnotationAJ theAnnotation = annotated.getAnnotationOfType(annotationType);
  109. // Check each one
  110. Set<String> keys = annotationValues.keySet();
  111. for (String k : keys) {
  112. boolean notEqual = false;
  113. String v = annotationValues.get(k);
  114. // if the key has a trailing '!' then it means the source expressed k!=v - so we are looking for
  115. // something other than the value specified
  116. if (k.endsWith("!")) {
  117. notEqual = true;
  118. k = k.substring(0, k.length() - 1);
  119. }
  120. if (theAnnotation.hasNamedValue(k)) {
  121. // Simple case, value is 'name=value' and the
  122. // annotation specified the same thing
  123. if (notEqual) {
  124. if (theAnnotation.hasNameValuePair(k, v)) {
  125. return FuzzyBoolean.NO;
  126. }
  127. } else {
  128. if (!theAnnotation.hasNameValuePair(k, v)) {
  129. return FuzzyBoolean.NO;
  130. }
  131. }
  132. } else {
  133. // Complex case, look at the default value
  134. ResolvedMember[] ms = ((ResolvedType) annotationType).getDeclaredMethods();
  135. boolean foundMatch = false;
  136. for (int i = 0; i < ms.length && !foundMatch; i++) {
  137. if (ms[i].isAbstract() && ms[i].getParameterTypes().length == 0 && ms[i].getName().equals(k)) {
  138. // we might be onto something
  139. String s = ms[i].getAnnotationDefaultValue();
  140. if (s != null && s.equals(v)) {
  141. foundMatch = true;
  142. }
  143. }
  144. }
  145. if (notEqual) {
  146. if (foundMatch) {
  147. return FuzzyBoolean.NO;
  148. }
  149. } else {
  150. if (!foundMatch) {
  151. return FuzzyBoolean.NO;
  152. }
  153. }
  154. }
  155. }
  156. }
  157. return FuzzyBoolean.YES;
  158. } else if (checkSupers) {
  159. ResolvedType toMatchAgainst = ((ResolvedType) annotated).getSuperclass();
  160. while (toMatchAgainst != null) {
  161. if (toMatchAgainst.hasAnnotation(annotationType)) {
  162. // Are we also matching annotation values?
  163. if (annotationValues != null) {
  164. AnnotationAJ theAnnotation = toMatchAgainst.getAnnotationOfType(annotationType);
  165. // Check each one
  166. Set<String> keys = annotationValues.keySet();
  167. for (String k : keys) {
  168. String v = annotationValues.get(k);
  169. if (theAnnotation.hasNamedValue(k)) {
  170. // Simple case, value is 'name=value' and
  171. // the annotation specified the same thing
  172. if (!theAnnotation.hasNameValuePair(k, v)) {
  173. return FuzzyBoolean.NO;
  174. }
  175. } else {
  176. // Complex case, look at the default value
  177. ResolvedMember[] ms = ((ResolvedType) annotationType).getDeclaredMethods();
  178. boolean foundMatch = false;
  179. for (int i = 0; i < ms.length && !foundMatch; i++) {
  180. if (ms[i].isAbstract() && ms[i].getParameterTypes().length == 0
  181. && ms[i].getName().equals(k)) {
  182. // we might be onto something
  183. String s = ms[i].getAnnotationDefaultValue();
  184. if (s != null && s.equals(v)) {
  185. foundMatch = true;
  186. }
  187. }
  188. }
  189. if (!foundMatch) {
  190. return FuzzyBoolean.NO;
  191. }
  192. }
  193. }
  194. }
  195. return FuzzyBoolean.YES;
  196. }
  197. toMatchAgainst = toMatchAgainst.getSuperclass();
  198. }
  199. }
  200. } else {
  201. // check parameter annotations
  202. if (parameterAnnotations == null) {
  203. return FuzzyBoolean.NO;
  204. }
  205. for (ResolvedType parameterAnnotation : parameterAnnotations) {
  206. if (annotationType.equals(parameterAnnotation)) {
  207. // Are we also matching annotation values?
  208. if (annotationValues != null) {
  209. parameterAnnotation
  210. .getWorld()
  211. .getMessageHandler()
  212. .handleMessage(
  213. MessageUtil
  214. .error("Compiler limitation: annotation value matching for parameter annotations not yet supported"));
  215. return FuzzyBoolean.NO;
  216. }
  217. return FuzzyBoolean.YES;
  218. }
  219. }
  220. }
  221. return FuzzyBoolean.NO;
  222. }
  223. // this version should be called for @this, @target, @args
  224. public FuzzyBoolean matchesRuntimeType(AnnotatedElement annotated) {
  225. if (getResolvedAnnotationType().isInheritedAnnotation()) {
  226. // a static match is good enough
  227. if (matches(annotated).alwaysTrue()) {
  228. return FuzzyBoolean.YES;
  229. }
  230. }
  231. // a subtype could match at runtime
  232. return FuzzyBoolean.MAYBE;
  233. }
  234. @Override
  235. public void resolve(World world) {
  236. if (!resolved) {
  237. annotationType = annotationType.resolve(world);
  238. resolved = true;
  239. }
  240. }
  241. /*
  242. * (non-Javadoc)
  243. *
  244. * @see org.aspectj.weaver.patterns.AnnotationTypePattern#resolveBindings(org .aspectj.weaver.patterns.IScope,
  245. * org.aspectj.weaver.patterns.Bindings, boolean)
  246. */
  247. @Override
  248. public AnnotationTypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding) {
  249. if (resolved) {
  250. return this;
  251. }
  252. resolved = true;
  253. String simpleName = maybeGetSimpleName();
  254. if (simpleName != null) {
  255. FormalBinding formalBinding = scope.lookupFormal(simpleName);
  256. if (formalBinding != null) {
  257. if (bindings == null) {
  258. scope.message(IMessage.ERROR, this, "negation doesn't allow binding");
  259. return this;
  260. }
  261. if (!allowBinding) {
  262. scope.message(IMessage.ERROR, this, "name binding only allowed in @pcds, args, this, and target");
  263. return this;
  264. }
  265. formalName = simpleName;
  266. bindingPattern = true;
  267. verifyIsAnnotationType(formalBinding.getType().resolve(scope.getWorld()), scope);
  268. BindingAnnotationTypePattern binding = new BindingAnnotationTypePattern(formalBinding);
  269. binding.copyLocationFrom(this);
  270. bindings.register(binding, scope);
  271. binding.resolveBinding(scope.getWorld());
  272. if (isForParameterAnnotationMatch()) {
  273. binding.setForParameterAnnotationMatch();
  274. }
  275. return binding;
  276. }
  277. }
  278. // Non binding case
  279. String cleanname = annotationType.getName();
  280. annotationType = scope.getWorld().resolve(annotationType, true);
  281. // We may not have found it if it is in a package, lets look it up...
  282. if (ResolvedType.isMissing(annotationType)) {
  283. UnresolvedType type = null;
  284. while (ResolvedType.isMissing(type = scope.lookupType(cleanname, this))) {
  285. int lastDot = cleanname.lastIndexOf('.');
  286. if (lastDot == -1) {
  287. break;
  288. }
  289. cleanname = cleanname.substring(0, lastDot) + "$" + cleanname.substring(lastDot + 1);
  290. }
  291. annotationType = scope.getWorld().resolve(type, true);
  292. }
  293. verifyIsAnnotationType((ResolvedType) annotationType, scope);
  294. return this;
  295. }
  296. @Override
  297. public AnnotationTypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  298. UnresolvedType newAnnotationType = annotationType;
  299. if (annotationType.isTypeVariableReference()) {
  300. TypeVariableReference t = (TypeVariableReference) annotationType;
  301. String key = t.getTypeVariable().getName();
  302. if (typeVariableMap.containsKey(key)) {
  303. newAnnotationType = typeVariableMap.get(key);
  304. }
  305. } else if (annotationType.isParameterizedType()) {
  306. newAnnotationType = annotationType.parameterize(typeVariableMap);
  307. }
  308. ExactAnnotationTypePattern ret = new ExactAnnotationTypePattern(newAnnotationType, annotationValues);
  309. ret.formalName = formalName;
  310. ret.bindingPattern = bindingPattern;
  311. ret.copyLocationFrom(this);
  312. if (isForParameterAnnotationMatch()) {
  313. ret.setForParameterAnnotationMatch();
  314. }
  315. return ret;
  316. }
  317. protected String maybeGetSimpleName() {
  318. if (formalName != null) {
  319. return formalName;
  320. }
  321. String ret = annotationType.getName();
  322. return (ret.indexOf('.') == -1) ? ret : null;
  323. }
  324. protected void verifyIsAnnotationType(ResolvedType type, IScope scope) {
  325. if (!type.isAnnotation()) {
  326. IMessage m = MessageUtil.error(WeaverMessages.format(WeaverMessages.REFERENCE_TO_NON_ANNOTATION_TYPE, type.getName()),
  327. getSourceLocation());
  328. scope.getWorld().getMessageHandler().handleMessage(m);
  329. resolved = false;
  330. }
  331. }
  332. private static byte VERSION = 1; // rev if serialisation form changes
  333. /*
  334. * (non-Javadoc)
  335. *
  336. * @see org.aspectj.weaver.patterns.PatternNode#write(java.io.DataOutputStream)
  337. */
  338. @Override
  339. public void write(CompressingDataOutputStream s) throws IOException {
  340. s.writeByte(AnnotationTypePattern.EXACT);
  341. s.writeByte(VERSION);
  342. s.writeBoolean(bindingPattern);
  343. if (bindingPattern) {
  344. s.writeUTF(formalName);
  345. } else {
  346. annotationType.write(s);
  347. }
  348. writeLocation(s);
  349. s.writeBoolean(isForParameterAnnotationMatch());
  350. if (annotationValues == null) {
  351. s.writeInt(0);
  352. } else {
  353. s.writeInt(annotationValues.size());
  354. Set<String> key = annotationValues.keySet();
  355. for (String k : key) {
  356. s.writeUTF(k);
  357. s.writeUTF(annotationValues.get(k));
  358. }
  359. }
  360. }
  361. public static AnnotationTypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  362. ExactAnnotationTypePattern ret;
  363. byte version = s.readByte();
  364. if (version > VERSION) {
  365. throw new BCException("ExactAnnotationTypePattern was written by a newer version of AspectJ");
  366. }
  367. boolean isBindingPattern = s.readBoolean();
  368. if (isBindingPattern) {
  369. ret = new ExactAnnotationTypePattern(s.readUTF());
  370. } else {
  371. ret = new ExactAnnotationTypePattern(UnresolvedType.read(s));
  372. }
  373. ret.readLocation(context, s);
  374. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ160) {
  375. if (s.readBoolean()) {
  376. ret.setForParameterAnnotationMatch();
  377. }
  378. }
  379. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ160M2) {
  380. int annotationValueCount = s.readInt();
  381. if (annotationValueCount > 0) {
  382. Map<String, String> aValues = new HashMap<>();
  383. for (int i = 0; i < annotationValueCount; i++) {
  384. String key = s.readUTF();
  385. String val = s.readUTF();
  386. aValues.put(key, val);
  387. }
  388. ret.annotationValues = aValues;
  389. }
  390. }
  391. return ret;
  392. }
  393. /*
  394. * (non-Javadoc)
  395. *
  396. * @see java.lang.Object#equals(java.lang.Object)
  397. */
  398. @Override
  399. public boolean equals(Object obj) {
  400. if (!(obj instanceof ExactAnnotationTypePattern)) {
  401. return false;
  402. }
  403. ExactAnnotationTypePattern other = (ExactAnnotationTypePattern) obj;
  404. return (other.annotationType.equals(annotationType))
  405. && isForParameterAnnotationMatch() == other.isForParameterAnnotationMatch()
  406. && (annotationValues == null ? other.annotationValues == null : annotationValues.equals(other.annotationValues));
  407. }
  408. /*
  409. * (non-Javadoc)
  410. *
  411. * @see java.lang.Object#hashCode()
  412. */
  413. @Override
  414. public int hashCode() {
  415. return (((annotationType.hashCode()) * 37 + (isForParameterAnnotationMatch() ? 0 : 1)) * 37)
  416. + (annotationValues == null ? 0 : annotationValues.hashCode());
  417. }
  418. @Override
  419. public String toString() {
  420. if (!resolved && formalName != null) {
  421. return formalName;
  422. }
  423. String ret = "@" + annotationType.toString();
  424. if (formalName != null) {
  425. ret = ret + " " + formalName;
  426. }
  427. return ret;
  428. }
  429. @Override
  430. public Object accept(PatternNodeVisitor visitor, Object data) {
  431. return visitor.visit(this, data);
  432. }
  433. }