T - the type of object that the constructor createspublic class ConstructorInvoker<T>
extends java.lang.Object
Constructor object from the Java Reflection API and provides
methods to invoke the constructor without having to handle checked exceptions.
This class delegates methods to the underlying Constructor object,
but wraps checked exceptions in RuntimeException to avoid having to
handle them. All ReflectiveOperationException instances are caught
and rethrown as RuntimeException. Other runtime exceptions are not
caught and will be thrown as normal.
| Constructor and Description |
|---|
ConstructorInvoker(@NotNull java.lang.reflect.Constructor<T> constructor) |
| Modifier and Type | Method and Description |
|---|---|
@NotNull java.lang.reflect.Constructor<T> |
getConstructor()
Returns the underlying
Constructor object that this ConstructorInvoker wraps. |
T |
newInstance(java.lang.Object... initargs)
Uses the constructor represented by this
Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters. |
public ConstructorInvoker(@NotNull
@NotNull java.lang.reflect.Constructor<T> constructor)
@NotNull public @NotNull java.lang.reflect.Constructor<T> getConstructor()
Constructor object that this ConstructorInvoker wraps.Constructor object.@NotNull public T newInstance(java.lang.Object... initargs) throws java.lang.IllegalArgumentException
Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.
Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.
If the number of formal parameters required by the underlying constructor
is 0, the supplied initargs array may be of length 0 or null.
If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification.
If the required access and argument checks succeed and the instantiation will proceed, the constructor's declaring class is initialized if it has not already been initialized.
If the constructor completes normally, returns the newly created and initialized instance.
initargs - array of objects to be passed as arguments to the constructor call; values of primitive types are wrapped in a wrapper object of the appropriate type (e.g. a float in a
Float)WrappedReflectiveOperationException - if this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException - if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value
cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type.WrappedReflectiveOperationException - if the class that declares the underlying constructor represents an abstract class.WrappedReflectiveOperationException - if the underlying constructor throws an exception.java.lang.ExceptionInInitializerError - if the initialization provoked by this method fails.