View Javadoc
1 package net.sf.cglib.core; 2 3 import java.util.*; 4 import org.objectweb.asm.Type; 5 6 public class TypeUtils { 7 private static final Map transforms = new HashMap(); 8 private static final Map rtransforms = new HashMap(); 9 10 private TypeUtils() { 11 } 12 13 static { 14 transforms.put("void", "V"); 15 transforms.put("byte", "B"); 16 transforms.put("char", "C"); 17 transforms.put("double", "D"); 18 transforms.put("float", "F"); 19 transforms.put("int", "I"); 20 transforms.put("long", "J"); 21 transforms.put("short", "S"); 22 transforms.put("boolean", "Z"); 23 24 CollectionUtils.reverse(transforms, rtransforms); 25 } 26 27 public static boolean isStatic(int access) { 28 return (Constants.ACC_STATIC & access) != 0; 29 } 30 31 public static boolean isAbstract(int access) { 32 return (Constants.ACC_ABSTRACT & access) != 0; 33 } 34 35 public static boolean isInterface(int access) { 36 return (Constants.ACC_INTERFACE & access) != 0; 37 } 38 39 public static boolean isPrivate(int access) { 40 return (Constants.ACC_PRIVATE & access) != 0; 41 } 42 43 44 // getPackage returns null on JDK 1.2 45 public static String getPackageName(Type type) { 46 String name = getClassName(type); 47 int idx = name.lastIndexOf('.'); 48 return (idx < 0) ? "" : name.substring(0, idx); 49 } 50 51 public static String upperFirst(String s) { 52 if (s == null || s.length() == 0) { 53 return s; 54 } 55 return Character.toUpperCase(s.charAt(0)) + s.substring(1); 56 } 57 58 public static String getClassName(Type type) { 59 if (isPrimitive(type)) { 60 return (String)rtransforms.get(type.getDescriptor()); 61 } else if (isArray(type)) { 62 return getClassName(getComponentType(type)) + "[]"; 63 } else { 64 return type.getClassName(); 65 } 66 } 67 68 public static Type[] add(Type[] types, Type extra) { 69 if (types == null) { 70 return new Type[]{ extra }; 71 } else { 72 List list = Arrays.asList(types); 73 if (list.contains(extra)) { 74 return types; 75 } 76 Type[] copy = new Type[types.length + 1]; 77 System.arraycopy(types, 0, copy, 0, types.length); 78 copy[types.length] = extra; 79 return copy; 80 } 81 } 82 83 public static Type[] add(Type[] t1, Type[] t2) { 84 // TODO: set semantics? 85 Type[] all = new Type[t1.length + t2.length]; 86 System.arraycopy(t1, 0, all, 0, t1.length); 87 System.arraycopy(t2, 0, all, t1.length, t2.length); 88 return all; 89 } 90 91 public static Type fromInternalName(String name) { 92 // TODO; primitives? 93 return Type.getType("L" + name + ";"); 94 } 95 96 public static Type[] fromInternalNames(String[] names) { 97 if (names == null) { 98 return null; 99 } 100 Type[] types = new Type[names.length]; 101 for (int i = 0; i < names.length; i++) { 102 types[i] = fromInternalName(names[i]); 103 } 104 return types; 105 } 106 107 public static int getStackSize(Type[] types) { 108 int size = 0; 109 for (int i = 0; i < types.length; i++) { 110 size += types[i].getSize(); 111 } 112 return size; 113 } 114 115 public static String[] toInternalNames(Type[] types) { 116 if (types == null) { 117 return null; 118 } 119 String[] names = new String[types.length]; 120 for (int i = 0; i < types.length; i++) { 121 names[i] = types[i].getInternalName(); 122 } 123 return names; 124 } 125 126 public static Signature parseSignature(String s) { 127 int space = s.indexOf(' '); 128 int lparen = s.indexOf('(', space); 129 int rparen = s.indexOf(')', lparen); 130 String returnType = s.substring(0, space); 131 String methodName = s.substring(space + 1, lparen); 132 StringBuffer sb = new StringBuffer(); 133 sb.append('('); 134 for (Iterator it = parseTypes(s, lparen + 1, rparen).iterator(); it.hasNext();) { 135 sb.append(it.next()); 136 } 137 sb.append(')'); 138 sb.append(map(returnType)); 139 return new Signature(methodName, sb.toString()); 140 } 141 142 public static Type parseType(String s) { 143 return Type.getType(map(s)); 144 } 145 146 public static Type[] parseTypes(String s) { 147 List names = parseTypes(s, 0, s.length()); 148 Type[] types = new Type[names.size()]; 149 for (int i = 0; i < types.length; i++) { 150 types[i] = Type.getType((String)names.get(i)); 151 } 152 return types; 153 } 154 155 public static Signature parseConstructor(Type[] types) { 156 StringBuffer sb = new StringBuffer(); 157 sb.append("("); 158 for (int i = 0; i < types.length; i++) { 159 sb.append(types[i].getDescriptor()); 160 } 161 sb.append(")"); 162 sb.append("V"); 163 return new Signature(Constants.CONSTRUCTOR_NAME, sb.toString()); 164 } 165 166 public static Signature parseConstructor(String sig) { 167 return parseSignature("void <init>(" + sig + ")"); // TODO 168 } 169 170 private static List parseTypes(String s, int mark, int end) { 171 List types = new ArrayList(5); 172 for (;;) { 173 int next = s.indexOf(',', mark); 174 if (next < 0) { 175 break; 176 } 177 types.add(map(s.substring(mark, next).trim())); 178 mark = next + 1; 179 } 180 types.add(map(s.substring(mark, end).trim())); 181 return types; 182 } 183 184 private static String map(String type) { 185 if (type.equals("")) { 186 return type; 187 } 188 String t = (String)transforms.get(type); 189 if (t != null) { 190 return t; 191 } else if (type.indexOf('.') < 0) { 192 return map("java.lang." + type); 193 } else { 194 StringBuffer sb = new StringBuffer(); 195 int index = 0; 196 while ((index = type.indexOf("[]", index) + 1) > 0) { 197 sb.append('['); 198 } 199 type = type.substring(0, type.length() - sb.length() * 2); 200 sb.append('L').append(type.replace('.', '/')).append(';'); 201 return sb.toString(); 202 } 203 } 204 205 public static Type getBoxedType(Type type) { 206 switch (type.getSort()) { 207 case Type.CHAR: 208 return Constants.TYPE_CHARACTER; 209 case Type.BOOLEAN: 210 return Constants.TYPE_BOOLEAN; 211 case Type.DOUBLE: 212 return Constants.TYPE_DOUBLE; 213 case Type.FLOAT: 214 return Constants.TYPE_FLOAT; 215 case Type.LONG: 216 return Constants.TYPE_LONG; 217 case Type.INT: 218 return Constants.TYPE_INTEGER; 219 case Type.SHORT: 220 return Constants.TYPE_SHORT; 221 case Type.BYTE: 222 return Constants.TYPE_BYTE; 223 default: 224 return type; 225 } 226 } 227 228 public static Type getUnboxedType(Type type) { 229 if (Constants.TYPE_INTEGER.equals(type)) { 230 return Type.INT_TYPE; 231 } else if (Constants.TYPE_BOOLEAN.equals(type)) { 232 return Type.BOOLEAN_TYPE; 233 } else if (Constants.TYPE_DOUBLE.equals(type)) { 234 return Type.DOUBLE_TYPE; 235 } else if (Constants.TYPE_LONG.equals(type)) { 236 return Type.LONG_TYPE; 237 } else if (Constants.TYPE_CHARACTER.equals(type)) { 238 return Type.CHAR_TYPE; 239 } else if (Constants.TYPE_BYTE.equals(type)) { 240 return Type.BYTE_TYPE; 241 } else if (Constants.TYPE_FLOAT.equals(type)) { 242 return Type.FLOAT_TYPE; 243 } else if (Constants.TYPE_SHORT.equals(type)) { 244 return Type.SHORT_TYPE; 245 } else { 246 return type; 247 } 248 } 249 250 public static boolean isArray(Type type) { 251 return type.getSort() == Type.ARRAY; 252 } 253 254 public static Type getComponentType(Type type) { 255 if (!isArray(type)) { 256 throw new IllegalArgumentException("Type " + type + " is not an array"); 257 } 258 return Type.getType(type.getDescriptor().substring(1)); 259 } 260 261 public static boolean isPrimitive(Type type) { 262 switch (type.getSort()) { 263 case Type.ARRAY: 264 case Type.OBJECT: 265 return false; 266 default: 267 return true; 268 } 269 } 270 271 public static String emulateClassGetName(Type type) { 272 if (isPrimitive(type) || isArray(type)) { 273 return type.getDescriptor().replace('/', '.'); 274 } else { 275 return type.getClassName(); 276 } 277 } 278 279 public static Type[] getTypes(Class[] classes) { 280 if (classes == null) { 281 return null; 282 } 283 Type[] types = new Type[classes.length]; 284 for (int i = 0; i < classes.length; i++) { 285 types[i] = Type.getType(classes[i]); 286 } 287 return types; 288 } 289 290 public static int ICONST(int value) { 291 switch (value) { 292 case -1: return Constants.ICONST_M1; 293 case 0: return Constants.ICONST_0; 294 case 1: return Constants.ICONST_1; 295 case 2: return Constants.ICONST_2; 296 case 3: return Constants.ICONST_3; 297 case 4: return Constants.ICONST_4; 298 case 5: return Constants.ICONST_5; 299 } 300 return -1; // error 301 } 302 303 public static int LCONST(long value) { 304 if (value == 0L) { 305 return Constants.LCONST_0; 306 } else if (value == 1L) { 307 return Constants.LCONST_1; 308 } else { 309 return -1; // error 310 } 311 } 312 313 public static int FCONST(float value) { 314 if (value == 0f) { 315 return Constants.FCONST_0; 316 } else if (value == 1f) { 317 return Constants.FCONST_1; 318 } else if (value == 2f) { 319 return Constants.FCONST_2; 320 } else { 321 return -1; // error 322 } 323 } 324 325 public static int DCONST(double value) { 326 if (value == 0d) { 327 return Constants.DCONST_0; 328 } else if (value == 1d) { 329 return Constants.DCONST_1; 330 } else { 331 return -1; // error 332 } 333 } 334 335 public static int NEWARRAY(Type type) { 336 switch (type.getSort()) { 337 case Type.BYTE: 338 return Constants.T_BYTE; 339 case Type.CHAR: 340 return Constants.T_CHAR; 341 case Type.DOUBLE: 342 return Constants.T_DOUBLE; 343 case Type.FLOAT: 344 return Constants.T_FLOAT; 345 case Type.INT: 346 return Constants.T_INT; 347 case Type.LONG: 348 return Constants.T_LONG; 349 case Type.SHORT: 350 return Constants.T_SHORT; 351 case Type.BOOLEAN: 352 return Constants.T_BOOLEAN; 353 default: 354 return -1; // error 355 } 356 } 357 }

This page was automatically generated by Maven