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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.AnnotationsAttribute.Copier;
  21. import javassist.bytecode.AnnotationsAttribute.Parser;
  22. import javassist.bytecode.annotation.*;
  23. /**
  24. * A class representing <code>RuntimeVisibleAnnotations_attribute</code> and
  25. * <code>RuntimeInvisibleAnnotations_attribute</code>.
  26. *
  27. * <p>To obtain an ParameterAnnotationAttribute object, invoke
  28. * <code>getAttribute(ParameterAnnotationsAttribute.invisibleTag)</code>
  29. * in <code>MethodInfo</code>.
  30. * The obtained attribute is a
  31. * runtime invisible annotations attribute.
  32. * If the parameter is
  33. * <code>ParameterAnnotationAttribute.visibleTag</code>, then the obtained
  34. * attribute is a runtime visible one.
  35. */
  36. public class ParameterAnnotationsAttribute extends AttributeInfo {
  37. /**
  38. * The name of the <code>RuntimeVisibleParameterAnnotations</code>
  39. * attribute.
  40. */
  41. public static final String visibleTag
  42. = "RuntimeVisibleParameterAnnotations";
  43. /**
  44. * The name of the <code>RuntimeInvisibleParameterAnnotations</code>
  45. * attribute.
  46. */
  47. public static final String invisibleTag
  48. = "RuntimeInvisibleParameterAnnotations";
  49. /**
  50. * Constructs
  51. * a <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
  52. *
  53. * @param cp constant pool
  54. * @param attrname attribute name (<code>visibleTag</code> or
  55. * <code>invisibleTag</code>).
  56. * @param info the contents of this attribute. It does not
  57. * include <code>attribute_name_index</code> or
  58. * <code>attribute_length</code>.
  59. */
  60. public ParameterAnnotationsAttribute(ConstPool cp, String attrname,
  61. byte[] info) {
  62. super(cp, attrname, info);
  63. }
  64. /**
  65. * Constructs an empty
  66. * <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
  67. * A new annotation can be later added to the created attribute
  68. * by <code>setAnnotations()</code>.
  69. *
  70. * @param cp constant pool
  71. * @param attrname attribute name (<code>visibleTag</code> or
  72. * <code>invisibleTag</code>).
  73. * @see #setAnnotations(Annotation[][])
  74. */
  75. public ParameterAnnotationsAttribute(ConstPool cp, String attrname) {
  76. this(cp, attrname, new byte[] { 0 });
  77. }
  78. /**
  79. * @param n the attribute name.
  80. */
  81. ParameterAnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
  82. throws IOException
  83. {
  84. super(cp, n, in);
  85. }
  86. /**
  87. * Returns <code>num_parameters</code>.
  88. */
  89. public int numParameters() {
  90. return info[0] & 0xff;
  91. }
  92. /**
  93. * Copies this attribute and returns a new copy.
  94. */
  95. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  96. Copier copier = new Copier(info, constPool, newCp, classnames);
  97. try {
  98. copier.parameters();
  99. return new ParameterAnnotationsAttribute(newCp, getName(),
  100. copier.close());
  101. }
  102. catch (Exception e) {
  103. throw new RuntimeException(e.toString());
  104. }
  105. }
  106. /**
  107. * Parses the annotations and returns a data structure representing
  108. * that parsed annotations. Note that changes of the node values of the
  109. * returned tree are not reflected on the annotations represented by
  110. * this object unless the tree is copied back to this object by
  111. * <code>setAnnotations()</code>.
  112. *
  113. * @return Each element of the returned array represents an array of
  114. * annotations that are associated with each method parameter.
  115. *
  116. * @see #setAnnotations(Annotation[][])
  117. */
  118. public Annotation[][] getAnnotations() {
  119. try {
  120. return new Parser(info, constPool).parseParameters();
  121. }
  122. catch (Exception e) {
  123. throw new RuntimeException(e.toString());
  124. }
  125. }
  126. /**
  127. * Changes the annotations represented by this object according to
  128. * the given array of <code>Annotation</code> objects.
  129. *
  130. * @param params the data structure representing the
  131. * new annotations. Every element of this array
  132. * is an array of <code>Annotation</code> and
  133. * it represens annotations of each method parameter.
  134. */
  135. public void setAnnotations(Annotation[][] params) {
  136. ByteArrayOutputStream output = new ByteArrayOutputStream();
  137. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  138. try {
  139. int n = params.length;
  140. writer.numParameters(n);
  141. for (int i = 0; i < n; ++i) {
  142. Annotation[] anno = params[i];
  143. writer.numAnnotations(anno.length);
  144. for (int j = 0; j < anno.length; ++j)
  145. anno[j].write(writer);
  146. }
  147. writer.close();
  148. }
  149. catch (IOException e) {
  150. throw new RuntimeException(e); // should never reach here.
  151. }
  152. set(output.toByteArray());
  153. }
  154. }