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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2004 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.invisibleTag)</code>
  28. * in <code>ClassFile</code>, <code>MethodInfo</code>,
  29. * or <code>FieldInfo</code>. The obtained attribute is a
  30. * runtime invisible annotations attribute.
  31. * If the parameter is
  32. * <code>AnnotationAttribute.visibleTag</code>, then the obtained
  33. * attribute is a runtime visible one.
  34. *
  35. * <p>If you want to record a new AnnotationAttribute object, execute the
  36. * following snippet:
  37. *
  38. * <ul><pre>
  39. * ClassFile cf = ... ;
  40. * ConstPool cp = cf.getConstPool();
  41. * AnnotationsAttribute attr
  42. * = new AnnotationsAttribute(cp, AnnotationsAttribute.invisibleTag);
  43. * Annotation a = new Annotation("Author", cp);
  44. * a.addMemberValue("name", new StringMemberValue("Chiba", cp));
  45. * attr.setAnnotation(a);
  46. * cf.addAttribute(attr);
  47. * </pre></ul>
  48. */
  49. public class AnnotationsAttribute extends AttributeInfo {
  50. /**
  51. * The name of the <code>RuntimeVisibleAnnotations</code> attribute.
  52. */
  53. public static final String visibleTag = "RuntimeVisibleAnnotations";
  54. /**
  55. * The name of the <code>RuntimeInvisibleAnnotations</code> attribute.
  56. */
  57. public static final String invisibleTag = "RuntimeInvisibleAnnotations";
  58. /**
  59. * Constructs a <code>Runtime(In)VisisbleAnnotations_attribute</code>.
  60. *
  61. * @param cp constant pool
  62. * @param attrname attribute name (<code>visibleTag</code> or
  63. * <code>invisibleTag</code>).
  64. * @param info the contents of this attribute. It does not
  65. * include <code>attribute_name_index</code> or
  66. * <code>attribute_length</code>.
  67. */
  68. public AnnotationsAttribute(ConstPool cp, String attrname, byte[] info) {
  69. super(cp, attrname, info);
  70. }
  71. /**
  72. * Constructs an empty
  73. * <code>Runtime(In)VisisbleAnnotations_attribute</code>.
  74. * A new annotation can be later added to the created attribute
  75. * by <code>setAnnotations()</code>.
  76. *
  77. * @param cp constant pool
  78. * @param attrname attribute name (<code>visibleTag</code> or
  79. * <code>invisibleTag</code>).
  80. * @see #setAnnotations(Annotation[])
  81. */
  82. public AnnotationsAttribute(ConstPool cp, String attrname) {
  83. this(cp, attrname, new byte[] { 0, 0 });
  84. }
  85. /**
  86. * @param n the attribute name.
  87. */
  88. AnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
  89. throws IOException
  90. {
  91. super(cp, n, in);
  92. }
  93. /**
  94. * Returns <code>num_annotations</code>.
  95. */
  96. public int numAnnotations() {
  97. return ByteArray.readU16bit(info, 0);
  98. }
  99. /**
  100. * Copies this attribute and returns a new copy.
  101. */
  102. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  103. Copier copier = new Copier(info, constPool, newCp, classnames);
  104. try {
  105. copier.annotationArray();
  106. return new AnnotationsAttribute(newCp, getName(), copier.close());
  107. }
  108. catch (Exception e) {
  109. throw new RuntimeException(e.toString());
  110. }
  111. }
  112. /**
  113. * Parses the annotations and returns a data structure representing
  114. * that parsed annotations. Note that changes of the node values of the
  115. * returned tree are not reflected on the annotations represented by
  116. * this object unless the tree is copied back to this object by
  117. * <code>setAnnotations()</code>.
  118. *
  119. * @see #setAnnotations(Annotation[])
  120. */
  121. public Annotation[] getAnnotations() {
  122. try {
  123. return new Parser(info, constPool).parseAnnotations();
  124. }
  125. catch (Exception e) {
  126. throw new RuntimeException(e.toString());
  127. }
  128. }
  129. /**
  130. * Changes the annotations represented by this object according to
  131. * the given array of <code>Annotation</code> objects.
  132. *
  133. * @param annotations the data structure representing the
  134. * new annotations.
  135. */
  136. public void setAnnotations(Annotation[] annotations) {
  137. ByteArrayOutputStream output = new ByteArrayOutputStream();
  138. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  139. try {
  140. int n = annotations.length;
  141. writer.numAnnotations(n);
  142. for (int i = 0; i < n; ++i)
  143. annotations[i].write(writer);
  144. writer.close();
  145. }
  146. catch (IOException e) {
  147. throw new RuntimeException(e); // should never reach here.
  148. }
  149. set(output.toByteArray());
  150. }
  151. /**
  152. * Changes the annotations. A call to this method is equivalent to:
  153. * <ul><pre>setAnnotations(new Annotation[] { annotation })</pre></ul>
  154. *
  155. * @param annotation the data structure representing
  156. * the new annotation.
  157. */
  158. public void setAnnotation(Annotation annotation) {
  159. setAnnotations(new Annotation[] { annotation });
  160. }
  161. /**
  162. * Returns a string representation of this object.
  163. */
  164. public String toString() {
  165. Annotation[] a = getAnnotations();
  166. StringBuffer sbuf = new StringBuffer();
  167. int i = 0;
  168. while (i < a.length) {
  169. sbuf.append(a[i++].toString());
  170. if (i != a.length)
  171. sbuf.append(", ");
  172. }
  173. return sbuf.toString();
  174. }
  175. static class Walker {
  176. byte[] info;
  177. Walker(byte[] attrInfo) {
  178. info = attrInfo;
  179. }
  180. final void parameters() throws Exception {
  181. int numParam = info[0] & 0xff;
  182. parameters(numParam, 1);
  183. }
  184. void parameters(int numParam, int pos) throws Exception {
  185. for (int i = 0; i < numParam; ++i)
  186. pos = annotationArray(pos);
  187. }
  188. final void annotationArray() throws Exception {
  189. annotationArray(0);
  190. }
  191. final int annotationArray(int pos) throws Exception {
  192. int num = ByteArray.readU16bit(info, pos);
  193. return annotationArray(pos + 2, num);
  194. }
  195. int annotationArray(int pos, int num) throws Exception {
  196. for (int i = 0; i < num; ++i)
  197. pos = annotation(pos);
  198. return pos;
  199. }
  200. final int annotation(int pos) throws Exception {
  201. int type = ByteArray.readU16bit(info, pos);
  202. int numPairs = ByteArray.readU16bit(info, pos + 2);
  203. return annotation(pos + 4, type, numPairs);
  204. }
  205. int annotation(int pos, int type, int numPairs) throws Exception {
  206. for (int j = 0; j < numPairs; ++j)
  207. pos = memberValuePair(pos);
  208. return pos;
  209. }
  210. final int memberValuePair(int pos) throws Exception {
  211. int nameIndex = ByteArray.readU16bit(info, pos);
  212. return memberValuePair(pos + 2, nameIndex);
  213. }
  214. int memberValuePair(int pos, int nameIndex) throws Exception {
  215. return memberValue(pos);
  216. }
  217. final int memberValue(int pos) throws Exception {
  218. int tag = info[pos] & 0xff;
  219. if (tag == 'e') {
  220. int typeNameIndex = ByteArray.readU16bit(info, pos + 1);
  221. int constNameIndex = ByteArray.readU16bit(info, pos + 3);
  222. enumMemberValue(typeNameIndex, constNameIndex);
  223. return pos + 5;
  224. }
  225. else if (tag == 'c') {
  226. int index = ByteArray.readU16bit(info, pos + 1);
  227. classMemberValue(index);
  228. return pos + 3;
  229. }
  230. else if (tag == '@')
  231. return annotationMemberValue(pos + 1);
  232. else if (tag == '[') {
  233. int num = ByteArray.readU16bit(info, pos + 1);
  234. return arrayMemberValue(pos + 3, num);
  235. }
  236. else { // primitive types or String.
  237. int index = ByteArray.readU16bit(info, pos + 1);
  238. constValueMember(tag, index);
  239. return pos + 3;
  240. }
  241. }
  242. void constValueMember(int tag, int index) throws Exception {}
  243. void enumMemberValue(int typeNameIndex, int constNameIndex)
  244. throws Exception {}
  245. void classMemberValue(int index) throws Exception {}
  246. int annotationMemberValue(int pos) throws Exception {
  247. return annotation(pos);
  248. }
  249. int arrayMemberValue(int pos, int num) throws Exception {
  250. for (int i = 0; i < num; ++i) {
  251. pos = memberValue(pos);
  252. }
  253. return pos;
  254. }
  255. }
  256. static class Copier extends Walker {
  257. ByteArrayOutputStream output;
  258. AnnotationsWriter writer;
  259. ConstPool srcPool, destPool;
  260. Map classnames;
  261. /**
  262. * Constructs a copier. This copier renames some class names
  263. * into the new names specified by <code>map</code> when it copies
  264. * an annotation attribute.
  265. *
  266. * @param info the source attribute.
  267. * @param src the constant pool of the source class.
  268. * @param dest the constant pool of the destination class.
  269. * @param map pairs of replaced and substituted class names.
  270. * It can be null.
  271. */
  272. Copier(byte[] info, ConstPool src, ConstPool dest, Map map) {
  273. super(info);
  274. output = new ByteArrayOutputStream();
  275. writer = new AnnotationsWriter(output, dest);
  276. srcPool = src;
  277. destPool = dest;
  278. classnames = map;
  279. }
  280. byte[] close() throws IOException {
  281. writer.close();
  282. return output.toByteArray();
  283. }
  284. void parameters(int numParam, int pos) throws Exception {
  285. writer.numParameters(numParam);
  286. super.parameters(numParam, pos);
  287. }
  288. int annotationArray(int pos, int num) throws Exception {
  289. writer.numAnnotations(num);
  290. return super.annotationArray(pos, num);
  291. }
  292. int annotation(int pos, int type, int numPairs) throws Exception {
  293. writer.annotation(copy(type), numPairs);
  294. return super.annotation(pos, type, numPairs);
  295. }
  296. int memberValuePair(int pos, int nameIndex) throws Exception {
  297. writer.memberValuePair(copy(nameIndex));
  298. return super.memberValuePair(pos, nameIndex);
  299. }
  300. void constValueMember(int tag, int index) throws Exception {
  301. writer.constValueIndex(tag, copy(index));
  302. super.constValueMember(tag, index);
  303. }
  304. void enumMemberValue(int typeNameIndex, int constNameIndex)
  305. throws Exception
  306. {
  307. writer.enumConstValue(copy(typeNameIndex), copy(constNameIndex));
  308. super.enumMemberValue(typeNameIndex, constNameIndex);
  309. }
  310. void classMemberValue(int index) throws Exception {
  311. writer.classInfoIndex(copy(index));
  312. super.classMemberValue(index);
  313. }
  314. int annotationMemberValue(int pos) throws Exception {
  315. writer.annotationValue();
  316. return super.annotationMemberValue(pos);
  317. }
  318. int arrayMemberValue(int pos, int num) throws Exception {
  319. writer.arrayValue(num);
  320. return super.arrayMemberValue(pos, num);
  321. }
  322. /**
  323. * Copies a constant pool entry into the destination constant pool
  324. * and returns the index of the copied entry.
  325. *
  326. * @param srcIndex the index of the copied entry into the source
  327. * constant pool.
  328. * @return the index of the copied item into the destination
  329. * constant pool.
  330. */
  331. int copy(int srcIndex) {
  332. return srcPool.copy(srcIndex, destPool, classnames);
  333. }
  334. }
  335. static class Parser extends Walker {
  336. ConstPool pool;
  337. Annotation[][] allParams; // all parameters
  338. Annotation[] allAnno; // all annotations
  339. Annotation currentAnno; // current annotation
  340. MemberValue memberValue;
  341. /**
  342. * Constructs a parser. This parser constructs a parse tree of
  343. * the annotations.
  344. *
  345. * @param info the attribute.
  346. * @param src the constant pool.
  347. */
  348. Parser(byte[] info, ConstPool cp) {
  349. super(info);
  350. pool = cp;
  351. }
  352. Annotation[][] parseParameters() throws Exception {
  353. parameters();
  354. return allParams;
  355. }
  356. Annotation[] parseAnnotations() throws Exception {
  357. annotationArray();
  358. return allAnno;
  359. }
  360. void parameters(int numParam, int pos) throws Exception {
  361. Annotation[][] params = new Annotation[numParam][];
  362. for (int i = 0; i < numParam; ++i) {
  363. pos = annotationArray(pos);
  364. params[i] = allAnno;
  365. }
  366. allParams = params;
  367. }
  368. int annotationArray(int pos, int num) throws Exception {
  369. Annotation[] array = new Annotation[num];
  370. for (int i = 0; i < num; ++i) {
  371. pos = annotation(pos);
  372. array[i] = currentAnno;
  373. }
  374. allAnno = array;
  375. return pos;
  376. }
  377. int annotation(int pos, int type, int numPairs) throws Exception {
  378. currentAnno = new Annotation(type, pool);
  379. return super.annotation(pos, type, numPairs);
  380. }
  381. int memberValuePair(int pos, int nameIndex) throws Exception {
  382. pos = super.memberValuePair(pos, nameIndex);
  383. currentAnno.addMemberValue(nameIndex, memberValue);
  384. return pos;
  385. }
  386. void constValueMember(int tag, int index) throws Exception {
  387. MemberValue m;
  388. ConstPool cp = pool;
  389. switch (tag) {
  390. case 'B' :
  391. m = new ByteMemberValue(index, cp);
  392. break;
  393. case 'C' :
  394. m = new CharMemberValue(index, cp);
  395. break;
  396. case 'D' :
  397. m = new DoubleMemberValue(index, cp);
  398. break;
  399. case 'F' :
  400. m = new FloatMemberValue(index, cp);
  401. break;
  402. case 'I' :
  403. m = new IntegerMemberValue(index, cp);
  404. break;
  405. case 'J' :
  406. m = new LongMemberValue(index, cp);
  407. break;
  408. case 'S' :
  409. m = new ShortMemberValue(index, cp);
  410. break;
  411. case 'Z' :
  412. m = new BooleanMemberValue(index, cp);
  413. break;
  414. case 's' :
  415. m = new StringMemberValue(index, cp);
  416. break;
  417. default :
  418. throw new RuntimeException("unknown tag:" + tag);
  419. }
  420. memberValue = m;
  421. super.constValueMember(tag, index);
  422. }
  423. void enumMemberValue(int typeNameIndex, int constNameIndex)
  424. throws Exception
  425. {
  426. memberValue = new EnumMemberValue(typeNameIndex,
  427. constNameIndex, pool);
  428. super.enumMemberValue(typeNameIndex, constNameIndex);
  429. }
  430. void classMemberValue(int index) throws Exception {
  431. memberValue = new ClassMemberValue(index, pool);
  432. super.classMemberValue(index);
  433. }
  434. int annotationMemberValue(int pos) throws Exception {
  435. Annotation anno = currentAnno;
  436. pos = super.annotationMemberValue(pos);
  437. memberValue = new AnnotationMemberValue(currentAnno, pool);
  438. currentAnno = anno;
  439. return pos;
  440. }
  441. int arrayMemberValue(int pos, int num) throws Exception {
  442. ArrayMemberValue amv = new ArrayMemberValue(pool);
  443. MemberValue[] elements = new MemberValue[num];
  444. for (int i = 0; i < num; ++i) {
  445. pos = memberValue(pos);
  446. elements[i] = memberValue;
  447. }
  448. amv.setValue(elements);
  449. memberValue = amv;
  450. return pos;
  451. }
  452. }
  453. }