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.

ParameterAnnotationsAttribute.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.io.IOException;
  20. import java.io.DataInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import javassist.bytecode.AnnotationsAttribute.Copier;
  23. import javassist.bytecode.AnnotationsAttribute.Parser;
  24. import javassist.bytecode.AnnotationsAttribute.Renamer;
  25. import javassist.bytecode.annotation.*;
  26. /**
  27. * A class representing <code>RuntimeVisibleAnnotations_attribute</code> and
  28. * <code>RuntimeInvisibleAnnotations_attribute</code>.
  29. *
  30. * <p>To obtain an ParameterAnnotationAttribute object, invoke
  31. * <code>getAttribute(ParameterAnnotationsAttribute.invisibleTag)</code>
  32. * in <code>MethodInfo</code>.
  33. * The obtained attribute is a
  34. * runtime invisible annotations attribute.
  35. * If the parameter is
  36. * <code>ParameterAnnotationAttribute.visibleTag</code>, then the obtained
  37. * attribute is a runtime visible one.
  38. */
  39. public class ParameterAnnotationsAttribute extends AttributeInfo {
  40. /**
  41. * The name of the <code>RuntimeVisibleParameterAnnotations</code>
  42. * attribute.
  43. */
  44. public static final String visibleTag
  45. = "RuntimeVisibleParameterAnnotations";
  46. /**
  47. * The name of the <code>RuntimeInvisibleParameterAnnotations</code>
  48. * attribute.
  49. */
  50. public static final String invisibleTag
  51. = "RuntimeInvisibleParameterAnnotations";
  52. /**
  53. * Constructs
  54. * a <code>Runtime(In)VisibleParameterAnnotations_attribute</code>.
  55. *
  56. * @param cp constant pool
  57. * @param attrname attribute name (<code>visibleTag</code> or
  58. * <code>invisibleTag</code>).
  59. * @param info the contents of this attribute. It does not
  60. * include <code>attribute_name_index</code> or
  61. * <code>attribute_length</code>.
  62. */
  63. public ParameterAnnotationsAttribute(ConstPool cp, String attrname,
  64. byte[] info) {
  65. super(cp, attrname, info);
  66. }
  67. /**
  68. * Constructs an empty
  69. * <code>Runtime(In)VisibleParameterAnnotations_attribute</code>.
  70. * A new annotation can be later added to the created attribute
  71. * by <code>setAnnotations()</code>.
  72. *
  73. * @param cp constant pool
  74. * @param attrname attribute name (<code>visibleTag</code> or
  75. * <code>invisibleTag</code>).
  76. * @see #setAnnotations(Annotation[][])
  77. */
  78. public ParameterAnnotationsAttribute(ConstPool cp, String attrname) {
  79. this(cp, attrname, new byte[] { 0 });
  80. }
  81. /**
  82. * @param n the attribute name.
  83. */
  84. ParameterAnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
  85. throws IOException
  86. {
  87. super(cp, n, in);
  88. }
  89. /**
  90. * Returns <code>num_parameters</code>.
  91. */
  92. public int numParameters() {
  93. return info[0] & 0xff;
  94. }
  95. /**
  96. * Copies this attribute and returns a new copy.
  97. */
  98. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  99. Copier copier = new Copier(info, constPool, newCp, classnames);
  100. try {
  101. copier.parameters();
  102. return new ParameterAnnotationsAttribute(newCp, getName(),
  103. copier.close());
  104. }
  105. catch (Exception e) {
  106. throw new RuntimeException(e.toString());
  107. }
  108. }
  109. /**
  110. * Parses the annotations and returns a data structure representing
  111. * that parsed annotations. Note that changes of the node values of the
  112. * returned tree are not reflected on the annotations represented by
  113. * this object unless the tree is copied back to this object by
  114. * <code>setAnnotations()</code>.
  115. *
  116. * @return Each element of the returned array represents an array of
  117. * annotations that are associated with each method parameter.
  118. *
  119. * @see #setAnnotations(Annotation[][])
  120. */
  121. public Annotation[][] getAnnotations() {
  122. try {
  123. return new Parser(info, constPool).parseParameters();
  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 params the data structure representing the
  134. * new annotations. Every element of this array
  135. * is an array of <code>Annotation</code> and
  136. * it represens annotations of each method parameter.
  137. */
  138. public void setAnnotations(Annotation[][] params) {
  139. ByteArrayOutputStream output = new ByteArrayOutputStream();
  140. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  141. try {
  142. int n = params.length;
  143. writer.numParameters(n);
  144. for (int i = 0; i < n; ++i) {
  145. Annotation[] anno = params[i];
  146. writer.numAnnotations(anno.length);
  147. for (int j = 0; j < anno.length; ++j)
  148. anno[j].write(writer);
  149. }
  150. writer.close();
  151. }
  152. catch (IOException e) {
  153. throw new RuntimeException(e); // should never reach here.
  154. }
  155. set(output.toByteArray());
  156. }
  157. /**
  158. * @param oldname a JVM class name.
  159. * @param newname a JVM class name.
  160. */
  161. void renameClass(String oldname, String newname) {
  162. HashMap map = new HashMap();
  163. map.put(oldname, newname);
  164. renameClass(map);
  165. }
  166. void renameClass(Map classnames) {
  167. Renamer renamer = new Renamer(info, getConstPool(), classnames);
  168. try {
  169. renamer.parameters();
  170. } catch (Exception e) {
  171. throw new RuntimeException(e);
  172. }
  173. }
  174. void getRefClasses(Map classnames) { renameClass(classnames); }
  175. /**
  176. * Returns a string representation of this object.
  177. */
  178. public String toString() {
  179. Annotation[][] aa = getAnnotations();
  180. StringBuilder sbuf = new StringBuilder();
  181. int k = 0;
  182. while (k < aa.length) {
  183. Annotation[] a = aa[k++];
  184. int i = 0;
  185. while (i < a.length) {
  186. sbuf.append(a[i++].toString());
  187. if (i != a.length)
  188. sbuf.append(" ");
  189. }
  190. if (k != aa.length)
  191. sbuf.append(", ");
  192. }
  193. return sbuf.toString();
  194. }
  195. }