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.

DeclareAnnotation.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* *******************************************************************
  2. * Copyright (c) 2005 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. * Contributors:
  10. * Adrian Colyer initial implementation
  11. * Andy Clement got it working
  12. * ******************************************************************/
  13. package org.aspectj.weaver.patterns;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.aspectj.bridge.MessageUtil;
  20. import org.aspectj.weaver.AjAttribute.WeaverVersionInfo;
  21. import org.aspectj.weaver.AnnotationAJ;
  22. import org.aspectj.weaver.CompressingDataOutputStream;
  23. import org.aspectj.weaver.ISourceContext;
  24. import org.aspectj.weaver.ResolvedMember;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.UnresolvedType;
  27. import org.aspectj.weaver.VersionedDataInputStream;
  28. import org.aspectj.weaver.WeaverMessages;
  29. import org.aspectj.weaver.World;
  30. /**
  31. * Represents a declare annotation statement, one of atField, atMethod, atConstructor or atType.
  32. *
  33. * @author Andy Clement
  34. */
  35. public class DeclareAnnotation extends Declare {
  36. public static final Kind AT_TYPE = new Kind(1, "type");
  37. public static final Kind AT_FIELD = new Kind(2, "field");
  38. public static final Kind AT_METHOD = new Kind(3, "method");
  39. public static final Kind AT_CONSTRUCTOR = new Kind(4, "constructor");
  40. public static final Kind AT_REMOVE_FROM_FIELD = new Kind(5, "removeFromField");
  41. private static final String AJC_DECLARE_ANNOTATION = "Lorg/aspectj/internal/lang/annotation/ajcDeclareAnnotation;";
  42. private Kind kind;
  43. // for declare @type
  44. private TypePattern typePattern;
  45. // for declare @field,@method,@constructor
  46. private ISignaturePattern signaturePattern;
  47. private ResolvedType containingAspect;
  48. private List<String> annotationMethods;
  49. private List<String> annotationStrings;
  50. private AnnotationAJ annotation; // discovered when required
  51. private ResolvedType annotationType; // discovered when required
  52. // not serialized:
  53. private int annotationStart;
  54. private int annotationEnd;
  55. /**
  56. * Constructor for declare atType.
  57. */
  58. public DeclareAnnotation(Kind kind, TypePattern typePattern) {
  59. this.typePattern = typePattern;
  60. this.kind = kind;
  61. init();
  62. }
  63. /**
  64. * Constructor for declare atMethod/atField/atConstructor.
  65. */
  66. public DeclareAnnotation(Kind kind, ISignaturePattern sigPattern) {
  67. this.signaturePattern = sigPattern;
  68. this.kind = kind;
  69. init();
  70. }
  71. private void init() {
  72. this.annotationMethods = new ArrayList<>();
  73. annotationMethods.add("unknown");
  74. this.annotationStrings = new ArrayList<>();
  75. annotationStrings.add("@<annotation>");
  76. }
  77. /**
  78. * Returns the string, useful before the real annotation has been resolved
  79. */
  80. public String getAnnotationString() {
  81. return annotationStrings.get(0);
  82. }
  83. public boolean isExactPattern() {
  84. return typePattern instanceof ExactTypePattern;
  85. }
  86. public String getAnnotationMethod() {
  87. return annotationMethods.get(0);
  88. }
  89. @Override
  90. public String toString() {
  91. StringBuilder ret = new StringBuilder();
  92. ret.append("declare @");
  93. ret.append(kind);
  94. ret.append(" : ");
  95. ret.append(typePattern != null ? typePattern.toString() : signaturePattern.toString());
  96. ret.append(" : ");
  97. ret.append(annotationStrings.get(0));
  98. return ret.toString();
  99. }
  100. @Override
  101. public Object accept(PatternNodeVisitor visitor, Object data) {
  102. return visitor.visit(this, data);
  103. }
  104. public Object traverse(PatternNodeVisitor visitor, Object data) {
  105. Object ret = accept(visitor, data);
  106. if (this.signaturePattern != null && this.signaturePattern instanceof SignaturePattern)
  107. ((SignaturePattern) this.signaturePattern).traverse(visitor, ret);
  108. if (this.typePattern != null)
  109. this.typePattern.traverse(visitor, ret);
  110. return ret;
  111. }
  112. @Override
  113. public void resolve(IScope scope) {
  114. if (!scope.getWorld().isInJava5Mode()) {
  115. String msg = null;
  116. if (kind == AT_TYPE) {
  117. msg = WeaverMessages.DECLARE_ATTYPE_ONLY_SUPPORTED_AT_JAVA5_LEVEL;
  118. } else if (kind == AT_METHOD) {
  119. msg = WeaverMessages.DECLARE_ATMETHOD_ONLY_SUPPORTED_AT_JAVA5_LEVEL;
  120. } else if (kind == AT_FIELD) {
  121. msg = WeaverMessages.DECLARE_ATFIELD_ONLY_SUPPORTED_AT_JAVA5_LEVEL;
  122. } else if (kind == AT_CONSTRUCTOR) {
  123. msg = WeaverMessages.DECLARE_ATCONS_ONLY_SUPPORTED_AT_JAVA5_LEVEL;
  124. }
  125. scope.message(MessageUtil.error(WeaverMessages.format(msg), getSourceLocation()));
  126. return;
  127. }
  128. if (typePattern != null) {
  129. typePattern = typePattern.resolveBindings(scope, Bindings.NONE, false, false);
  130. }
  131. if (signaturePattern != null) {
  132. signaturePattern = signaturePattern.resolveBindings(scope, Bindings.NONE);
  133. }
  134. this.containingAspect = scope.getEnclosingType();
  135. }
  136. @Override
  137. public Declare parameterizeWith(Map<String, UnresolvedType> typeVariableBindingMap, World w) {
  138. DeclareAnnotation ret;
  139. if (this.kind == AT_TYPE) {
  140. ret = new DeclareAnnotation(kind, this.typePattern.parameterizeWith(typeVariableBindingMap, w));
  141. } else {
  142. ret = new DeclareAnnotation(kind, this.signaturePattern.parameterizeWith(typeVariableBindingMap, w));
  143. }
  144. ret.annotationMethods = this.annotationMethods;
  145. ret.annotationStrings = this.annotationStrings;
  146. ret.annotation = this.annotation;
  147. ret.containingAspect = this.containingAspect;
  148. ret.copyLocationFrom(this);
  149. return ret;
  150. }
  151. @Override
  152. public boolean isAdviceLike() {
  153. return false;
  154. }
  155. public void setAnnotationString(String annotationString) {
  156. this.annotationStrings.set(0, annotationString);
  157. }
  158. public void setAnnotationLocation(int start, int end) {
  159. this.annotationStart = start;
  160. this.annotationEnd = end;
  161. }
  162. public int getAnnotationSourceStart() {
  163. return annotationStart;
  164. }
  165. public int getAnnotationSourceEnd() {
  166. return annotationEnd;
  167. }
  168. public void setAnnotationMethod(String methodName) {
  169. this.annotationMethods.set(0, methodName);
  170. }
  171. @Override
  172. public boolean equals(Object obj) {
  173. if (!(obj instanceof DeclareAnnotation)) {
  174. return false;
  175. }
  176. DeclareAnnotation other = (DeclareAnnotation) obj;
  177. if (!this.kind.equals(other.kind)) {
  178. return false;
  179. }
  180. if (!this.annotationStrings.get(0).equals(other.annotationStrings.get(0))) {
  181. return false;
  182. }
  183. if (!this.annotationMethods.get(0).equals(other.annotationMethods.get(0))) {
  184. return false;
  185. }
  186. if (this.typePattern != null) {
  187. if (!typePattern.equals(other.typePattern)) {
  188. return false;
  189. }
  190. }
  191. if (this.signaturePattern != null) {
  192. if (!signaturePattern.equals(other.signaturePattern)) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. }
  198. @Override
  199. public int hashCode() {
  200. int result = 19;
  201. result = 37 * result + kind.hashCode();
  202. result = 37 * result + annotationStrings.get(0).hashCode();
  203. result = 37 * result + annotationMethods.get(0).hashCode();
  204. if (typePattern != null) {
  205. result = 37 * result + typePattern.hashCode();
  206. }
  207. if (signaturePattern != null) {
  208. result = 37 * result + signaturePattern.hashCode();
  209. }
  210. return result;
  211. }
  212. @Override
  213. public void write(CompressingDataOutputStream s) throws IOException {
  214. s.writeByte(Declare.ANNOTATION);
  215. if (kind.id == AT_FIELD.id && isRemover) {
  216. s.writeInt(AT_REMOVE_FROM_FIELD.id);
  217. } else {
  218. s.writeInt(kind.id);
  219. }
  220. int max = 0;
  221. s.writeByte(max = annotationStrings.size());
  222. for (int i = 0; i < max; i++) {
  223. s.writeUTF(annotationStrings.get(i));
  224. }
  225. s.writeByte(max = annotationMethods.size());
  226. for (int i = 0; i < max; i++) {
  227. s.writeUTF(annotationMethods.get(i));
  228. }
  229. if (typePattern != null) {
  230. typePattern.write(s);
  231. }
  232. if (signaturePattern != null) {
  233. AbstractSignaturePattern.writeCompoundSignaturePattern(s, signaturePattern);
  234. }
  235. writeLocation(s);
  236. }
  237. public static Declare read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  238. DeclareAnnotation ret = null;
  239. boolean isRemover = false;
  240. int kind = s.readInt();
  241. if (kind == AT_REMOVE_FROM_FIELD.id) {
  242. kind = AT_FIELD.id;
  243. isRemover = true;
  244. }
  245. // old format was just a single string and method
  246. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_AJ169) {
  247. // int numAnnotationStrings =
  248. s.readByte();
  249. }
  250. String annotationString = s.readUTF();
  251. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_AJ169) {
  252. // int numAnnotationMethods =
  253. s.readByte();
  254. }
  255. String annotationMethod = s.readUTF();
  256. TypePattern tp = null;
  257. SignaturePattern sp = null;
  258. switch (kind) {
  259. case 1:
  260. tp = TypePattern.read(s, context);
  261. ret = new DeclareAnnotation(AT_TYPE, tp);
  262. break;
  263. case 2:
  264. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_AJ169) {
  265. ret = new DeclareAnnotation(AT_FIELD, AbstractSignaturePattern.readCompoundSignaturePattern(s, context));
  266. } else {
  267. sp = SignaturePattern.read(s, context);
  268. ret = new DeclareAnnotation(AT_FIELD, sp);
  269. }
  270. if (isRemover) {
  271. ret.setRemover(true);
  272. }
  273. break;
  274. case 3:
  275. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_AJ169) {
  276. ret = new DeclareAnnotation(AT_METHOD, AbstractSignaturePattern.readCompoundSignaturePattern(s, context));
  277. } else {
  278. sp = SignaturePattern.read(s, context);
  279. ret = new DeclareAnnotation(AT_METHOD, sp);
  280. }
  281. break;
  282. case 4:
  283. if (s.getMajorVersion() >= WeaverVersionInfo.WEAVER_VERSION_AJ169) {
  284. ret = new DeclareAnnotation(AT_CONSTRUCTOR, AbstractSignaturePattern.readCompoundSignaturePattern(s, context));
  285. } else {
  286. sp = SignaturePattern.read(s, context);
  287. ret = new DeclareAnnotation(AT_CONSTRUCTOR, sp);
  288. }
  289. break;
  290. }
  291. ret.setAnnotationString(annotationString);
  292. ret.setAnnotationMethod(annotationMethod);
  293. ret.readLocation(context, s);
  294. return ret;
  295. }
  296. /**
  297. * For declare atConstructor, atMethod, atField
  298. */
  299. public boolean matches(ResolvedMember resolvedmember, World world) {
  300. if (kind == AT_METHOD || kind == AT_CONSTRUCTOR) {
  301. if (resolvedmember != null && resolvedmember.getName().charAt(0) == '<') {
  302. // <clinit> or <init>
  303. if (kind == AT_METHOD) {
  304. return false;
  305. }
  306. }
  307. }
  308. return signaturePattern.matches(resolvedmember, world, false);
  309. }
  310. /**
  311. * For declare atType.
  312. */
  313. public boolean matches(ResolvedType type) {
  314. if (!typePattern.matchesStatically(type)) {
  315. return false;
  316. }
  317. if (type.getWorld().getLint().typeNotExposedToWeaver.isEnabled() && !type.isExposedToWeaver()) {
  318. type.getWorld().getLint().typeNotExposedToWeaver.signal(type.getName(), getSourceLocation());
  319. }
  320. return true;
  321. }
  322. public void setAspect(ResolvedType typeX) {
  323. containingAspect = typeX;
  324. }
  325. public UnresolvedType getAspect() {
  326. return containingAspect;
  327. }
  328. public void copyAnnotationTo(ResolvedType onType) {
  329. ensureAnnotationDiscovered();
  330. if (annotation == null)
  331. return;
  332. if (!onType.hasAnnotation(annotation.getType())) {
  333. onType.addAnnotation(annotation);
  334. }
  335. }
  336. /**
  337. * @return declared annotation; can be null for annotations with SOURCE retention, which of course are not present in
  338. * the byte code
  339. */
  340. public AnnotationAJ getAnnotation() {
  341. ensureAnnotationDiscovered();
  342. return annotation;
  343. }
  344. /**
  345. * The annotation specified in the declare @type is stored against a simple method of the form "ajc$declare_<NN>",
  346. * this method finds that method and retrieves the annotation.
  347. * <p>
  348. * Caveat: Nothing will be found for annotations with SOURCE retention, which of course are not present in the byte
  349. * code.
  350. */
  351. private void ensureAnnotationDiscovered() {
  352. if (annotation != null)
  353. return;
  354. String annotationMethod = annotationMethods.get(0);
  355. for (Iterator<ResolvedMember> iter = containingAspect.getMethods(true, true); iter.hasNext();) {
  356. ResolvedMember member = iter.next();
  357. if (!member.getName().equals(annotationMethod))
  358. continue;
  359. AnnotationAJ[] annos = member.getAnnotations();
  360. // If weaving broken code, annos can be null or empty
  361. if (annos == null || annos.length == 0)
  362. return;
  363. int idx = 0;
  364. if (annos[0].getType().getSignature().equals(AJC_DECLARE_ANNOTATION)) {
  365. // @ajcDeclareAnnotation marker annotations should always come in pairs with the actual declared annotations.
  366. // If the second annotation is not found, it means that it has SOURCE retention and the annotation declaration
  367. // is to be ignored.
  368. if (annos.length < 2)
  369. break;
  370. idx = 1;
  371. }
  372. annotation = annos[idx];
  373. break;
  374. }
  375. }
  376. public TypePattern getTypePattern() {
  377. return typePattern;
  378. }
  379. public ISignaturePattern getSignaturePattern() {
  380. return signaturePattern;
  381. }
  382. public boolean isStarredAnnotationPattern() {
  383. if (typePattern != null) {
  384. return typePattern.isStarAnnotation();
  385. } else {
  386. return signaturePattern.isStarAnnotation();
  387. }
  388. }
  389. public Kind getKind() {
  390. return kind;
  391. }
  392. public boolean isDeclareAtConstuctor() {
  393. return kind.equals(AT_CONSTRUCTOR);
  394. }
  395. public boolean isDeclareAtMethod() {
  396. return kind.equals(AT_METHOD);
  397. }
  398. public boolean isDeclareAtType() {
  399. return kind.equals(AT_TYPE);
  400. }
  401. public boolean isDeclareAtField() {
  402. return kind.equals(AT_FIELD);
  403. }
  404. /**
  405. * @return the type of the annotation; can be null for annotations with SOURCE retention, which of course are not
  406. * present in the byte code
  407. */
  408. public ResolvedType getAnnotationType() {
  409. if (annotationType != null)
  410. return annotationType;
  411. String annotationMethod = annotationMethods.get(0);
  412. for (Iterator<ResolvedMember> iter = containingAspect.getMethods(true, true); iter.hasNext(); ) {
  413. ResolvedMember member = iter.next();
  414. if (!member.getName().equals(annotationMethod))
  415. continue;
  416. ResolvedType[] annoTypes = member.getAnnotationTypes();
  417. // If weaving broken code, annoTypes can be null or empty
  418. if (annoTypes == null || annoTypes.length == 0)
  419. return null;
  420. int idx = 0;
  421. if (annoTypes[0].getSignature().equals(AJC_DECLARE_ANNOTATION)) {
  422. // @ajcDeclareAnnotation marker annotations should always come in pairs with the actual declared annotations.
  423. // If the second annotation is not found, it means that it has SOURCE retention and the annotation declaration
  424. // is to be ignored.
  425. if (annoTypes.length < 2)
  426. break;
  427. idx = 1;
  428. }
  429. annotationType = annoTypes[idx];
  430. break;
  431. }
  432. return annotationType;
  433. }
  434. /**
  435. * @return true if the annotation specified is allowed on a field
  436. */
  437. public boolean isAnnotationAllowedOnField() {
  438. ensureAnnotationDiscovered();
  439. return annotation != null && annotation.allowedOnField();
  440. }
  441. public String getPatternAsString() {
  442. if (signaturePattern != null) {
  443. return signaturePattern.toString();
  444. }
  445. if (typePattern != null) {
  446. return typePattern.toString();
  447. }
  448. return "DONT KNOW";
  449. }
  450. /**
  451. * Return true if this declare annotation could ever match something in the specified type - only really able to make
  452. * intelligent decision if a type was specified in the sig/type pattern signature.
  453. */
  454. public boolean couldEverMatch(ResolvedType type) {
  455. // Haven't implemented variant for typePattern (doesn't seem worth it!)
  456. // BUGWARNING This test might not be sufficient for funny cases relating
  457. // to interfaces and the use of '+' - but it seems really important to
  458. // do something here so we don't iterate over all fields and all methods
  459. // in all types exposed to the weaver! So look out for bugs here and
  460. // we can update the test as appropriate.
  461. if (signaturePattern != null) {
  462. return signaturePattern.couldEverMatch(type);
  463. }
  464. return true;
  465. }
  466. /**
  467. * Provide a name suffix so that we can tell the different declare annotations forms apart in the AjProblemReporter
  468. */
  469. @Override
  470. public String getNameSuffix() {
  471. return getKind().toString();
  472. }
  473. /**
  474. * Captures type of declare annotation (method/type/field/constructor)
  475. */
  476. public static class Kind {
  477. private final int id;
  478. private String s;
  479. private Kind(int n, String name) {
  480. id = n;
  481. s = name;
  482. }
  483. @Override
  484. public int hashCode() {
  485. return (19 + 37 * id);
  486. }
  487. @Override
  488. public boolean equals(Object obj) {
  489. if (!(obj instanceof Kind)) {
  490. return false;
  491. }
  492. Kind other = (Kind) obj;
  493. return other.id == id;
  494. }
  495. @Override
  496. public String toString() {
  497. return "at_" + s;
  498. }
  499. }
  500. boolean isRemover = false;
  501. public void setRemover(boolean b) {
  502. isRemover = b;
  503. }
  504. public boolean isRemover() {
  505. return isRemover;
  506. }
  507. }