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.

MemberValue.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.ConstPool;
  19. import javassist.bytecode.Descriptor;
  20. import java.io.IOException;
  21. import java.lang.reflect.Array;
  22. import java.lang.reflect.Method;
  23. /**
  24. * The value of a member declared in an annotation.
  25. *
  26. * @see Annotation#getMemberValue(String)
  27. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  28. * @author Shigeru Chiba
  29. */
  30. public abstract class MemberValue {
  31. ConstPool cp;
  32. char tag;
  33. MemberValue(char tag, ConstPool cp) {
  34. this.cp = cp;
  35. this.tag = tag;
  36. }
  37. /**
  38. * Returns the value. If the value type is a primitive type, the
  39. * returned value is boxed.
  40. */
  41. abstract Object getValue(ClassLoader cl, ClassPool cp, Method m)
  42. throws ClassNotFoundException;
  43. abstract Class getType(ClassLoader cl) throws ClassNotFoundException;
  44. static Class loadClass(ClassLoader cl, String classname)
  45. throws ClassNotFoundException, NoSuchClassError
  46. {
  47. try {
  48. return Class.forName(convertFromArray(classname), true, cl);
  49. }
  50. catch (LinkageError e) {
  51. throw new NoSuchClassError(classname, e);
  52. }
  53. }
  54. private static String convertFromArray(String classname)
  55. {
  56. int index = classname.indexOf("[]");
  57. if (index != -1) {
  58. String rawType = classname.substring(0, index);
  59. StringBuffer sb = new StringBuffer(Descriptor.of(rawType));
  60. while (index != -1) {
  61. sb.insert(0, "[");
  62. index = classname.indexOf("[]", index + 1);
  63. }
  64. return sb.toString().replace('/', '.');
  65. }
  66. return classname;
  67. }
  68. /**
  69. * Accepts a visitor.
  70. */
  71. public abstract void accept(MemberValueVisitor visitor);
  72. /**
  73. * Writes the value.
  74. */
  75. public abstract void write(AnnotationsWriter w) throws IOException;
  76. }