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.

AnnotationDefaultAttribute.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.io.ByteArrayOutputStream;
  18. import java.io.DataInputStream;
  19. import java.io.IOException;
  20. import java.util.Map;
  21. import javassist.CtClass;
  22. import javassist.bytecode.annotation.AnnotationsWriter;
  23. import javassist.bytecode.annotation.MemberValue;
  24. /**
  25. * A class representing <code>AnnotationDefault_attribute</code>.
  26. *
  27. * <p>For example, if you declare the following annotation type:
  28. *
  29. * <pre>
  30. * &#64;interface Author {
  31. * String name() default "Shakespeare";
  32. * int age() default 99;
  33. * }
  34. * </pre>
  35. *
  36. * <p>The defautl values of <code>name</code> and <code>age</code>
  37. * are stored as annotation default attributes in <code>Author.class</code>.
  38. * The following code snippet obtains the default value of <code>name</code>:
  39. *
  40. * <pre>
  41. * ClassPool pool = ...
  42. * CtClass cc = pool.get("Author");
  43. * CtMethod cm = cc.getDeclaredMethod("age");
  44. * MethodInfo minfo = cm.getMethodInfo();
  45. * AnnotationDefaultAttribute ada
  46. * = (AnnotationDefaultAttribute)
  47. * minfo.getAttribute(AnnotationDefaultAttribute.tag);
  48. * MemberValue value = ada.getDefaultValue()); // default value of age
  49. * </pre>
  50. *
  51. * <p>If the following statement is executed after the code above,
  52. * the default value of age is set to 80:
  53. *
  54. * <pre>
  55. * ada.setDefaultValue(new IntegerMemberValue(minfo.getConstPool(), 80));
  56. * </pre>
  57. *
  58. * @see AnnotationsAttribute
  59. * @see javassist.bytecode.annotation.MemberValue
  60. */
  61. public class AnnotationDefaultAttribute extends AttributeInfo {
  62. /**
  63. * The name of the <code>AnnotationDefault</code> attribute.
  64. */
  65. public static final String tag = "AnnotationDefault";
  66. /**
  67. * Constructs an <code>AnnotationDefault_attribute</code>.
  68. *
  69. * @param cp constant pool
  70. * @param info the contents of this attribute. It does not
  71. * include <code>attribute_name_index</code> or
  72. * <code>attribute_length</code>.
  73. */
  74. public AnnotationDefaultAttribute(ConstPool cp, byte[] info) {
  75. super(cp, tag, info);
  76. }
  77. /**
  78. * Constructs an empty <code>AnnotationDefault_attribute</code>.
  79. * The default value can be set by <code>setDefaultValue()</code>.
  80. *
  81. * @param cp constant pool
  82. * @see #setDefaultValue(javassist.bytecode.annotation.MemberValue)
  83. */
  84. public AnnotationDefaultAttribute(ConstPool cp) {
  85. this(cp, new byte[] { 0, 0 });
  86. }
  87. /**
  88. * @param n the attribute name.
  89. */
  90. AnnotationDefaultAttribute(ConstPool cp, int n, DataInputStream in)
  91. throws IOException
  92. {
  93. super(cp, n, in);
  94. }
  95. /**
  96. * Copies this attribute and returns a new copy.
  97. */
  98. @Override
  99. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  100. AnnotationsAttribute.Copier copier
  101. = new AnnotationsAttribute.Copier(info, constPool, newCp, classnames);
  102. try {
  103. copier.memberValue(0);
  104. return new AnnotationDefaultAttribute(newCp, copier.close());
  105. }
  106. catch (Exception e) {
  107. throw new RuntimeException(e.toString());
  108. }
  109. }
  110. @Override
  111. void renameClass(String oldname, String newname) {
  112. try {
  113. MemberValue defaultValue = getDefaultValue();
  114. defaultValue.renameClass(oldname, newname);
  115. setDefaultValue(defaultValue);
  116. } catch (Exception e) {
  117. // ignore
  118. }
  119. }
  120. @Override
  121. void renameClass(Map<String, String> classnames) {
  122. try {
  123. MemberValue defaultValue = getDefaultValue();
  124. defaultValue.renameClass(classnames);
  125. setDefaultValue(defaultValue);
  126. } catch (Exception e) {
  127. // ignore
  128. }
  129. }
  130. /**
  131. * Obtains the default value represented by this attribute.
  132. */
  133. public MemberValue getDefaultValue()
  134. {
  135. try {
  136. return new AnnotationsAttribute.Parser(info, constPool)
  137. .parseMemberValue();
  138. }
  139. catch (Exception e) {
  140. throw new RuntimeException(e.toString());
  141. }
  142. }
  143. /**
  144. * Changes the default value represented by this attribute.
  145. *
  146. * @param value the new value.
  147. * @see javassist.bytecode.annotation.Annotation#createMemberValue(ConstPool, CtClass)
  148. */
  149. public void setDefaultValue(MemberValue value) {
  150. ByteArrayOutputStream output = new ByteArrayOutputStream();
  151. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  152. try {
  153. value.write(writer);
  154. writer.close();
  155. }
  156. catch (IOException e) {
  157. throw new RuntimeException(e); // should never reach here.
  158. }
  159. set(output.toByteArray());
  160. }
  161. /**
  162. * Returns a string representation of this object.
  163. */
  164. @Override
  165. public String toString() {
  166. return getDefaultValue().toString();
  167. }
  168. }