ConstructorUtils
- public class org.apache.commons.lang3.reflect.ConstructorUtils[source]
Utility reflection methods focused on constructors, modeled after
MethodUtils.Known Limitations
Accessing Constructors In A Non-Public Class
Constructors in non-public classes (such as package-private classes or classes enclosed in non-public classes) are not accessible. Methods such as
#getAccessibleConstructor(Class, Class[])and#getMatchingAccessibleConstructor(Class, Class[])returnnullwhen invoked on non-public classes. Consequently, invocation methods such as#invokeConstructor(Class, Object...)and#invokeExactConstructor(Class, Object...)throw aNoSuchMethodException.- Since:
2.5
Constructors
Deprecated. ConstructorUtils instances should NOT be constructed in standard programming. |
- @Deprecated public ConstructorUtils()[source]
Deprecated: TODO Make private in 4.0.
ConstructorUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
ConstructorUtils.invokeConstructor(cls, args).This constructor is
publicto permit tools that require a JavaBean instance to operate.
Methods
Gets a constructor given a class and signature, checking accessibility. |
|
Gets the specified constructor if it is accessible; otherwise, returns null. |
|
Gets an accessible constructor with compatible parameters. |
|
Returns a new instance of the specified class inferring the right constructor from the types of the arguments. |
|
Returns a new instance of the specified class choosing the right constructor from the list of parameter types. |
|
Returns a new instance of the specified class inferring the right constructor from the types of the arguments. |
|
Returns a new instance of the specified class choosing the right constructor from the list of parameter types. |
- public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes)[source]
Gets a constructor given a class and signature, checking accessibility.
This finds the constructor and ensures that it is accessible. The constructor signature must match the parameter types exactly.
- Type parameters:
<T> – the constructor type.
- Parameters:
cls – The class to find a constructor for, not
null.parameterTypes – The array of parameter types,
nulltreated as empty.
- Returns:
The constructor,
nullif no matching accessible constructor found.- Throws:
java.lang.NullPointerException – Thrown if
clsisnull.java.lang.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.
- See also:
- public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor)[source]
Gets the specified constructor if it is accessible; otherwise, returns null.
This simply ensures that the constructor is accessible.
- Type parameters:
<T> – the constructor type.
- Parameters:
ctor – The prototype constructor object, not
null.- Returns:
The constructor,
nullif no matching accessible constructor found.- See also:
- Throws:
java.lang.NullPointerException – Thrown if
ctorisnull.java.lang.SecurityException – 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.
- public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls, Class<?>... parameterTypes)[source]
Gets an accessible constructor with compatible parameters.
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.
- Type parameters:
<T> – the constructor type.
- Parameters:
cls – The class to find a constructor for, not
null.parameterTypes – find method with compatible parameters.
- Returns:
The constructor, null if no matching accessible constructor found.
- Throws:
java.lang.NullPointerException – Thrown if
clsisnulljava.lang.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.
- See also:
- public static <T> T invokeConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException[source]
Returns a new instance of the specified class inferring the right constructor from the types of the arguments.
This locates and calls a constructor. The constructor signature must match the argument types by assignment compatibility.
- Type parameters:
<T> – the type to be constructed.
- Parameters:
cls – The class to be constructed, not
null.args – The array of arguments,
nulltreated as empty.
- Returns:
new instance of
cls, notnull.- Throws:
java.lang.NullPointerException – Thrown if
clsisnull.java.lang.NoSuchMethodException – Thrown if a matching constructor cannot be found.
java.lang.IllegalAccessException – Thrown if the found
Constructoris enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException – Thrown if:
the number of actual and formal parameters differ; or
an unwrapping conversion for primitive arguments fails; or
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.
java.lang.InstantiationException – Thrown if the class that declares the underlying constructor represents an abstract class.
java.lang.reflect.InvocationTargetException – Thrown if the underlying constructor throws an exception.
java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.
java.lang.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.
- See also:
- public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException[source]
Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
This locates and calls a constructor. The constructor signature must match the parameter types by assignment compatibility.
- Type parameters:
<T> – the type to be constructed.
- Parameters:
cls – The class to be constructed, not
null.args – The array of arguments,
nulltreated as empty.parameterTypes – The array of parameter types,
nulltreated as empty.
- Returns:
new instance of
cls, notnull- Throws:
java.lang.NullPointerException – Thrown if
clsisnull.java.lang.NoSuchMethodException – Thrown if a matching constructor cannot be found.
java.lang.IllegalAccessException – Thrown if the found
Constructoris enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException – Thrown if:
the number of actual and formal parameters differ; or
an unwrapping conversion for primitive arguments fails; or
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.
java.lang.InstantiationException – Thrown if the class that declares the underlying constructor represents an abstract class.
java.lang.reflect.InvocationTargetException – Thrown if the underlying constructor throws an exception.
java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.
java.lang.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.
- See also:
- public static <T> T invokeExactConstructor(Class<T> cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException[source]
Returns a new instance of the specified class inferring the right constructor from the types of the arguments.
This locates and calls a constructor. The constructor signature must match the argument types exactly.
- Type parameters:
<T> – the type to be constructed.
- Parameters:
cls – The class to be constructed, not
null.args – The array of arguments,
nulltreated as empty.
- Returns:
new instance of
cls, notnull.- Throws:
java.lang.NullPointerException – Thrown if
clsisnull.java.lang.NoSuchMethodException – Thrown if a matching constructor cannot be found.
java.lang.IllegalAccessException – Thrown if the found
Constructoris enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException – Thrown if:
the number of actual and formal parameters differ; or
an unwrapping conversion for primitive arguments fails; or
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.
java.lang.InstantiationException – Thrown if the class that declares the underlying constructor represents an abstract class.
java.lang.reflect.InvocationTargetException – Thrown if the underlying constructor throws an exception.
java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.
java.lang.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.
- See also:
- public static <T> T invokeExactConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException[source]
Returns a new instance of the specified class choosing the right constructor from the list of parameter types.
This locates and calls a constructor. The constructor signature must match the parameter types exactly.
- Type parameters:
<T> – the type to construct.
- Parameters:
cls – The class to construct, not
null.args – The array of arguments,
nulltreated as empty.parameterTypes – The array of parameter types,
nulltreated as empty.
- Returns:
new instance of
cls, notnull.- Throws:
java.lang.NullPointerException – Thrown if
clsisnull.java.lang.NoSuchMethodException – Thrown if a matching constructor cannot be found.
java.lang.IllegalAccessException – Thrown if the found
Constructoris enforcing Java language access control and the underlying constructor is inaccessible.java.lang.IllegalArgumentException – Thrown if:
the number of actual and formal parameters differ; or
an unwrapping conversion for primitive arguments fails; or
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.
java.lang.InstantiationException – Thrown if the class that declares the underlying constructor represents an abstract class.
java.lang.reflect.InvocationTargetException – Thrown if the underlying constructor throws an exception.
java.lang.ExceptionInInitializerError – Thrown if the initialization provoked by this method fails.
java.lang.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.
- See also: