public class ConstructorUtils extends Object
MethodUtils.
There is an issue when invoking public constructors contained in a default access superclass. Reflection correctly locates these constructors and
assigns them as public. However, an IllegalAccessException is thrown if the constructor is invoked.
ConstructorUtils contains a workaround for this situation: it will attempt to call AccessibleObject.setAccessible(boolean)
on this constructor. If this call succeeds, then the method can be invoked as normal. This call will only succeed when the application has sufficient
security privileges. If this call fails then a warning will be logged and the method may fail.
| Constructor and Description |
|---|
ConstructorUtils()
Deprecated.
TODO Make private in 4.0.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> Constructor<T> |
getAccessibleConstructor(Class<T> cls,
Class<?>... parameterTypes)
Finds a constructor given a class and signature, checking accessibility.
|
static <T> Constructor<T> |
getAccessibleConstructor(Constructor<T> ctor)
Checks if the specified constructor is accessible.
|
static <T> Constructor<T> |
getMatchingAccessibleConstructor(Class<T> cls,
Class<?>... parameterTypes)
Finds an accessible constructor with compatible parameters.
|
static <T> T |
invokeConstructor(Class<T> cls,
Object... args)
Returns a new instance of the specified class inferring the right constructor from the types of the arguments.
|
static <T> T |
invokeConstructor(Class<T> cls,
Object[] args,
Class<?>[] parameterTypes)
Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
|
static <T> T |
invokeExactConstructor(Class<T> cls,
Object... args)
Returns a new instance of the specified class inferring the right constructor from the types of the arguments.
|
static <T> T |
invokeExactConstructor(Class<T> cls,
Object[] args,
Class<?>[] parameterTypes)
Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
|
@Deprecated public ConstructorUtils()
ConstructorUtils.invokeConstructor(cls, args).
This constructor is public to permit tools that require a JavaBean instance to operate.
public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes)
This finds the constructor and ensures that it is accessible. The constructor signature must match the parameter types exactly.
T - the constructor type.cls - the class to find a constructor for, not null.parameterTypes - the array of parameter types, null treated as empty.null if no matching accessible constructor found.NullPointerException - if cls is nullSecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class loader
for the class and invocation of SecurityManager.checkPackageAccess(String) denies access to the package of the
class.Class.getConstructor(java.lang.Class<?>...),
getAccessibleConstructor(java.lang.reflect.Constructor)public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor)
This simply ensures that the constructor is accessible.
T - the constructor type.ctor - the prototype constructor object, not null.null if no matching accessible constructor found.NullPointerException - if ctor is nullSecurityException - Thrown if a security manager is present and a caller's class loader is not the same as or an ancestor of the class loader
for a class and invocation of SecurityManager.checkPackageAccess(String) denies access to the package of the class.SecurityManagerpublic static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes)
This checks all the constructor and finds one with compatible parameters This requires that every parameter is assignable from the given parameter types. This is a more flexible search than the normal exact matching algorithm.
First it checks if there is a constructor matching the exact signature. If not then all the constructors of the class are checked to see if their signatures are assignment-compatible with the parameter types. The first assignment-compatible matching constructor is returned.
T - the constructor type.cls - the class to find a constructor for, not null.parameterTypes - find method with compatible parameters.NullPointerException - Thrown if cls is nullSecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class loader for the
class and invocation of SecurityManager.checkPackageAccess(String) denies access to the package of the class.SecurityManager.checkPackageAccess(String)public static <T> T invokeConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
This locates and calls a constructor. The constructor signature must match the argument types by assignment compatibility.
T - the type to be constructed.cls - the class to be constructed, not null.args - the array of arguments, null treated as empty.cls, not null.NullPointerException - Thrown if cls is null.NoSuchMethodException - Thrown if a matching constructor cannot be found.IllegalAccessException - Thrown if the found Constructor is enforcing Java language access control and the underlying constructor is
inaccessible.IllegalArgumentException - Thrown if:
InstantiationException - Thrown if the class that declares the underlying constructor represents an abstract class.InvocationTargetException - Thrown if the underlying constructor throws an exception.ExceptionInInitializerError - Thrown if the initialization provoked by this method fails.SecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class
loader for the class and invocation of SecurityManager.checkPackageAccess(String) denies access to the
package of the class.invokeConstructor(Class, Object[], Class[])public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
This locates and calls a constructor. The constructor signature must match the parameter types by assignment compatibility.
T - the type to be constructed.cls - the class to be constructed, not null.args - the array of arguments, null treated as empty.parameterTypes - the array of parameter types, null treated as empty.cls, not nullNullPointerException - Thrown if cls is null.NoSuchMethodException - Thrown if a matching constructor cannot be found.IllegalAccessException - Thrown if the found Constructor is enforcing Java language access control and the underlying constructor is
inaccessible.IllegalArgumentException - Thrown if:
InstantiationException - Thrown if the class that declares the underlying constructor represents an abstract class.InvocationTargetException - Thrown if the underlying constructor throws an exception.ExceptionInInitializerError - Thrown if the initialization provoked by this method fails.SecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class
loader for the class and invocation of SecurityManager.checkPackageAccess(String) denies access to the
package of the class.Constructor.newInstance(Object...),
Constructor.newInstance(java.lang.Object...)public static <T> T invokeExactConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
This locates and calls a constructor. The constructor signature must match the argument types exactly.
T - the type to be constructed.cls - the class to be constructed, not null.args - the array of arguments, null treated as empty.cls, not null.NullPointerException - Thrown if cls is null.NoSuchMethodException - Thrown if a matching constructor cannot be found.IllegalAccessException - Thrown if the found Constructor is enforcing Java language access control and the underlying constructor is
inaccessible.IllegalArgumentException - Thrown if:
InstantiationException - Thrown if the class that declares the underlying constructor represents an abstract class.InvocationTargetException - Thrown if the underlying constructor throws an exception.ExceptionInInitializerError - Thrown if the initialization provoked by this method fails.SecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class
loader for the class and invocation of SecurityManager.checkPackageAccess(String) denies access to the
package of the class.Constructor.newInstance(Object...),
invokeExactConstructor(Class, Object[], Class[])public static <T> T invokeExactConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
This locates and calls a constructor. The constructor signature must match the parameter types exactly.
T - the type to construct.cls - the class to construct, not null.args - the array of arguments, null treated as empty.parameterTypes - the array of parameter types, null treated as empty.cls, not null.NullPointerException - Thrown if cls is null.NoSuchMethodException - Thrown if a matching constructor cannot be found.IllegalAccessException - Thrown if the found Constructor is enforcing Java language access control and the underlying constructor is
inaccessible.IllegalArgumentException - Thrown if:
InstantiationException - Thrown if the class that declares the underlying constructor represents an abstract class.InvocationTargetException - Thrown if the underlying constructor throws an exception.ExceptionInInitializerError - Thrown if the initialization provoked by this method fails.SecurityException - Thrown if a security manager is present and the caller's class loader is not the same as or an ancestor of the class
loader for the class and invocation of SecurityManager.checkPackageAccess(String) denies access to the
package of the class.Constructor.newInstance(Object...)Copyright © 2001–2026 The Apache Software Foundation. All rights reserved.