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.

EnumMemberValue.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * Enum constant value.
  21. *
  22. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  23. * @author Shigeru Chiba
  24. */
  25. public class EnumMemberValue extends MemberValue {
  26. int typeIndex, valueIndex;
  27. /**
  28. * Constructs an enum constant value. The initial value is specified
  29. * by the constant pool entries at the given indexes.
  30. *
  31. * @param type the index of a CONSTANT_Utf8_info structure
  32. * representing the enum type.
  33. * @param value the index of a CONSTANT_Utf8_info structure.
  34. * representing the enum value.
  35. */
  36. public EnumMemberValue(int type, int value, ConstPool cp) {
  37. super('e', cp);
  38. this.typeIndex = type;
  39. this.valueIndex = value;
  40. }
  41. /**
  42. * Constructs an enum constant value.
  43. * The initial value is not specified.
  44. */
  45. public EnumMemberValue(ConstPool cp) {
  46. super('e', cp);
  47. typeIndex = valueIndex = 0;
  48. }
  49. /**
  50. * Obtains the enum type name.
  51. *
  52. * @return a fully-qualified type name.
  53. */
  54. public String getType() {
  55. return Descriptor.toClassName(cp.getUtf8Info(typeIndex));
  56. }
  57. /**
  58. * Changes the enum type name.
  59. *
  60. * @param typename a fully-qualified type name.
  61. */
  62. public void setType(String typename) {
  63. typeIndex = cp.addUtf8Info(Descriptor.of(typename));
  64. }
  65. /**
  66. * Obtains the name of the enum constant value.
  67. */
  68. public String getValue() {
  69. return cp.getUtf8Info(valueIndex);
  70. }
  71. /**
  72. * Changes the name of the enum constant value.
  73. */
  74. public void setValue(String name) {
  75. valueIndex = cp.addUtf8Info(name);
  76. }
  77. public String toString() {
  78. return getType() + "." + getValue();
  79. }
  80. void write(AnnotationsWriter writer) throws IOException {
  81. writer.enumConstValue(getType(), getValue());
  82. }
  83. /**
  84. * Accepts a visitor.
  85. */
  86. public void accept(MemberValueVisitor visitor) {
  87. visitor.visitEnumMemberValue(this);
  88. }
  89. }