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

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