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

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