1 20 package fr.dyade.aaa.util; 21 22 import java.lang.reflect.*; 23 24 public class TypeLoader { 25 26 34 public static Class loadType(String typeName, 35 ClassLoader classLoader) { 36 try { 37 return Class.forName(typeName); 38 } catch (ClassNotFoundException exc) { 39 try { 40 if (classLoader != null) { 41 return classLoader.loadClass(typeName); 42 } 43 } catch (ClassNotFoundException exc2) {} 44 } 45 46 if (typeName.equals("int")) { 52 return Integer .class; 53 } else if (typeName.equals("long")) { 54 return Long .class; 55 } else if (typeName.equals("boolean")) { 56 return Boolean .class; 57 } else if (typeName.equals("short")) { 58 return Short .class; 59 } else if (typeName.equals("double")) { 60 return Double .class; 61 } else if (typeName.equals("float")) { 62 return Float .class; 63 } else if (typeName.length() > 4 && 64 typeName.charAt(0) == '[' && 65 typeName.charAt(1) == 'L' && 66 typeName.charAt(typeName.length() - 1) == ';') { 67 String eltClassName = typeName.substring( 68 2, typeName.length() - 1); 69 Class eltClass = loadType(eltClassName, classLoader); 70 if (eltClass == null) return null; 71 Object arrayInstance = Array.newInstance(eltClass, 0); 73 return arrayInstance.getClass(); 74 } else return null; 75 } 76 } 77 | Popular Tags |