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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * 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.annotation;
  17. import java.io.IOException;
  18. import java.lang.reflect.Method;
  19. import javassist.ClassPool;
  20. import javassist.bytecode.ConstPool;
  21. import javassist.bytecode.Descriptor;
  22. /**
  23. * Enum constant value.
  24. *
  25. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  26. * @author Shigeru Chiba
  27. */
  28. public class EnumMemberValue extends MemberValue {
  29. int typeIndex, valueIndex;
  30. /**
  31. * Constructs an enum constant value. The initial value is specified
  32. * by the constant pool entries at the given indexes.
  33. *
  34. * @param type the index of a CONSTANT_Utf8_info structure
  35. * representing the enum type.
  36. * @param value the index of a CONSTANT_Utf8_info structure.
  37. * representing the enum value.
  38. */
  39. public EnumMemberValue(int type, int value, ConstPool cp) {
  40. super('e', cp);
  41. this.typeIndex = type;
  42. this.valueIndex = value;
  43. }
  44. /**
  45. * Constructs an enum constant value.
  46. * The initial value is not specified.
  47. */
  48. public EnumMemberValue(ConstPool cp) {
  49. super('e', cp);
  50. typeIndex = valueIndex = 0;
  51. }
  52. Object getValue(ClassLoader cl, ClassPool cp, Method m)
  53. throws ClassNotFoundException
  54. {
  55. try {
  56. return getType(cl).getField(getValue()).get(null);
  57. }
  58. catch (NoSuchFieldException e) {
  59. throw new ClassNotFoundException(getType() + "." + getValue());
  60. }
  61. catch (IllegalAccessException e) {
  62. throw new ClassNotFoundException(getType() + "." + getValue());
  63. }
  64. }
  65. Class getType(ClassLoader cl) throws ClassNotFoundException {
  66. return loadClass(cl, getType());
  67. }
  68. /**
  69. * Obtains the enum type name.
  70. *
  71. * @return a fully-qualified type name.
  72. */
  73. public String getType() {
  74. return Descriptor.toClassName(cp.getUtf8Info(typeIndex));
  75. }
  76. /**
  77. * Changes the enum type name.
  78. *
  79. * @param typename a fully-qualified type name.
  80. */
  81. public void setType(String typename) {
  82. typeIndex = cp.addUtf8Info(Descriptor.of(typename));
  83. }
  84. /**
  85. * Obtains the name of the enum constant value.
  86. */
  87. public String getValue() {
  88. return cp.getUtf8Info(valueIndex);
  89. }
  90. /**
  91. * Changes the name of the enum constant value.
  92. */
  93. public void setValue(String name) {
  94. valueIndex = cp.addUtf8Info(name);
  95. }
  96. public String toString() {
  97. return getType() + "." + getValue();
  98. }
  99. /**
  100. * Writes the value.
  101. */
  102. public void write(AnnotationsWriter writer) throws IOException {
  103. writer.enumConstValue(cp.getUtf8Info(typeIndex), getValue());
  104. }
  105. /**
  106. * Accepts a visitor.
  107. */
  108. public void accept(MemberValueVisitor visitor) {
  109. visitor.visitEnumMemberValue(this);
  110. }
  111. }