@ SuppressWarnings ( "unchecked" ) // types must agree public static < T > T instantiateObject ( Class < T > rawType ) { try { Constructor < ? > constructor = rawType . getDeclaredConstructor ( ) ; if ( ! constructor . isAccessible ( ) ) { constructor . setAccessible ( true ) ; } return ( T ) constructor . newInstance ( ( Object [ ] ) null ) ; } catch ( Exception e ) { // Default constructor failed, attempt Unsafe Allocation Logger . w ( "Default constructor failed for " + rawType . getCanonicalName ( ) + "\nWith exception : " + e . getMessage ( ) + "\nAttempting unsafe allocation of object." ) ; } try { UnsafeAllocator unsafeAllocator = UnsafeAllocator . create ( ) ; return unsafeAllocator . newInstance ( rawType ) ; } catch ( Exception e ) { // Give up throw new RuntimeException ( "Failed to instantiate " + rawType . getCanonicalName ( ) , e ) ; } }
Creates a new instance of given raw type .