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.

ClassMemberValue.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 2004 Bill Burke. 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.annotation;
  16. import javassist.bytecode.ConstPool;
  17. import javassist.bytecode.Descriptor;
  18. import java.io.IOException;
  19. /**
  20. * String constant value.
  21. *
  22. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  23. * @author Shigeru Chiba
  24. */
  25. public class ClassMemberValue extends MemberValue {
  26. int valueIndex;
  27. /**
  28. * Constructs a string constant value. The initial value is specified
  29. * by the constant pool entry at the given index.
  30. *
  31. * @param index the index of a CONSTANT_Utf8_info structure.
  32. */
  33. public ClassMemberValue(int index, ConstPool cp) {
  34. super('c', cp);
  35. this.valueIndex = index;
  36. }
  37. /**
  38. * Constructs a string constant value.
  39. *
  40. * @param className the initial value.
  41. */
  42. public ClassMemberValue(String className, ConstPool cp) {
  43. super('c', cp);
  44. setValue(className);
  45. }
  46. /**
  47. * Constructs a string constant value.
  48. * The initial value is java.lang.Class.
  49. */
  50. public ClassMemberValue(ConstPool cp) {
  51. super('c', cp);
  52. setValue("java.lang.Class");
  53. }
  54. /**
  55. * Obtains the value of the member.
  56. *
  57. * @return fully-qualified class name.
  58. */
  59. public String getValue() {
  60. return Descriptor.toClassName(cp.getUtf8Info(valueIndex));
  61. }
  62. /**
  63. * Sets the value of the member.
  64. *
  65. * @param newClassName fully-qualified class name.
  66. */
  67. public void setValue(String newClassName) {
  68. valueIndex = cp.addUtf8Info(Descriptor.of(newClassName));
  69. }
  70. /**
  71. * Obtains the string representation of this object.
  72. */
  73. public String toString() {
  74. return "<" + getValue() + " class>";
  75. }
  76. void write(AnnotationsWriter writer) throws IOException {
  77. writer.constValueIndex(getValue());
  78. }
  79. /**
  80. * Accepts a visitor.
  81. */
  82. public void accept(MemberValueVisitor visitor) {
  83. visitor.visitClassMemberValue(this);
  84. }
  85. }