From 1865f0b68d0af1c7d90b4b5bcb1aec91054c724b Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 22 Mar 2010 06:22:28 +0000 Subject: [PATCH] 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 --- .../lang3/reflect/ConstructorUtils.java | 69 ++----------------- 1 file changed, 4 insertions(+), 65 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java index c7a968d8c..a7792e924 100644 --- a/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java +++ b/src/main/java/org/apache/commons/lang3/reflect/ConstructorUtils.java @@ -64,30 +64,6 @@ public class ConstructorUtils { super(); } - /** - *

Convenience method returning new instance of klazz using a single argument constructor. - * The formal parameter type is inferred from the actual values of arg. - * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.

- * - *

The signatures should be assignment compatible.

- * - * @param cls the class to be constructed. - * @param arg the actual argument - * @return new instance of klazz - * - * @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 invokeConstructor(Class cls, Object arg) - throws NoSuchMethodException, IllegalAccessException, - InvocationTargetException, InstantiationException { - return invokeConstructor(cls, new Object[] { arg }); - } - /** *

Returns new instance of klazz created using the actual arguments args. * The formal parameter types are inferred from the actual values of args. @@ -106,7 +82,7 @@ public class ConstructorUtils { * * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ - public static T invokeConstructor(Class cls, Object[] args) + public static T invokeConstructor(Class cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (null == args) { @@ -155,30 +131,6 @@ public class ConstructorUtils { return ctor.newInstance(args); } - /** - *

Convenience method returning new instance of klazz using a single argument constructor. - * The formal parameter type is inferred from the actual values of arg. - * See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.

- * - *

The signatures should match exactly.

- * - * @param cls the class to be constructed. - * @param arg the actual argument - * @return new instance of klazz - * - * @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 invokeExactConstructor(Class cls, Object arg) - throws NoSuchMethodException, IllegalAccessException, - InvocationTargetException, InstantiationException { - return invokeExactConstructor(cls, new Object[] { arg }); - } - /** *

Returns new instance of klazz created using the actual arguments args. * The formal parameter types are inferred from the actual values of args. @@ -197,7 +149,7 @@ public class ConstructorUtils { * * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[]) */ - public static T invokeExactConstructor(Class cls, Object[] args) + public static T invokeExactConstructor(Class cls, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (null == args) { @@ -248,19 +200,6 @@ public class ConstructorUtils { 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 Constructor getAccessibleConstructor(Class 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 class ConstructorUtils { * @see #getAccessibleConstructor(java.lang.reflect.Constructor) */ public static Constructor getAccessibleConstructor(Class cls, - Class[] parameterTypes) { + Class... parameterTypes) { try { return getAccessibleConstructor(cls.getConstructor(parameterTypes)); } catch (NoSuchMethodException e) { @@ -307,7 +246,7 @@ public class ConstructorUtils { */ @SuppressWarnings("unchecked") public static Constructor getMatchingAccessibleConstructor(Class 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 {