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.

Desc.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. 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.runtime;
  17. /**
  18. * A support class for implementing <code>$sig</code> and
  19. * <code>$type</code>.
  20. * This support class is required at runtime
  21. * only if <code>$sig</code> or <code>$type</code> is used.
  22. */
  23. public class Desc {
  24. /**
  25. * Specifies how a <code>java.lang.Class</code> object is loaded.
  26. *
  27. * <p>If true, it is loaded by:
  28. * <pre>Thread.currentThread().getContextClassLoader().loadClass()</pre>
  29. * <p>If false, it is loaded by <code>Class.forName()</code>.
  30. * The default value is false.
  31. */
  32. public static boolean useContextClassLoader = false;
  33. private static Class getClassObject(String name)
  34. throws ClassNotFoundException
  35. {
  36. if (useContextClassLoader)
  37. return Class.forName(name, true, Thread.currentThread().getContextClassLoader());
  38. else
  39. return Class.forName(name);
  40. }
  41. /**
  42. * Interprets the given class name.
  43. * It is used for implementing <code>$class</code>.
  44. */
  45. public static Class getClazz(String name) {
  46. try {
  47. return getClassObject(name);
  48. }
  49. catch (ClassNotFoundException e) {
  50. throw new RuntimeException(
  51. "$class: internal error, could not find class '" + name
  52. + "' (Desc.useContextClassLoader: "
  53. + Boolean.toString(useContextClassLoader) + ")", e);
  54. }
  55. }
  56. /**
  57. * Interprets the given type descriptor representing a method
  58. * signature. It is used for implementing <code>$sig</code>.
  59. */
  60. public static Class[] getParams(String desc) {
  61. if (desc.charAt(0) != '(')
  62. throw new RuntimeException("$sig: internal error");
  63. return getType(desc, desc.length(), 1, 0);
  64. }
  65. /**
  66. * Interprets the given type descriptor.
  67. * It is used for implementing <code>$type</code>.
  68. */
  69. public static Class getType(String desc) {
  70. Class[] result = getType(desc, desc.length(), 0, 0);
  71. if (result == null || result.length != 1)
  72. throw new RuntimeException("$type: internal error");
  73. return result[0];
  74. }
  75. private static Class[] getType(String desc, int descLen,
  76. int start, int num) {
  77. Class clazz;
  78. if (start >= descLen)
  79. return new Class[num];
  80. char c = desc.charAt(start);
  81. switch (c) {
  82. case 'Z' :
  83. clazz = Boolean.TYPE;
  84. break;
  85. case 'C' :
  86. clazz = Character.TYPE;
  87. break;
  88. case 'B' :
  89. clazz = Byte.TYPE;
  90. break;
  91. case 'S' :
  92. clazz = Short.TYPE;
  93. break;
  94. case 'I' :
  95. clazz = Integer.TYPE;
  96. break;
  97. case 'J' :
  98. clazz = Long.TYPE;
  99. break;
  100. case 'F' :
  101. clazz = Float.TYPE;
  102. break;
  103. case 'D' :
  104. clazz = Double.TYPE;
  105. break;
  106. case 'V' :
  107. clazz = Void.TYPE;
  108. break;
  109. case 'L' :
  110. case '[' :
  111. return getClassType(desc, descLen, start, num);
  112. default :
  113. return new Class[num];
  114. }
  115. Class[] result = getType(desc, descLen, start + 1, num + 1);
  116. result[num] = clazz;
  117. return result;
  118. }
  119. private static Class[] getClassType(String desc, int descLen,
  120. int start, int num) {
  121. int end = start;
  122. while (desc.charAt(end) == '[')
  123. ++end;
  124. if (desc.charAt(end) == 'L') {
  125. end = desc.indexOf(';', end);
  126. if (end < 0)
  127. throw new IndexOutOfBoundsException("bad descriptor");
  128. }
  129. String cname;
  130. if (desc.charAt(start) == 'L')
  131. cname = desc.substring(start + 1, end);
  132. else
  133. cname = desc.substring(start, end + 1);
  134. Class[] result = getType(desc, descLen, end + 1, num + 1);
  135. try {
  136. result[num] = getClassObject(cname.replace('/', '.'));
  137. }
  138. catch (ClassNotFoundException e) {
  139. // "new RuntimeException(e)" is not available in JDK 1.3.
  140. throw new RuntimeException(e.getMessage());
  141. }
  142. return result;
  143. }
  144. }