Vararging constructor methods; much like the method ones were in MethodUtils. LANG-396

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@925970 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-03-22 06:22:28 +00:00
parent a22c0543d8
commit 1865f0b68d
1 changed files with 4 additions and 65 deletions

View File

@ -64,30 +64,6 @@ public ConstructorUtils() {
super();
}
/**
* <p>Convenience method returning new instance of <code>klazz</code> using a single argument constructor.
* The formal parameter type is inferred from the actual values of <code>arg</code>.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.</p>
*
* <p>The signatures should be assignment compatible.</p>
*
* @param cls the class to be constructed.
* @param arg the actual argument
* @return new instance of <code>klazz</code>
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static <T> T invokeConstructor(Class<T> cls, Object arg)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
return invokeConstructor(cls, new Object[] { arg });
}
/**
* <p>Returns new instance of <code>klazz</code> created using the actual arguments <code>args</code>.
* The formal parameter types are inferred from the actual values of <code>args</code>.
@ -106,7 +82,7 @@ public static <T> T invokeConstructor(Class<T> cls, Object arg)
*
* @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static <T> T invokeConstructor(Class<T> cls, Object[] args)
public static <T> T invokeConstructor(Class<T> cls, Object... args)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
if (null == args) {
@ -155,30 +131,6 @@ public static <T> T invokeConstructor(Class<T> cls, Object[] args,
return ctor.newInstance(args);
}
/**
* <p>Convenience method returning new instance of <code>klazz</code> using a single argument constructor.
* The formal parameter type is inferred from the actual values of <code>arg</code>.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.</p>
*
* <p>The signatures should match exactly.</p>
*
* @param cls the class to be constructed.
* @param arg the actual argument
* @return new instance of <code>klazz</code>
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static <T> T invokeExactConstructor(Class<T> cls, Object arg)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
return invokeExactConstructor(cls, new Object[] { arg });
}
/**
* <p>Returns new instance of <code>klazz</code> created using the actual arguments <code>args</code>.
* The formal parameter types are inferred from the actual values of <code>args</code>.
@ -197,7 +149,7 @@ public static <T> T invokeExactConstructor(Class<T> cls, Object arg)
*
* @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static <T> T invokeExactConstructor(Class<T> cls, Object[] args)
public static <T> T invokeExactConstructor(Class<T> cls, Object... args)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
if (null == args) {
@ -248,19 +200,6 @@ public static <T> T invokeExactConstructor(Class<T> cls, Object[] args,
return ctor.newInstance(args);
}
/**
* Returns a constructor with single argument.
* @param cls the class to be constructed
* @param parameterType The constructor parameter type
* @return null if matching accessible constructor can not be found.
* @see Class#getConstructor
* @see #getAccessibleConstructor(java.lang.reflect.Constructor)
*/
public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls,
Class<?> parameterType) {
return getAccessibleConstructor(cls, new Class[] { parameterType });
}
/**
* Returns a constructor given a class and signature.
* @param cls the class to be constructed
@ -270,7 +209,7 @@ public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls,
* @see #getAccessibleConstructor(java.lang.reflect.Constructor)
*/
public static <T> Constructor<T> getAccessibleConstructor(Class<T> cls,
Class<?>[] parameterTypes) {
Class<?>... parameterTypes) {
try {
return getAccessibleConstructor(cls.getConstructor(parameterTypes));
} catch (NoSuchMethodException e) {
@ -307,7 +246,7 @@ public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) {
*/
@SuppressWarnings("unchecked")
public static <T> Constructor<T> getMatchingAccessibleConstructor(Class<T> cls,
Class<?>[] parameterTypes) {
Class<?>... parameterTypes) {
// see if we can find the constructor directly
// most of the time this works and it's much faster
try {