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.

AnnotationsAttribute.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. import java.util.Map;
  17. import java.io.IOException;
  18. import java.io.DataInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import javassist.bytecode.annotation.*;
  21. /**
  22. * A class representing
  23. * <code>RuntimeVisibleAnnotations_attribute</code> and
  24. * <code>RuntimeInvisibleAnnotations_attribute</code>.
  25. *
  26. * <p>To obtain an AnnotationAttribute object, invoke
  27. * <code>getAttribute(AnnotationsAttribute.visibleTag)</code>
  28. * in <code>ClassFile</code>, <code>MethodInfo</code>,
  29. * or <code>FieldInfo</code>. The obtained attribute is a
  30. * runtime visible annotations attribute.
  31. * If the parameter is
  32. * <code>AnnotationAttribute.invisibleTag</code>, then the obtained
  33. * attribute is a runtime invisible one.
  34. *
  35. * <p>For example,
  36. *
  37. * <ul><pre>
  38. * import javassist.bytecode.annotation.Annotation;
  39. * :
  40. * CtMethod m = ... ;
  41. * MethodInfo minfo = m.getMethodInfo();
  42. * AnnotationsAttribute attr = (AnnotationsAttribute)
  43. * minfo.getAttribute(AnnotationsAttribute.invisibleTag);
  44. * Annotation an = attr.getAnnotation("Author");
  45. * String s = ((StringMemberValue)an.getMemberValue("name")).getValue();
  46. * System.out.println("@Author(name=" + s + ")");
  47. * </pre></ul>
  48. *
  49. * <p>This code snippet retrieves an annotation of the type <code>Author</code>
  50. * from the <code>MethodInfo</code> object specified by <code>minfo</code>.
  51. * Then, it prints the value of <code>name</code> in <code>Author</code>.
  52. *
  53. * <p>If the annotation type <code>Author</code> is annotated by a meta annotation:
  54. *
  55. * <ul><pre>
  56. * &#64;Retention(RetentionPolicy.RUNTIME)
  57. * </pre></ul>
  58. *
  59. * <p>Then <code>Author</code> is visible at runtime. Therefore, the third
  60. * statement of the code snippet above must be changed into:
  61. *
  62. * <ul><pre>
  63. * AnnotationsAttribute attr = (AnnotationsAttribute)
  64. * minfo.getAttribute(AnnotationsAttribute.visibleTag);
  65. * </pre></ul>
  66. *
  67. * <p>The attribute tag must be <code>visibleTag</code> instead of
  68. * <code>invisibleTag</code>.
  69. *
  70. * <p>If the member value of an annotation is not specified, the default value
  71. * is used as that member value. If so, <code>getMemberValue()</code> in
  72. * <code>Annotation</code> returns <code>null</code>
  73. * since the default value is not included in the
  74. * <code>AnnotationsAttribute</code>. It is included in the
  75. * <code>AnnotationDefaultAttribute</code> of the method declared in the
  76. * annotation type.
  77. *
  78. * <p>If you want to record a new AnnotationAttribute object, execute the
  79. * following snippet:
  80. *
  81. * <ul><pre>
  82. * ClassFile cf = ... ;
  83. * ConstPool cp = cf.getConstPool();
  84. * AnnotationsAttribute attr
  85. * = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
  86. * Annotation a = new Annotation("Author", cp);
  87. * a.addMemberValue("name", new StringMemberValue("Chiba", cp));
  88. * attr.setAnnotation(a);
  89. * cf.addAttribute(attr);
  90. * cf.setVersionToJava5();
  91. * </pre></ul>
  92. *
  93. * <p>The last statement is necessary if the class file was produced by
  94. * Javassist or JDK 1.4. Otherwise, it is not necessary.
  95. *
  96. * @see AnnotationDefaultAttribute
  97. * @see javassist.bytecode.annotation.Annotation
  98. */
  99. public class AnnotationsAttribute extends AttributeInfo {
  100. /**
  101. * The name of the <code>RuntimeVisibleAnnotations</code> attribute.
  102. */
  103. public static final String visibleTag = "RuntimeVisibleAnnotations";
  104. /**
  105. * The name of the <code>RuntimeInvisibleAnnotations</code> attribute.
  106. */
  107. public static final String invisibleTag = "RuntimeInvisibleAnnotations";
  108. /**
  109. * Constructs a <code>Runtime(In)VisisbleAnnotations_attribute</code>.
  110. *
  111. * @param cp constant pool
  112. * @param attrname attribute name (<code>visibleTag</code> or
  113. * <code>invisibleTag</code>).
  114. * @param info the contents of this attribute. It does not
  115. * include <code>attribute_name_index</code> or
  116. * <code>attribute_length</code>.
  117. */
  118. public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info) {
  119. super(cp, attrname, info);
  120. }
  121. /**
  122. * Constructs an empty
  123. * <code>Runtime(In)VisisbleAnnotations_attribute</code>.
  124. * A new annotation can be later added to the created attribute
  125. * by <code>setAnnotations()</code>.
  126. *
  127. * @param cp constant pool
  128. * @param attrname attribute name (<code>visibleTag</code> or
  129. * <code>invisibleTag</code>).
  130. * @see #setAnnotations(Annotation[])
  131. */
  132. public AnnotationsAttribute(ConstPool cp, String attrname) {
  133. this(cp, attrname, new byte[] { 0, 0 });
  134. }
  135. /**
  136. * @param n the attribute name.
  137. */
  138. AnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
  139. throws IOException
  140. {
  141. super(cp, n, in);
  142. }
  143. /**
  144. * Returns <code>num_annotations</code>.
  145. */
  146. public int numAnnotations() {
  147. return ByteArray.readU16bit(info, 0);
  148. }
  149. /**
  150. * Copies this attribute and returns a new copy.
  151. */
  152. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  153. Copier copier = new Copier(info, constPool, newCp, classnames);
  154. try {
  155. copier.annotationArray();
  156. return new AnnotationsAttribute(newCp, getName(), copier.close());
  157. }
  158. catch (Exception e) {
  159. throw new RuntimeException(e.toString());
  160. }
  161. }
  162. /**
  163. * Parses the annotations and returns a data structure representing
  164. * the annotation with the specified type. See also
  165. * <code>getAnnotations()</code> as to the returned data structure.
  166. *
  167. * @param type the annotation type.
  168. * @return null if the specified annotation type is not included.
  169. * @see #getAnnotations()
  170. */
  171. public Annotation getAnnotation(String type) {
  172. Annotation[] annotations = getAnnotations();
  173. for (int i = 0; i < annotations.length; i++) {
  174. if (annotations[i].getTypeName().equals(type))
  175. return annotations[i];
  176. }
  177. return null;
  178. }
  179. /**
  180. * Adds an annotation. If there is an annotation with the same type,
  181. * it is removed before the new annotation is added.
  182. *
  183. * @param annotation the added annotation.
  184. */
  185. public void addAnnotation(Annotation annotation) {
  186. String type = annotation.getTypeName();
  187. Annotation[] annotations = getAnnotations();
  188. for (int i = 0; i < annotations.length; i++) {
  189. if (annotations[i].getTypeName().equals(type)) {
  190. annotations[i] = annotation;
  191. setAnnotations(annotations);
  192. return;
  193. }
  194. }
  195. Annotation[] newlist = new Annotation[annotations.length + 1];
  196. System.arraycopy(annotations, 0, newlist, 0, annotations.length);
  197. newlist[annotations.length] = annotation;
  198. setAnnotations(newlist);
  199. }
  200. /**
  201. * Parses the annotations and returns a data structure representing
  202. * that parsed annotations. Note that changes of the node values of the
  203. * returned tree are not reflected on the annotations represented by
  204. * this object unless the tree is copied back to this object by
  205. * <code>setAnnotations()</code>.
  206. *
  207. * @see #setAnnotations(Annotation[])
  208. */
  209. public Annotation[] getAnnotations() {
  210. try {
  211. return new Parser(info, constPool).parseAnnotations();
  212. }
  213. catch (Exception e) {
  214. throw new RuntimeException(e.toString());
  215. }
  216. }
  217. /**
  218. * Changes the annotations represented by this object according to
  219. * the given array of <code>Annotation</code> objects.
  220. *
  221. * @param annotations the data structure representing the
  222. * new annotations.
  223. */
  224. public void setAnnotations(Annotation[] annotations) {
  225. ByteArrayOutputStream output = new ByteArrayOutputStream();
  226. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  227. try {
  228. int n = annotations.length;
  229. writer.numAnnotations(n);
  230. for (int i = 0; i < n; ++i)
  231. annotations[i].write(writer);
  232. writer.close();
  233. }
  234. catch (IOException e) {
  235. throw new RuntimeException(e); // should never reach here.
  236. }
  237. set(output.toByteArray());
  238. }
  239. /**
  240. * Changes the annotations. A call to this method is equivalent to:
  241. * <ul><pre>setAnnotations(new Annotation[] { annotation })</pre></ul>
  242. *
  243. * @param annotation the data structure representing
  244. * the new annotation.
  245. */
  246. public void setAnnotation(Annotation annotation) {
  247. setAnnotations(new Annotation[] { annotation });
  248. }
  249. /**
  250. * Returns a string representation of this object.
  251. */
  252. public String toString() {
  253. Annotation[] a = getAnnotations();
  254. StringBuffer sbuf = new StringBuffer();
  255. int i = 0;
  256. while (i < a.length) {
  257. sbuf.append(a[i++].toString());
  258. if (i != a.length)
  259. sbuf.append(", ");
  260. }
  261. return sbuf.toString();
  262. }
  263. static class Walker {
  264. byte[] info;
  265. Walker(byte[] attrInfo) {
  266. info = attrInfo;
  267. }
  268. final void parameters() throws Exception {
  269. int numParam = info[0] & 0xff;
  270. parameters(numParam, 1);
  271. }
  272. void parameters(int numParam, int pos) throws Exception {
  273. for (int i = 0; i < numParam; ++i)
  274. pos = annotationArray(pos);
  275. }
  276. final void annotationArray() throws Exception {
  277. annotationArray(0);
  278. }
  279. final int annotationArray(int pos) throws Exception {
  280. int num = ByteArray.readU16bit(info, pos);
  281. return annotationArray(pos + 2, num);
  282. }
  283. int annotationArray(int pos, int num) throws Exception {
  284. for (int i = 0; i < num; ++i)
  285. pos = annotation(pos);
  286. return pos;
  287. }
  288. final int annotation(int pos) throws Exception {
  289. int type = ByteArray.readU16bit(info, pos);
  290. int numPairs = ByteArray.readU16bit(info, pos + 2);
  291. return annotation(pos + 4, type, numPairs);
  292. }
  293. int annotation(int pos, int type, int numPairs) throws Exception {
  294. for (int j = 0; j < numPairs; ++j)
  295. pos = memberValuePair(pos);
  296. return pos;
  297. }
  298. final int memberValuePair(int pos) throws Exception {
  299. int nameIndex = ByteArray.readU16bit(info, pos);
  300. return memberValuePair(pos + 2, nameIndex);
  301. }
  302. int memberValuePair(int pos, int nameIndex) throws Exception {
  303. return memberValue(pos);
  304. }
  305. final int memberValue(int pos) throws Exception {
  306. int tag = info[pos] & 0xff;
  307. if (tag == 'e') {
  308. int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
  309. int constNameIndex = ByteArray.readU16bit(info, pos + 3);
  310. enumMemberValue(typeNameIndex, constNameIndex);
  311. return pos + 5;
  312. }
  313. else if (tag == 'c') {
  314. int index = ByteArray.readU16bit(info, pos + 1);
  315. classMemberValue(index);
  316. return pos + 3;
  317. }
  318. else if (tag == '@')
  319. return annotationMemberValue(pos + 1);
  320. else if (tag == '[') {
  321. int num = ByteArray.readU16bit(info, pos + 1);
  322. return arrayMemberValue(pos + 3, num);
  323. }
  324. else { // primitive types or String.
  325. int index = ByteArray.readU16bit(info, pos + 1);
  326. constValueMember(tag, index);
  327. return pos + 3;
  328. }
  329. }
  330. void constValueMember(int tag, int index) throws Exception {}
  331. void enumMemberValue(int typeNameIndex, int constNameIndex)
  332. throws Exception {
  333. }
  334. void classMemberValue(int index) throws Exception {}
  335. int annotationMemberValue(int pos) throws Exception {
  336. return annotation(pos);
  337. }
  338. int arrayMemberValue(int pos, int num) throws Exception {
  339. for (int i = 0; i < num; ++i) {
  340. pos = memberValue(pos);
  341. }
  342. return pos;
  343. }
  344. }
  345. static class Copier extends Walker {
  346. ByteArrayOutputStream output;
  347. AnnotationsWriter writer;
  348. ConstPool srcPool, destPool;
  349. Map classnames;
  350. /**
  351. * Constructs a copier. This copier renames some class names
  352. * into the new names specified by <code>map</code> when it copies
  353. * an annotation attribute.
  354. *
  355. * @param info the source attribute.
  356. * @param src the constant pool of the source class.
  357. * @param dest the constant pool of the destination class.
  358. * @param map pairs of replaced and substituted class names.
  359. * It can be null.
  360. */
  361. Copier(byte[] info, ConstPool src, ConstPool dest, Map map) {
  362. super(info);
  363. output = new ByteArrayOutputStream();
  364. writer = new AnnotationsWriter(output, dest);
  365. srcPool = src;
  366. destPool = dest;
  367. classnames = map;
  368. }
  369. byte[] close() throws IOException {
  370. writer.close();
  371. return output.toByteArray();
  372. }
  373. void parameters(int numParam, int pos) throws Exception {
  374. writer.numParameters(numParam);
  375. super.parameters(numParam, pos);
  376. }
  377. int annotationArray(int pos, int num) throws Exception {
  378. writer.numAnnotations(num);
  379. return super.annotationArray(pos, num);
  380. }
  381. int annotation(int pos, int type, int numPairs) throws Exception {
  382. writer.annotation(copy(type), numPairs);
  383. return super.annotation(pos, type, numPairs);
  384. }
  385. int memberValuePair(int pos, int nameIndex) throws Exception {
  386. writer.memberValuePair(copy(nameIndex));
  387. return super.memberValuePair(pos, nameIndex);
  388. }
  389. void constValueMember(int tag, int index) throws Exception {
  390. writer.constValueIndex(tag, copy(index));
  391. super.constValueMember(tag, index);
  392. }
  393. void enumMemberValue(int typeNameIndex, int constNameIndex)
  394. throws Exception
  395. {
  396. writer.enumConstValue(copy(typeNameIndex), copy(constNameIndex));
  397. super.enumMemberValue(typeNameIndex, constNameIndex);
  398. }
  399. void classMemberValue(int index) throws Exception {
  400. writer.classInfoIndex(copy(index));
  401. super.classMemberValue(index);
  402. }
  403. int annotationMemberValue(int pos) throws Exception {
  404. writer.annotationValue();
  405. return super.annotationMemberValue(pos);
  406. }
  407. int arrayMemberValue(int pos, int num) throws Exception {
  408. writer.arrayValue(num);
  409. return super.arrayMemberValue(pos, num);
  410. }
  411. /**
  412. * Copies a constant pool entry into the destination constant pool
  413. * and returns the index of the copied entry.
  414. *
  415. * @param srcIndex the index of the copied entry into the source
  416. * constant pool.
  417. * @return the index of the copied item into the destination
  418. * constant pool.
  419. */
  420. int copy(int srcIndex) {
  421. return srcPool.copy(srcIndex, destPool, classnames);
  422. }
  423. }
  424. static class Parser extends Walker {
  425. ConstPool pool;
  426. Annotation[][] allParams; // all parameters
  427. Annotation[] allAnno; // all annotations
  428. Annotation currentAnno; // current annotation
  429. MemberValue currentMember; // current member
  430. /**
  431. * Constructs a parser. This parser constructs a parse tree of
  432. * the annotations.
  433. *
  434. * @param info the attribute.
  435. * @param src the constant pool.
  436. */
  437. Parser(byte[] info, ConstPool cp) {
  438. super(info);
  439. pool = cp;
  440. }
  441. Annotation[][] parseParameters() throws Exception {
  442. parameters();
  443. return allParams;
  444. }
  445. Annotation[] parseAnnotations() throws Exception {
  446. annotationArray();
  447. return allAnno;
  448. }
  449. MemberValue parseMemberValue() throws Exception {
  450. memberValue(0);
  451. return currentMember;
  452. }
  453. void parameters(int numParam, int pos) throws Exception {
  454. Annotation[][] params = new Annotation[numParam][];
  455. for (int i = 0; i < numParam; ++i) {
  456. pos = annotationArray(pos);
  457. params[i] = allAnno;
  458. }
  459. allParams = params;
  460. }
  461. int annotationArray(int pos, int num) throws Exception {
  462. Annotation[] array = new Annotation[num];
  463. for (int i = 0; i < num; ++i) {
  464. pos = annotation(pos);
  465. array[i] = currentAnno;
  466. }
  467. allAnno = array;
  468. return pos;
  469. }
  470. int annotation(int pos, int type, int numPairs) throws Exception {
  471. currentAnno = new Annotation(type, pool);
  472. return super.annotation(pos, type, numPairs);
  473. }
  474. int memberValuePair(int pos, int nameIndex) throws Exception {
  475. pos = super.memberValuePair(pos, nameIndex);
  476. currentAnno.addMemberValue(nameIndex, currentMember);
  477. return pos;
  478. }
  479. void constValueMember(int tag, int index) throws Exception {
  480. MemberValue m;
  481. ConstPool cp = pool;
  482. switch (tag) {
  483. case 'B' :
  484. m = new ByteMemberValue(index, cp);
  485. break;
  486. case 'C' :
  487. m = new CharMemberValue(index, cp);
  488. break;
  489. case 'D' :
  490. m = new DoubleMemberValue(index, cp);
  491. break;
  492. case 'F' :
  493. m = new FloatMemberValue(index, cp);
  494. break;
  495. case 'I' :
  496. m = new IntegerMemberValue(index, cp);
  497. break;
  498. case 'J' :
  499. m = new LongMemberValue(index, cp);
  500. break;
  501. case 'S' :
  502. m = new ShortMemberValue(index, cp);
  503. break;
  504. case 'Z' :
  505. m = new BooleanMemberValue(index, cp);
  506. break;
  507. case 's' :
  508. m = new StringMemberValue(index, cp);
  509. break;
  510. default :
  511. throw new RuntimeException("unknown tag:" + tag);
  512. }
  513. currentMember = m;
  514. super.constValueMember(tag, index);
  515. }
  516. void enumMemberValue(int typeNameIndex, int constNameIndex)
  517. throws Exception
  518. {
  519. currentMember = new EnumMemberValue(typeNameIndex,
  520. constNameIndex, pool);
  521. super.enumMemberValue(typeNameIndex, constNameIndex);
  522. }
  523. void classMemberValue(int index) throws Exception {
  524. currentMember = new ClassMemberValue(index, pool);
  525. super.classMemberValue(index);
  526. }
  527. int annotationMemberValue(int pos) throws Exception {
  528. Annotation anno = currentAnno;
  529. pos = super.annotationMemberValue(pos);
  530. currentMember = new AnnotationMemberValue(currentAnno, pool);
  531. currentAnno = anno;
  532. return pos;
  533. }
  534. int arrayMemberValue(int pos, int num) throws Exception {
  535. ArrayMemberValue amv = new ArrayMemberValue(pool);
  536. MemberValue[] elements = new MemberValue[num];
  537. for (int i = 0; i < num; ++i) {
  538. pos = memberValue(pos);
  539. elements[i] = currentMember;
  540. }
  541. amv.setValue(elements);
  542. currentMember = amv;
  543. return pos;
  544. }
  545. }
  546. }