1 16 package org.apache.myfaces.util; 17 18 import org.apache.commons.el.Coercions; 19 import org.apache.commons.el.Logger; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.faces.FacesException; 24 import javax.servlet.jsp.el.ELException ; 25 import java.io.InputStream ; 26 import java.lang.reflect.Array ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 31 73 public class ClassUtils 74 { 75 77 private static final Log log = LogFactory.getLog(ClassUtils.class); 78 private static final Logger COERCION_LOGGER = new Logger(System.out); 79 80 public static final Class BOOLEAN_ARRAY_CLASS = boolean[].class; 81 public static final Class BYTE_ARRAY_CLASS = byte[].class; 82 public static final Class CHAR_ARRAY_CLASS = char[].class; 83 public static final Class SHORT_ARRAY_CLASS = short[].class; 84 public static final Class INT_ARRAY_CLASS = int[].class; 85 public static final Class LONG_ARRAY_CLASS = long[].class; 86 public static final Class FLOAT_ARRAY_CLASS = float[].class; 87 public static final Class DOUBLE_ARRAY_CLASS = double[].class; 88 public static final Class OBJECT_ARRAY_CLASS = Object [].class; 89 public static final Class BOOLEAN_OBJECT_ARRAY_CLASS = Boolean [].class; 90 public static final Class BYTE_OBJECT_ARRAY_CLASS = Byte [].class; 91 public static final Class CHARACTER_OBJECT_ARRAY_CLASS = Character [].class; 92 public static final Class SHORT_OBJECT_ARRAY_CLASS = Short [].class; 93 public static final Class INTEGER_OBJECT_ARRAY_CLASS = Integer [].class; 94 public static final Class LONG_OBJECT_ARRAY_CLASS = Long [].class; 95 public static final Class FLOAT_OBJECT_ARRAY_CLASS = Float [].class; 96 public static final Class DOUBLE_OBJECT_ARRAY_CLASS = Double [].class; 97 public static final Class STRING_OBJECT_ARRAY_CLASS = String [].class; 98 99 public static final Map COMMON_TYPES = new HashMap (64); 100 static 101 { 102 COMMON_TYPES.put("byte", Byte.TYPE); 103 COMMON_TYPES.put("char", Character.TYPE); 104 COMMON_TYPES.put("double", Double.TYPE); 105 COMMON_TYPES.put("float", Float.TYPE); 106 COMMON_TYPES.put("int", Integer.TYPE); 107 COMMON_TYPES.put("long", Long.TYPE); 108 COMMON_TYPES.put("short", Short.TYPE); 109 COMMON_TYPES.put("boolean", Boolean.TYPE); 110 COMMON_TYPES.put("void", Void.TYPE); 111 COMMON_TYPES.put("java.lang.Object", Object .class); 112 COMMON_TYPES.put("java.lang.Boolean", Boolean .class); 113 COMMON_TYPES.put("java.lang.Byte", Byte .class); 114 COMMON_TYPES.put("java.lang.Character", Character .class); 115 COMMON_TYPES.put("java.lang.Short", Short .class); 116 COMMON_TYPES.put("java.lang.Integer", Integer .class); 117 COMMON_TYPES.put("java.lang.Long", Long .class); 118 COMMON_TYPES.put("java.lang.Float", Float .class); 119 COMMON_TYPES.put("java.lang.Double", Double .class); 120 COMMON_TYPES.put("java.lang.String", String .class); 121 122 COMMON_TYPES.put("byte[]", BYTE_ARRAY_CLASS); 123 COMMON_TYPES.put("char[]", CHAR_ARRAY_CLASS); 124 COMMON_TYPES.put("double[]", DOUBLE_ARRAY_CLASS); 125 COMMON_TYPES.put("float[]", FLOAT_ARRAY_CLASS); 126 COMMON_TYPES.put("int[]", INT_ARRAY_CLASS); 127 COMMON_TYPES.put("long[]", LONG_ARRAY_CLASS); 128 COMMON_TYPES.put("short[]", SHORT_ARRAY_CLASS); 129 COMMON_TYPES.put("boolean[]", BOOLEAN_ARRAY_CLASS); 130 COMMON_TYPES.put("java.lang.Object[]", OBJECT_ARRAY_CLASS); 131 COMMON_TYPES.put("java.lang.Boolean[]", BOOLEAN_OBJECT_ARRAY_CLASS); 132 COMMON_TYPES.put("java.lang.Byte[]", BYTE_OBJECT_ARRAY_CLASS); 133 COMMON_TYPES.put("java.lang.Character[]", CHARACTER_OBJECT_ARRAY_CLASS); 134 COMMON_TYPES.put("java.lang.Short[]", SHORT_OBJECT_ARRAY_CLASS); 135 COMMON_TYPES.put("java.lang.Integer[]", INTEGER_OBJECT_ARRAY_CLASS); 136 COMMON_TYPES.put("java.lang.Long[]", LONG_OBJECT_ARRAY_CLASS); 137 COMMON_TYPES.put("java.lang.Float[]", FLOAT_OBJECT_ARRAY_CLASS); 138 COMMON_TYPES.put("java.lang.Double[]", DOUBLE_OBJECT_ARRAY_CLASS); 139 COMMON_TYPES.put("java.lang.String[]", STRING_OBJECT_ARRAY_CLASS); 140 } 142 143 144 private ClassUtils() 145 { 146 } 148 149 151 161 public static Class classForName(String type) 162 throws ClassNotFoundException 163 { 164 if (type == null) throw new NullPointerException ("type"); 165 try 166 { 167 return Class.forName(type, 169 false, Thread.currentThread().getContextClassLoader()); 171 } 172 catch (ClassNotFoundException ignore) 173 { 174 return Class.forName(type, 176 false, ClassUtils.class.getClassLoader()); 178 } 179 } 180 181 182 190 public static Class simpleClassForName(String type) 191 { 192 try 193 { 194 return classForName(type); 195 } 196 catch (ClassNotFoundException e) 197 { 198 log.error("Class " + type + " not found", e); 199 throw new FacesException(e); 200 } 201 } 202 203 204 214 public static Class javaTypeToClass(String type) 215 throws ClassNotFoundException 216 { 217 if (type == null) throw new NullPointerException ("type"); 218 219 Class clazz = (Class ) COMMON_TYPES.get(type); 221 if (clazz != null) 222 { 223 return clazz; 224 } 225 226 int len = type.length(); 227 if (len > 2 && type.charAt(len - 1) == ']' && type.charAt(len - 2) == '[') 228 { 229 String componentType = type.substring(0, len - 2); 230 Class componentTypeClass = classForName(componentType); 231 return Array.newInstance(componentTypeClass, 0).getClass(); 232 } 233 else 234 { 235 return classForName(type); 236 } 237 } 238 239 240 248 public static Class simpleJavaTypeToClass(String type) 249 { 250 try 251 { 252 return javaTypeToClass(type); 253 } 254 catch (ClassNotFoundException e) 255 { 256 log.error("Class " + type + " not found", e); 257 throw new FacesException(e); 258 } 259 } 260 261 public static InputStream getResourceAsStream(String resource) 262 { 263 InputStream stream = Thread.currentThread().getContextClassLoader() 264 .getResourceAsStream(resource); 265 if (stream == null) 266 { 267 stream = ClassUtils.class.getClassLoader().getResourceAsStream(resource); 269 } 270 return stream; 271 } 272 273 274 public static Object newInstance(String type) 275 throws FacesException 276 { 277 if (type == null) return null; 278 return newInstance(simpleClassForName(type)); 279 } 280 281 282 public static Object newInstance(Class clazz) 283 throws FacesException 284 { 285 try 286 { 287 return clazz.newInstance(); 288 } 289 catch(NoClassDefFoundError e) 290 { 291 log.error("Class : "+clazz.getName()+" not found.",e); 292 throw new FacesException(e); 293 } 294 catch (InstantiationException e) 295 { 296 log.error(e.getMessage(), e); 297 throw new FacesException(e); 298 } 299 catch (IllegalAccessException e) 300 { 301 log.error(e.getMessage(), e); 302 throw new FacesException(e); 303 } 304 } 305 306 public static Object convertToType(Object value, Class desiredClass) 307 { 308 if (value == null) return null; 309 310 try 311 { 312 return Coercions.coerce(value, desiredClass, COERCION_LOGGER); 315 } 316 catch (ELException e) 317 { 318 String message = "Cannot coerce " + value.getClass().getName() 319 + " to " + desiredClass.getName(); 320 log.error(message, e); 321 throw new FacesException(message, e); 322 } 323 } 324 } 325 | Popular Tags |