public static Object instantiate ( Class expected , String property , String defaultClassName ) { String className = getProperty ( property , defaultClassName ) ; Class clazz = null ; try { clazz = loadClass ( className ) ; } catch ( ClassNotFoundException e ) { throw new NestedRuntimeException ( "Cannot load class " + className , e ) ; } Object result = null ; try { result = clazz . newInstance ( ) ; } catch ( InstantiationException e ) { throw new NestedRuntimeException ( "Error instantiating " + className , e ) ; } catch ( IllegalAccessException e ) { throw new NestedRuntimeException ( "Error instantiating " + className , e ) ; } if ( expected . isAssignableFrom ( clazz ) == false ) throw new NestedRuntimeException ( "Class " + className + " from classloader " + clazz . getClassLoader ( ) + " is not of the expected class " + expected + " loaded from " + expected . getClassLoader ( ) ) ; return result ; }
Instantiate a java class object