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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 final ThreadLocal<Boolean> USE_CONTEXT_CLASS_LOADER_LOCALLY = new ThreadLocal<Boolean>() {
  34. @Override
  35. protected Boolean initialValue() {
  36. return false;
  37. }
  38. };
  39. public static void setUseContextClassLoaderLocally() {
  40. USE_CONTEXT_CLASS_LOADER_LOCALLY.set(true);
  41. }
  42. public static void resetUseContextClassLoaderLocally() {
  43. USE_CONTEXT_CLASS_LOADER_LOCALLY.remove();
  44. }
  45. private static Class<?> getClassObject(String name)
  46. throws ClassNotFoundException
  47. {
  48. if (useContextClassLoader || USE_CONTEXT_CLASS_LOADER_LOCALLY.get())
  49. return Class.forName(name, true, Thread.currentThread().getContextClassLoader());
  50. return Class.forName(name);
  51. }
  52. /**
  53. * Interprets the given class name.
  54. * It is used for implementing <code>$class</code>.
  55. */
  56. public static Class<?> getClazz(String name) {
  57. try {
  58. return getClassObject(name);
  59. }
  60. catch (ClassNotFoundException e) {
  61. throw new RuntimeException(
  62. "$class: internal error, could not find class '" + name
  63. + "' (Desc.useContextClassLoader: "
  64. + Boolean.toString(useContextClassLoader) + ")", e);
  65. }
  66. }
  67. /**
  68. * Interprets the given type descriptor representing a method
  69. * signature. It is used for implementing <code>$sig</code>.
  70. */
  71. public static Class<?>[] getParams(String desc) {
  72. if (desc.charAt(0) != '(')
  73. throw new RuntimeException("$sig: internal error");
  74. return getType(desc, desc.length(), 1, 0);
  75. }
  76. /**
  77. * Interprets the given type descriptor.
  78. * It is used for implementing <code>$type</code>.
  79. */
  80. public static Class<?> getType(String desc) {
  81. Class<?>[] result = getType(desc, desc.length(), 0, 0);
  82. if (result == null || result.length != 1)
  83. throw new RuntimeException("$type: internal error");
  84. return result[0];
  85. }
  86. private static Class<?>[] getType(String desc, int descLen,
  87. int start, int num) {
  88. Class<?> clazz;
  89. if (start >= descLen)
  90. return new Class[num];
  91. char c = desc.charAt(start);
  92. switch (c) {
  93. case 'Z' :
  94. clazz = Boolean.TYPE;
  95. break;
  96. case 'C' :
  97. clazz = Character.TYPE;
  98. break;
  99. case 'B' :
  100. clazz = Byte.TYPE;
  101. break;
  102. case 'S' :
  103. clazz = Short.TYPE;
  104. break;
  105. case 'I' :
  106. clazz = Integer.TYPE;
  107. break;
  108. case 'J' :
  109. clazz = Long.TYPE;
  110. break;
  111. case 'F' :
  112. clazz = Float.TYPE;
  113. break;
  114. case 'D' :
  115. clazz = Double.TYPE;
  116. break;
  117. case 'V' :
  118. clazz = Void.TYPE;
  119. break;
  120. case 'L' :
  121. case '[' :
  122. return getClassType(desc, descLen, start, num);
  123. default :
  124. return new Class[num];
  125. }
  126. Class<?>[] result = getType(desc, descLen, start + 1, num + 1);
  127. result[num] = clazz;
  128. return result;
  129. }
  130. private static Class<?>[] getClassType(String desc, int descLen,
  131. int start, int num) {
  132. int end = start;
  133. while (desc.charAt(end) == '[')
  134. ++end;
  135. if (desc.charAt(end) == 'L') {
  136. end = desc.indexOf(';', end);
  137. if (end < 0)
  138. throw new IndexOutOfBoundsException("bad descriptor");
  139. }
  140. String cname;
  141. if (desc.charAt(start) == 'L')
  142. cname = desc.substring(start + 1, end);
  143. else
  144. cname = desc.substring(start, end + 1);
  145. Class<?>[] result = getType(desc, descLen, end + 1, num + 1);
  146. try {
  147. result[num] = getClassObject(cname.replace('/', '.'));
  148. }
  149. catch (ClassNotFoundException e) {
  150. // "new RuntimeException(e)" is not available in JDK 1.3.
  151. throw new RuntimeException(e.getMessage());
  152. }
  153. return result;
  154. }
  155. }