From 4f41cd567796a10c20e345687b53a6c58a1d3cef Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 25 Sep 2019 14:54:36 +0200 Subject: [PATCH] [New] Added ClassHelper.instantiateClass() with arguments --- .../li/strolch/utils/helper/ClassHelper.java | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ClassHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ClassHelper.java index 731970d94..be9791209 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ClassHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ClassHelper.java @@ -27,23 +27,45 @@ public class ClassHelper { /** * Returns an instance of the class' name given by instantiating the class through an empty arguments constructor * - * @param - * the type of the class to return * @param className * the name of a class to instantiate through an empty arguments constructor + * @param + * the type of the class to return * * @return the newly instantiated object from the given class name * * @throws IllegalArgumentException * if the class could not be instantiated */ - @SuppressWarnings("unchecked") public static T instantiateClass(String className) throws IllegalArgumentException { + return instantiateClass(className, new Class[0], new Object[0]); + } + + /** + * Returns an instance of the class' name given by instantiating the constructor using the given types and + * arguments + * + * @param className + * the name of a class to instantiate through an empty arguments constructor + * @param types + * the constructor types + * @param args + * the constructor arguments + * @param + * the type of the class to return + * + * @return the newly instantiated object from the given class name + * + * @throws IllegalArgumentException + */ + public static T instantiateClass(String className, Class[] types, Object[] args) + throws IllegalArgumentException { try { + @SuppressWarnings("unchecked") Class clazz = (Class) Class.forName(className); - return clazz.getConstructor().newInstance(); + return instantiateClass(clazz, types, args); } catch (Exception e) { String msg = MessageFormat.format("The class {0} could not be instantiated: ", className); //$NON-NLS-1$ @@ -54,10 +76,10 @@ public class ClassHelper { /** * Instantiates an object for the given {@link Class} using an empty arguments constructor * - * @param - * the type of the class to return * @param clazz * the {@link Class} from which a new object is to be instantiated using an empty arguments constructor + * @param + * the type of the class to return * * @return the newly instantiated object from the given {@link Class} * @@ -65,9 +87,31 @@ public class ClassHelper { * if the {@link Class} could not be instantiated */ public static T instantiateClass(Class clazz) throws IllegalArgumentException { + return instantiateClass(clazz, new Class[0], new Object[0]); + } + + /** + * Instantiates an object for the given {@link Class} using the given constructor arguments + * + * @param clazz + * the {@link Class} from which a new object is to be instantiated using an empty arguments constructor + * @param types + * the constructor types + * @param args + * the constructor arguments + * @param + * the type of the class to return + * + * @return the newly instantiated object from the given {@link Class} + * + * @throws IllegalArgumentException + * if the {@link Class} could not be instantiated + */ + public static T instantiateClass(Class clazz, Class[] types, Object[] args) + throws IllegalArgumentException { try { - return clazz.getConstructor().newInstance(); + return clazz.getConstructor(types).newInstance(args); } catch (Exception e) { String msg = MessageFormat @@ -79,10 +123,10 @@ public class ClassHelper { /** * Loads the {@link Class} object for the given class name * - * @param - * the type of {@link Class} to return * @param className * the name of the {@link Class} to load and return + * @param + * the type of {@link Class} to return * * @return the {@link Class} object for the given class name *