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.9KB

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