spring-framework / org.springframework.beans / BeanUtils / instantiateClass

instantiateClass

open static fun <T : Any> instantiateClass(clazz: Class<T>): T

Instantiate a class using its no-arg constructor.

Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.

Parameters

clazz - class to instantiate

Exceptions

BeanInstantiationException - if the bean cannot be instantiated

Return
the new instance

See Also
Constructor#newInstance

open static fun <T : Any> instantiateClass(clazz: Class<*>, assignableTo: Class<T>): T

Instantiate a class using its no-arg constructor and return the new instance as the specified assignable type.

Useful in cases where the type of the class to instantiate (clazz) is not available, but the type desired (assignableTo) is known.

Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor.

Parameters

clazz - class to instantiate

assignableTo - type that clazz must be assignableTo

Exceptions

BeanInstantiationException - if the bean cannot be instantiated

Return
the new instance

See Also
Constructor#newInstance

open static fun <T : Any> instantiateClass(ctor: Constructor<T>, vararg args: Any): T

Convenience method to instantiate a class using the given constructor.

Note that this method tries to set the constructor accessible if given a non-accessible (that is, non-public) constructor, and supports Kotlin classes with optional parameters and default values.

Parameters

ctor - the constructor to instantiate

args - the constructor arguments to apply (use null for an unspecified parameter if needed for Kotlin classes with optional parameters and default values)

Exceptions

BeanInstantiationException - if the bean cannot be instantiated

Return
the new instance

See Also
Constructor#newInstance