Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AnnotationDefaultAttribute.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /**
  111. * Obtains the default value represented by this attribute.
  112. */
  113. public MemberValue getDefaultValue()
  114. {
  115. try {
  116. return new AnnotationsAttribute.Parser(info, constPool)
  117. .parseMemberValue();
  118. }
  119. catch (Exception e) {
  120. throw new RuntimeException(e.toString());
  121. }
  122. }
  123. /**
  124. * Changes the default value represented by this attribute.
  125. *
  126. * @param value the new value.
  127. * @see javassist.bytecode.annotation.Annotation#createMemberValue(ConstPool, CtClass)
  128. */
  129. public void setDefaultValue(MemberValue value) {
  130. ByteArrayOutputStream output = new ByteArrayOutputStream();
  131. AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
  132. try {
  133. value.write(writer);
  134. writer.close();
  135. }
  136. catch (IOException e) {
  137. throw new RuntimeException(e); // should never reach here.
  138. }
  139. set(output.toByteArray());
  140. }
  141. /**
  142. * Returns a string representation of this object.
  143. */
  144. @Override
  145. public String toString() {
  146. return getDefaultValue().toString();
  147. }
  148. }