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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 javassist.ClassPool;
  18. import javassist.bytecode.BadBytecode;
  19. import javassist.bytecode.ConstPool;
  20. import javassist.bytecode.Descriptor;
  21. import javassist.bytecode.SignatureAttribute;
  22. import java.io.IOException;
  23. import java.lang.reflect.Method;
  24. /**
  25. * Class value.
  26. *
  27. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  28. * @author Shigeru Chiba
  29. */
  30. public class ClassMemberValue extends MemberValue {
  31. int valueIndex;
  32. /**
  33. * Constructs a class value. The initial value is specified
  34. * by the constant pool entry at the given index.
  35. *
  36. * @param index the index of a CONSTANT_Utf8_info structure.
  37. */
  38. public ClassMemberValue(int index, ConstPool cp) {
  39. super('c', cp);
  40. this.valueIndex = index;
  41. }
  42. /**
  43. * Constructs a class value.
  44. *
  45. * @param className the initial value.
  46. */
  47. public ClassMemberValue(String className, ConstPool cp) {
  48. super('c', cp);
  49. setValue(className);
  50. }
  51. /**
  52. * Constructs a class value.
  53. * The initial value is java.lang.Class.
  54. */
  55. public ClassMemberValue(ConstPool cp) {
  56. super('c', cp);
  57. setValue("java.lang.Class");
  58. }
  59. Object getValue(ClassLoader cl, ClassPool cp, Method m)
  60. throws ClassNotFoundException {
  61. final String classname = getValue();
  62. if (classname.equals("void"))
  63. return void.class;
  64. else if (classname.equals("int"))
  65. return int.class;
  66. else if (classname.equals("byte"))
  67. return byte.class;
  68. else if (classname.equals("long"))
  69. return long.class;
  70. else if (classname.equals("double"))
  71. return double.class;
  72. else if (classname.equals("float"))
  73. return float.class;
  74. else if (classname.equals("char"))
  75. return char.class;
  76. else if (classname.equals("short"))
  77. return short.class;
  78. else if (classname.equals("boolean"))
  79. return boolean.class;
  80. else
  81. return loadClass(cl, classname);
  82. }
  83. Class getType(ClassLoader cl) throws ClassNotFoundException {
  84. return loadClass(cl, "java.lang.Class");
  85. }
  86. /**
  87. * Obtains the value of the member.
  88. *
  89. * @return fully-qualified class name.
  90. */
  91. public String getValue() {
  92. String v = cp.getUtf8Info(valueIndex);
  93. try {
  94. return SignatureAttribute.toTypeSignature(v).toString();
  95. } catch (BadBytecode e) {
  96. throw new RuntimeException(e);
  97. }
  98. }
  99. /**
  100. * Sets the value of the member.
  101. *
  102. * @param newClassName fully-qualified class name.
  103. */
  104. public void setValue(String newClassName) {
  105. String setTo = Descriptor.of(newClassName);
  106. valueIndex = cp.addUtf8Info(setTo);
  107. }
  108. /**
  109. * Obtains the string representation of this object.
  110. */
  111. public String toString() {
  112. return getValue() + ".class";
  113. }
  114. /**
  115. * Writes the value.
  116. */
  117. public void write(AnnotationsWriter writer) throws IOException {
  118. writer.classInfoIndex(cp.getUtf8Info(valueIndex));
  119. }
  120. /**
  121. * Accepts a visitor.
  122. */
  123. public void accept(MemberValueVisitor visitor) {
  124. visitor.visitClassMemberValue(this);
  125. }
  126. }