1 package org.apache.ojb.broker.util; 2 3 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Constructor ; 22 import java.lang.reflect.Modifier ; 23 import java.net.URL ; 24 25 import org.apache.ojb.broker.OJBRuntimeException; 26 import org.apache.ojb.broker.PersistenceBrokerException; 27 import org.apache.ojb.broker.metadata.ClassDescriptor; 28 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; 29 30 35 public class ClassHelper 36 { 37 38 private static final Object [] NO_ARGS = {}; 39 40 private static final Class [] NO_ARGS_CLASS = {}; 41 42 43 private static ClassLoader _classLoader = null; 44 45 private static Object _mutex = new Object (); 46 47 50 private ClassHelper() 51 { 52 } 53 54 61 public static void setClassLoader(ClassLoader loader) 62 { 63 synchronized (_mutex) 64 { 65 _classLoader = loader; 66 } 67 } 68 69 78 public static ClassLoader getClassLoader() 79 { 80 final ClassLoader ojbClassLoader; 81 if (_classLoader != null) 82 { 83 ojbClassLoader = _classLoader; 84 } 85 else 86 { 87 final ClassLoader threadCtxtClassLoader; 88 threadCtxtClassLoader = Thread.currentThread().getContextClassLoader(); 89 if (threadCtxtClassLoader == null) 90 { 91 ojbClassLoader = ClassLoader.getSystemClassLoader(); 93 } 94 else 95 { 96 ojbClassLoader = threadCtxtClassLoader; 97 } 98 } 99 return ojbClassLoader; 100 } 101 102 108 public static URL getResource(String name) 109 { 110 return getClassLoader().getResource(name); 111 } 112 113 120 public static Class getClass(String className, boolean initialize) throws ClassNotFoundException 121 { 122 return Class.forName(className, initialize, getClassLoader()); 123 } 124 125 131 public static Object newInstance(Class target) throws InstantiationException , 132 IllegalAccessException 133 { 134 return target.newInstance(); 135 } 136 137 146 public static Object newInstance(Class target, boolean makeAccessible) throws InstantiationException , 147 IllegalAccessException 148 { 149 if (makeAccessible) 150 { 151 try 152 { 153 return newInstance(target, NO_ARGS_CLASS, NO_ARGS, makeAccessible); 154 } 155 catch (InvocationTargetException e) 156 { 157 throw new OJBRuntimeException("Unexpected exception while instantiate class '" 158 + target + "' with default constructor", e); 159 } 160 catch (NoSuchMethodException e) 161 { 162 throw new OJBRuntimeException("Unexpected exception while instantiate class '" 163 + target + "' with default constructor", e); 164 } 165 } 166 else 167 { 168 return target.newInstance(); 169 } 170 } 171 172 180 public static Object newInstance(Class target, Class [] types, Object [] args) throws InstantiationException , 181 IllegalAccessException , 182 IllegalArgumentException , 183 InvocationTargetException , 184 NoSuchMethodException , 185 SecurityException 186 { 187 return newInstance(target, types, args, false); 188 } 189 190 201 public static Object newInstance(Class target, Class [] types, Object [] args, boolean makeAccessible) throws InstantiationException , 202 IllegalAccessException , 203 IllegalArgumentException , 204 InvocationTargetException , 205 NoSuchMethodException , 206 SecurityException 207 { 208 Constructor con; 209 210 if (makeAccessible) 211 { 212 con = target.getDeclaredConstructor(types); 213 if (makeAccessible && !con.isAccessible()) 214 { 215 con.setAccessible(true); 216 } 217 } 218 else 219 { 220 con = target.getConstructor(types); 221 } 222 return con.newInstance(args); 223 } 224 225 233 public static Method getMethod(Class clazz, String methodName, Class [] params) 234 { 235 try 236 { 237 return clazz.getMethod(methodName, params); 238 } 239 catch (Exception ignored) 240 {} 241 return null; 242 } 243 244 251 public static Field getField(Class clazz, String fieldName) 252 { 253 try 254 { 255 return clazz.getField(fieldName); 256 } 257 catch (Exception ignored) 258 {} 259 return null; 260 } 261 262 263 267 273 public static Class getClass(String name) throws ClassNotFoundException 274 { 275 return getClass(name, true); 276 } 277 278 279 285 public static Object newInstance(String className) throws InstantiationException , 286 IllegalAccessException , 287 ClassNotFoundException 288 { 289 return newInstance(getClass(className)); 290 } 291 292 301 public static Object newInstance(String className, Class [] types, Object [] args) throws InstantiationException , 302 IllegalAccessException , 303 IllegalArgumentException , 304 InvocationTargetException , 305 NoSuchMethodException , 306 SecurityException , 307 ClassNotFoundException 308 { 309 return newInstance(getClass(className), types, args); 310 } 311 312 320 public static Object newInstance(Class target, Class type, Object arg) throws InstantiationException , 321 IllegalAccessException , 322 IllegalArgumentException , 323 InvocationTargetException , 324 NoSuchMethodException , 325 SecurityException 326 { 327 return newInstance(target, new Class []{ type }, new Object []{ arg }); 328 } 329 330 339 public static Object newInstance(String className, Class type, Object arg) throws InstantiationException , 340 IllegalAccessException , 341 IllegalArgumentException , 342 InvocationTargetException , 343 NoSuchMethodException , 344 SecurityException , 345 ClassNotFoundException 346 { 347 return newInstance(className, new Class []{type}, new Object []{arg}); 348 } 349 350 358 public static Method getMethod(Object object, String methodName, Class [] params) 359 { 360 return getMethod(object.getClass(), methodName, params); 361 } 362 363 371 public static Method getMethod(String className, String methodName, Class [] params) 372 { 373 try 374 { 375 return getMethod(getClass(className, false), methodName, params); 376 } 377 catch (Exception ignored) 378 {} 379 return null; 380 } 381 382 388 public static Object buildNewObjectInstance(ClassDescriptor cld) 389 { 390 Object result = null; 391 392 if ((cld.getFactoryClass() == null) || (cld.getFactoryMethod() == null)) 395 { 396 try 397 { 398 Constructor con = cld.getZeroArgumentConstructor(); 400 if(con == null) 401 { 402 throw new ClassNotPersistenceCapableException( 403 "A zero argument constructor was not provided! Class was '" + cld.getClassNameOfObject() + "'"); 404 } 405 result = ConstructorHelper.instantiate(con); 406 } 407 catch (InstantiationException e) 408 { 409 throw new ClassNotPersistenceCapableException( 410 "Can't instantiate class '" + cld.getClassNameOfObject()+"'"); 411 } 412 } 413 else 414 { 415 try 416 { 417 Method method = cld.getFactoryMethod(); 419 420 if (Modifier.isStatic(method.getModifiers())) 421 { 422 result = method.invoke(null, null); 424 } 425 else 426 { 427 Object factoryInstance = cld.getFactoryClass().newInstance(); 430 431 result = method.invoke(factoryInstance, null); 432 } 433 } 434 catch (Exception ex) 435 { 436 throw new PersistenceBrokerException("Unable to build object instance of class '" 437 + cld.getClassNameOfObject() + "' from factory:" + cld.getFactoryClass() 438 + "." + cld.getFactoryMethod(), ex); 439 } 440 } 441 return result; 442 } 443 } 444 | Popular Tags |