@ SuppressWarnings ( "unchecked" ) public static < T > T newInstance ( Class < T > classToInstantiate ) { int modifiers = classToInstantiate . getModifiers ( ) ; final Object object ; if ( Modifier . isInterface ( modifiers ) ) { object = Proxy . newProxyInstance ( WhiteboxImpl . class . getClassLoader ( ) , new Class < ? > [ ] { classToInstantiate } , new InvocationHandler ( ) { @ Override public Object invoke ( Object proxy , Method method , Object [ ] args ) throws Throwable { return TypeUtils . getDefaultValue ( method . getReturnType ( ) ) ; } } ) ; } else if ( classToInstantiate . isArray ( ) ) { object = Array . newInstance ( classToInstantiate . getComponentType ( ) , 0 ) ; } else if ( Modifier . isAbstract ( modifiers ) ) { throw new IllegalArgumentException ( "Cannot instantiate an abstract class. Please use the ConcreteClassGenerator in PowerMock support to generate a concrete class first." ) ; } else { Objenesis objenesis = new ObjenesisStd ( ) ; ObjectInstantiator thingyInstantiator = objenesis . getInstantiatorOf ( classToInstantiate ) ; object = thingyInstantiator . newInstance ( ) ; } return ( T ) object ; }
Create a new instance of a class without invoking its constructor .