1 package org.apache.ojb.broker.util; 2 3 17 18 import java.lang.reflect.Constructor ; 19 20 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException; 21 22 32 public class ConstructorHelper 33 { 34 37 private static final Object [] NO_ARGS = {}; 38 39 42 private ConstructorHelper() 43 { 44 } 45 46 54 public static Object instantiate(Class clazz) throws InstantiationException 55 { 56 Object result = null; 57 try 58 { 59 result = ClassHelper.newInstance(clazz); 60 } 61 catch(IllegalAccessException e) 62 { 63 try 64 { 65 result = ClassHelper.newInstance(clazz, true); 66 } 67 catch(Exception e1) 68 { 69 throw new ClassNotPersistenceCapableException("Can't instantiate class '" 70 + (clazz != null ? clazz.getName() : "null") 71 + "', message was: " + e1.getMessage() + ")", e1); 72 } 73 } 74 return result; 75 } 76 77 85 public static Object instantiate(Constructor constructor) throws InstantiationException 86 { 87 if(constructor == null) 88 { 89 throw new ClassNotPersistenceCapableException( 90 "A zero argument constructor was not provided!"); 91 } 92 93 Object result = null; 94 try 95 { 96 result = constructor.newInstance(NO_ARGS); 97 } 98 catch(InstantiationException e) 99 { 100 throw e; 101 } 102 catch(Exception e) 103 { 104 throw new ClassNotPersistenceCapableException("Can't instantiate class '" 105 + (constructor != null ? constructor.getDeclaringClass().getName() : "null") 106 + "' with given constructor: " + e.getMessage(), e); 107 } 108 return result; 109 } 110 } 111 | Popular Tags |