1 15 16 package javassist.runtime; 17 18 24 public class Desc { 25 26 34 public static boolean useContextClassLoader = false; 35 36 private static Class getClassObject(String name) 37 throws ClassNotFoundException 38 { 39 if (useContextClassLoader) 40 return Thread.currentThread().getContextClassLoader() 41 .loadClass(name); 42 else 43 return Class.forName(name); 44 } 45 46 50 public static Class getClazz(String name) { 51 try { 52 return getClassObject(name); 53 } 54 catch (ClassNotFoundException e) { 55 throw new RuntimeException ("$class: internal error"); 56 } 57 } 58 59 63 public static Class [] getParams(String desc) { 64 if (desc.charAt(0) != '(') 65 throw new RuntimeException ("$sig: internal error"); 66 67 return getType(desc, desc.length(), 1, 0); 68 } 69 70 74 public static Class getType(String desc) { 75 Class [] result = getType(desc, desc.length(), 0, 0); 76 if (result == null || result.length != 1) 77 throw new RuntimeException ("$type: internal error"); 78 79 return result[0]; 80 } 81 82 private static Class [] getType(String desc, int descLen, 83 int start, int num) { 84 Class clazz; 85 if (start >= descLen) 86 return new Class [num]; 87 88 char c = desc.charAt(start); 89 switch (c) { 90 case 'Z' : 91 clazz = Boolean.TYPE; 92 break; 93 case 'C' : 94 clazz = Character.TYPE; 95 break; 96 case 'B' : 97 clazz = Byte.TYPE; 98 break; 99 case 'S' : 100 clazz = Short.TYPE; 101 break; 102 case 'I' : 103 clazz = Integer.TYPE; 104 break; 105 case 'J' : 106 clazz = Long.TYPE; 107 break; 108 case 'F' : 109 clazz = Float.TYPE; 110 break; 111 case 'D' : 112 clazz = Double.TYPE; 113 break; 114 case 'V' : 115 clazz = Void.TYPE; 116 break; 117 case 'L' : 118 case '[' : 119 return getClassType(desc, descLen, start, num); 120 default : 121 return new Class [num]; 122 } 123 124 Class [] result = getType(desc, descLen, start + 1, num + 1); 125 result[num] = clazz; 126 return result; 127 } 128 129 private static Class [] getClassType(String desc, int descLen, 130 int start, int num) { 131 int end = start; 132 while (desc.charAt(end) == '[') 133 ++end; 134 135 if (desc.charAt(end) == 'L') { 136 end = desc.indexOf(';', end); 137 if (end < 0) 138 throw new IndexOutOfBoundsException ("bad descriptor"); 139 } 140 141 String cname; 142 if (desc.charAt(start) == 'L') 143 cname = desc.substring(start + 1, end); 144 else 145 cname = desc.substring(start, end + 1); 146 147 Class [] result = getType(desc, descLen, end + 1, num + 1); 148 try { 149 result[num] = getClassObject(cname.replace('/', '.')); 150 } 151 catch (ClassNotFoundException e) { 152 throw new RuntimeException (e.getMessage()); 154 } 155 156 return result; 157 } 158 } 159 | Popular Tags |