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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 javassist.CtClass;
  18. import javassist.bytecode.annotation.AnnotationsWriter;
  19. import javassist.bytecode.annotation.MemberValue;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.DataInputStream;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. /**
  25. * A class representing <code>AnnotationDefault_attribute</code>.
  26. *
  27. * <p>For example, if you declare the following annotation type:
  28. *
  29. * <ul><pre>
  30. * &#64;interface Author {
  31. * String name() default "Shakespeare";
  32. * int age() default 99;
  33. * }
  34. * </pre></ul>
  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. * <ul><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></ul>
  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. * <ul><pre>
  55. * ada.setDefaultValue(new IntegerMemberValue(minfo.getConstPool(), 80));
  56. * </pre></ul>
  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. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  99. AnnotationsAttribute.Copier copier
  100. = new AnnotationsAttribute.Copier(info, constPool, newCp, classnames);
  101. try {
  102. copier.memberValue(0);
  103. return new AnnotationDefaultAttribute(newCp, copier.close());
  104. }
  105. catch (Exception e) {
  106. throw new RuntimeException(e.toString());
  107. }
  108. }
  109. /**
  110. * Obtains the default value represented by this attribute.
  111. */
  112. public MemberValue getDefaultValue()
  113. {
  114. try {
  115. return new AnnotationsAttribute.Parser(info, constPool)
  116. .parseMemberValue();
  117. }
  118. catch (Exception e) {
  119. throw new RuntimeException(e.toString());
  120. }
  121. }
  122. /**
  123. * Changes the default value represented by this attribute.
  124. *
  125. * @param value the new value.
  126. * @see javassist.bytecode.annotation.Annotation#createMemberValue(ConstPool, CtClass)
  127. */
  128. public void setDefaultValue(MemberValue value) {
  129. ByteArrayOutputStream output = new ByteArrayOutputStream();
  130. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  131. try {
  132. value.write(writer);
  133. writer.close();
  134. }
  135. catch (IOException e) {
  136. throw new RuntimeException(e); // should never reach here.
  137. }
  138. set(output.toByteArray());
  139. }
  140. /**
  141. * Returns a string representation of this object.
  142. */
  143. public String toString() {
  144. return getDefaultValue().toString();
  145. }
  146. }