@ SuppressWarnings ( "unchecked" ) public static < T > T instantiateDefault ( Class < T > someClass ) { if ( someClass . isArray ( ) ) { return ( T ) Array . newInstance ( someClass . getComponentType ( ) , 0 ) ; } else { if ( boolean . class . equals ( someClass ) || Boolean . class . equals ( someClass ) ) { return ( T ) Boolean . FALSE ; } else if ( int . class . equals ( someClass ) || Integer . class . equals ( someClass ) ) { return ( T ) Integer . valueOf ( 0 ) ; } else if ( long . class . equals ( someClass ) || Long . class . equals ( someClass ) ) { return ( T ) Long . valueOf ( 0L ) ; } else if ( short . class . equals ( someClass ) || Short . class . equals ( someClass ) ) { return ( T ) Short . valueOf ( ( short ) 0 ) ; } else if ( float . class . equals ( someClass ) || Float . class . equals ( someClass ) ) { return ( T ) Float . valueOf ( 0f ) ; } else if ( double . class . equals ( someClass ) || Double . class . equals ( someClass ) ) { return ( T ) Double . valueOf ( 0d ) ; } else if ( byte . class . equals ( someClass ) || Byte . class . equals ( someClass ) ) { return ( T ) Byte . valueOf ( ( byte ) 0 ) ; } else if ( char . class . equals ( someClass ) || Character . class . equals ( someClass ) ) { return ( T ) Character . valueOf ( ( char ) 0 ) ; } else { try { Constructor < T > defaultConstructor = someClass . getDeclaredConstructor ( ) ; defaultConstructor . setAccessible ( true ) ; return defaultConstructor . newInstance ( ) ; } catch ( Exception e ) { throw ShedException . wrap ( e , ShedErrorCode . UNABLE_TO_INSTANTIATE_CLASS ) . put ( "class" , someClass ) ; } } } }
Instantiate a class by invoking its default constructor . If the specified class denotes an array an empty array of the correct component type is created . If the specified class denotes a primitive the primitive is created with its default value .