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

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