public static <T> T instantiate(Class<T> clazz, Class<? super T> castTo) {
		if (clazz == null) {
			throw new NullPointerException();
		}

		// check if the class is a subclass, if the check is required
		if (castTo != null && !castTo.isAssignableFrom(clazz)) {
			throw new RuntimeException("The class '" + clazz.getName() + "' is not a subclass of '" +
				castTo.getName() + "' as is required.");
		}

		return instantiate(clazz);
	}
public static < T > T instantiate ( Class < T > clazz , Class < ? super T > castTo ) { if ( clazz == null ) { throw new NullPointerException ( ) ; } if ( castTo != null && ! castTo . isAssignableFrom ( clazz ) ) { throw new RuntimeException ( "The class '" + clazz . getName ( ) + "' is not a subclass of '" + castTo . getName ( ) + "' as is required." ) ; } return instantiate ( clazz ) ; }
Creates a new instance of the given class.

@param <T> The generic type of the class.
@param clazz The class to instantiate.
@param castTo Optional parameter, specifying the class that the given class must be a subclass off. This
argument is added to prevent class cast exceptions occurring later.
@return An instance of the given class.

@throws RuntimeException Thrown, if the class could not be instantiated. The exception contains a detailed
message about the reason why the instantiation failed.