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.

AnnotationsWriter.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode.annotation;
  17. import java.io.*;
  18. import javassist.bytecode.ByteArray;
  19. import javassist.bytecode.ConstPool;
  20. /**
  21. * A convenience class for constructing a
  22. * <code>..Annotations_attribute</code>.
  23. * See the source code of the <code>AnnotationsAttribute.Copier</code> class.
  24. *
  25. * <p>The following code snippet is an example of use of this class:
  26. *
  27. * <ul><pre>
  28. * ConstPool pool = ...;
  29. * output = new ByteArrayOutputStream();
  30. * writer = new AnnotationsWriter(output, pool);
  31. *
  32. * writer.numAnnotations(1);
  33. * writer.annotation("Author", 2);
  34. * writer.memberValuePair("name");
  35. * writer.constValueIndex("chiba");
  36. * writer.memberValuePair("address");
  37. * writer.constValueIndex("tokyo");
  38. *
  39. * writer.close();
  40. * byte[] attribute_info = output.toByteArray();
  41. * AnnotationsAttribute anno
  42. * = new AnnotationsAttribute(pool, AnnotationsAttribute.visibleTag,
  43. * attribute_info);
  44. * </pre></ul>
  45. *
  46. * <p>The code snippet above generates the annotation attribute
  47. * corresponding to this annotation:
  48. *
  49. * <ul><pre>
  50. * &nbsp;@Author(name = "chiba", address = "tokyo")
  51. * </pre></ul>
  52. *
  53. * @see javassist.bytecode.AnnotationsAttribute
  54. * @see javassist.bytecode.ParameterAnnotationsAttribute
  55. */
  56. public class AnnotationsWriter {
  57. private OutputStream output;
  58. private ConstPool pool;
  59. /**
  60. * Constructs with the given output stream.
  61. *
  62. * @param os the output stream.
  63. * @param cp the constant pool.
  64. */
  65. public AnnotationsWriter(OutputStream os, ConstPool cp) {
  66. output = os;
  67. pool = cp;
  68. }
  69. /**
  70. * Obtains the constant pool given to the constructor.
  71. */
  72. public ConstPool getConstPool() {
  73. return pool;
  74. }
  75. /**
  76. * Closes the output stream.
  77. *
  78. */
  79. public void close() throws IOException {
  80. output.close();
  81. }
  82. /**
  83. * Writes <code>num_parameters</code> in
  84. * <code>Runtime(In)VisibleParameterAnnotations_attribute</code>.
  85. * This method must be followed by <code>num</code> calls to
  86. * <code>numAnnotations()</code>.
  87. */
  88. public void numParameters(int num) throws IOException {
  89. output.write(num);
  90. }
  91. /**
  92. * Writes <code>num_annotations</code> in
  93. * <code>Runtime(In)VisibleAnnotations_attribute</code>.
  94. * This method must be followed by <code>num</code> calls to
  95. * <code>annotation()</code>.
  96. */
  97. public void numAnnotations(int num) throws IOException {
  98. write16bit(num);
  99. }
  100. /**
  101. * Writes <code>annotation</code>.
  102. * This method must be followed by <code>numMemberValuePairs</code>
  103. * calls to <code>memberValuePair()</code>.
  104. *
  105. * @param type the annotation interface name.
  106. * @param numMemberValuePairs <code>num_member_value_pairs</code>
  107. * in <code>annotation</code>.
  108. */
  109. public void annotation(String type, int numMemberValuePairs)
  110. throws IOException
  111. {
  112. annotation(pool.addUtf8Info(type), numMemberValuePairs);
  113. }
  114. /**
  115. * Writes <code>annotation</code>.
  116. * This method must be followed by <code>numMemberValuePairs</code>
  117. * calls to <code>memberValuePair()</code>.
  118. *
  119. * @param typeIndex <code>type_index</code> in <code>annotation</code>.
  120. * @param numMemberValuePairs <code>num_member_value_pairs</code>
  121. * in <code>annotation</code>.
  122. */
  123. public void annotation(int typeIndex, int numMemberValuePairs)
  124. throws IOException
  125. {
  126. write16bit(typeIndex);
  127. write16bit(numMemberValuePairs);
  128. }
  129. /**
  130. * Writes an element of a <code>member_value_pairs</code> array
  131. * in <code>annotation</code>.
  132. * This method must be followed by a
  133. * call to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
  134. * etc.
  135. *
  136. * @param memberName the name of the annotation type member.
  137. */
  138. public void memberValuePair(String memberName) throws IOException {
  139. memberValuePair(pool.addUtf8Info(memberName));
  140. }
  141. /**
  142. * Writes an element of a <code>member_value_pairs</code> array
  143. * in <code>annotation</code>.
  144. * This method must be followed by a
  145. * call to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
  146. * etc.
  147. *
  148. * @param memberNameIndex <code>member_name_index</code>
  149. * in <code>member_value_pairs</code> array.
  150. */
  151. public void memberValuePair(int memberNameIndex) throws IOException {
  152. write16bit(memberNameIndex);
  153. }
  154. /**
  155. * Writes <code>tag</code> and <code>const_value_index</code>
  156. * in <code>member_value</code>.
  157. *
  158. * @param value the constant value.
  159. */
  160. public void constValueIndex(boolean value) throws IOException {
  161. constValueIndex('Z', pool.addIntegerInfo(value ? 1 : 0));
  162. }
  163. /**
  164. * Writes <code>tag</code> and <code>const_value_index</code>
  165. * in <code>member_value</code>.
  166. *
  167. * @param value the constant value.
  168. */
  169. public void constValueIndex(byte value) throws IOException {
  170. constValueIndex('B', pool.addIntegerInfo(value));
  171. }
  172. /**
  173. * Writes <code>tag</code> and <code>const_value_index</code>
  174. * in <code>member_value</code>.
  175. *
  176. * @param value the constant value.
  177. */
  178. public void constValueIndex(char value) throws IOException {
  179. constValueIndex('C', pool.addIntegerInfo(value));
  180. }
  181. /**
  182. * Writes <code>tag</code> and <code>const_value_index</code>
  183. * in <code>member_value</code>.
  184. *
  185. * @param value the constant value.
  186. */
  187. public void constValueIndex(short value) throws IOException {
  188. constValueIndex('S', pool.addIntegerInfo(value));
  189. }
  190. /**
  191. * Writes <code>tag</code> and <code>const_value_index</code>
  192. * in <code>member_value</code>.
  193. *
  194. * @param value the constant value.
  195. */
  196. public void constValueIndex(int value) throws IOException {
  197. constValueIndex('I', pool.addIntegerInfo(value));
  198. }
  199. /**
  200. * Writes <code>tag</code> and <code>const_value_index</code>
  201. * in <code>member_value</code>.
  202. *
  203. * @param value the constant value.
  204. */
  205. public void constValueIndex(long value) throws IOException {
  206. constValueIndex('J', pool.addLongInfo(value));
  207. }
  208. /**
  209. * Writes <code>tag</code> and <code>const_value_index</code>
  210. * in <code>member_value</code>.
  211. *
  212. * @param value the constant value.
  213. */
  214. public void constValueIndex(float value) throws IOException {
  215. constValueIndex('F', pool.addFloatInfo(value));
  216. }
  217. /**
  218. * Writes <code>tag</code> and <code>const_value_index</code>
  219. * in <code>member_value</code>.
  220. *
  221. * @param value the constant value.
  222. */
  223. public void constValueIndex(double value) throws IOException {
  224. constValueIndex('D', pool.addDoubleInfo(value));
  225. }
  226. /**
  227. * Writes <code>tag</code> and <code>const_value_index</code>
  228. * in <code>member_value</code>.
  229. *
  230. * @param value the constant value.
  231. */
  232. public void constValueIndex(String value) throws IOException {
  233. constValueIndex('s', pool.addUtf8Info(value));
  234. }
  235. /**
  236. * Writes <code>tag</code> and <code>const_value_index</code>
  237. * in <code>member_value</code>.
  238. *
  239. * @param tag <code>tag</code> in <code>member_value</code>.
  240. * @param index <code>const_value_index</code>
  241. * in <code>member_value</code>.
  242. */
  243. public void constValueIndex(int tag, int index)
  244. throws IOException
  245. {
  246. output.write(tag);
  247. write16bit(index);
  248. }
  249. /**
  250. * Writes <code>tag</code> and <code>enum_const_value</code>
  251. * in <code>member_value</code>.
  252. *
  253. * @param typeName the type name of the enum constant.
  254. * @param constName the simple name of the enum constant.
  255. */
  256. public void enumConstValue(String typeName, String constName)
  257. throws IOException
  258. {
  259. enumConstValue(pool.addUtf8Info(typeName),
  260. pool.addUtf8Info(constName));
  261. }
  262. /**
  263. * Writes <code>tag</code> and <code>enum_const_value</code>
  264. * in <code>member_value</code>.
  265. *
  266. * @param typeNameIndex <code>type_name_index</code>
  267. * in <code>member_value</code>.
  268. * @param constNameIndex <code>const_name_index</code>
  269. * in <code>member_value</code>.
  270. */
  271. public void enumConstValue(int typeNameIndex, int constNameIndex)
  272. throws IOException
  273. {
  274. output.write('e');
  275. write16bit(typeNameIndex);
  276. write16bit(constNameIndex);
  277. }
  278. /**
  279. * Writes <code>tag</code> and <code>class_info_index</code>
  280. * in <code>member_value</code>.
  281. *
  282. * @param name the class name.
  283. */
  284. public void classInfoIndex(String name) throws IOException {
  285. classInfoIndex(pool.addUtf8Info(name));
  286. }
  287. /**
  288. * Writes <code>tag</code> and <code>class_info_index</code>
  289. * in <code>member_value</code>.
  290. *
  291. * @param index <code>class_info_index</code>
  292. */
  293. public void classInfoIndex(int index) throws IOException {
  294. output.write('c');
  295. write16bit(index);
  296. }
  297. /**
  298. * Writes <code>tag</code> and <code>annotation_value</code>
  299. * in <code>member_value</code>.
  300. * This method must be followed by a call to <code>annotation()</code>.
  301. */
  302. public void annotationValue() throws IOException {
  303. output.write('@');
  304. }
  305. /**
  306. * Writes <code>tag</code> and <code>array_value</code>
  307. * in <code>member_value</code>.
  308. * This method must be followed by <code>numValues</code> calls
  309. * to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
  310. * etc.
  311. *
  312. * @param numValues <code>num_values</code>
  313. * in <code>array_value</code>.
  314. */
  315. public void arrayValue(int numValues) throws IOException {
  316. output.write('[');
  317. write16bit(numValues);
  318. }
  319. private void write16bit(int value) throws IOException {
  320. byte[] buf = new byte[2];
  321. ByteArray.write16bit(value, buf, 0);
  322. output.write(buf);
  323. }
  324. }