safeNewInstance

open fun <T> safeNewInstance(@NotNull constructor: Constructor<T>, args: Array<Any>): T

Safely creates a new instance of the class, unwrapping InvocationTargetException.

This method calls newInstance but throws the target exception directly instead of wrapping it in InvocationTargetException, providing cleaner exception handling for constructor calls.

example

Constructor<MyClass> constructor = MyClass.class.getConstructor(String.class);
try {
    MyClass instance = FastInvoke.safeNewInstance(constructor, new Object[]{"initial value"});
} catch (MyCustomException e) {
    // Handle the actual exception thrown by the constructor
}

Return

a new instance of the class; never null

Parameters

constructor

the constructor to use for instantiation; must not be null

args

the arguments to pass to the constructor; may be null if the constructor takes no arguments

<T>

the type of the instance to create

Throws

if the constructor throws an exception or if instantiation fails

if the class cannot be instantiated

if the constructor is not accessible

if the arguments are not appropriate for the constructor