[New] Added ClassHelper.instantiateClass() with arguments

This commit is contained in:
Robert von Burg 2019-09-25 14:54:36 +02:00
parent d9aadc61f5
commit 4f41cd5677
1 changed files with 53 additions and 9 deletions

View File

@ -27,23 +27,45 @@ public class ClassHelper {
/** /**
* Returns an instance of the class' name given by instantiating the class through an empty arguments constructor * Returns an instance of the class' name given by instantiating the class through an empty arguments constructor
* *
* @param <T>
* the type of the class to return
* @param className * @param className
* the name of a class to instantiate through an empty arguments constructor * the name of a class to instantiate through an empty arguments constructor
* @param <T>
* the type of the class to return
* *
* @return the newly instantiated object from the given class name * @return the newly instantiated object from the given class name
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the class could not be instantiated * if the class could not be instantiated
*/ */
@SuppressWarnings("unchecked")
public static <T> T instantiateClass(String className) throws IllegalArgumentException { public static <T> 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 <T>
* the type of the class to return
*
* @return the newly instantiated object from the given class name
*
* @throws IllegalArgumentException
*/
public static <T> T instantiateClass(String className, Class<?>[] types, Object[] args)
throws IllegalArgumentException {
try { try {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) Class.forName(className); Class<T> clazz = (Class<T>) Class.forName(className);
return clazz.getConstructor().newInstance(); return instantiateClass(clazz, types, args);
} catch (Exception e) { } catch (Exception e) {
String msg = MessageFormat.format("The class {0} could not be instantiated: ", className); //$NON-NLS-1$ 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 * Instantiates an object for the given {@link Class} using an empty arguments constructor
* *
* @param <T>
* the type of the class to return
* @param clazz * @param clazz
* the {@link Class} from which a new object is to be instantiated using an empty arguments constructor * the {@link Class} from which a new object is to be instantiated using an empty arguments constructor
* @param <T>
* the type of the class to return
* *
* @return the newly instantiated object from the given {@link Class} * @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 * if the {@link Class} could not be instantiated
*/ */
public static <T> T instantiateClass(Class<T> clazz) throws IllegalArgumentException { public static <T> T instantiateClass(Class<T> 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 <T>
* 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> T instantiateClass(Class<T> clazz, Class<?>[] types, Object[] args)
throws IllegalArgumentException {
try { try {
return clazz.getConstructor().newInstance(); return clazz.getConstructor(types).newInstance(args);
} catch (Exception e) { } catch (Exception e) {
String msg = MessageFormat String msg = MessageFormat
@ -79,10 +123,10 @@ public class ClassHelper {
/** /**
* Loads the {@link Class} object for the given class name * Loads the {@link Class} object for the given class name
* *
* @param <T>
* the type of {@link Class} to return
* @param className * @param className
* the name of the {@link Class} to load and return * the name of the {@link Class} to load and return
* @param <T>
* the type of {@link Class} to return
* *
* @return the {@link Class} object for the given class name * @return the {@link Class} object for the given class name
* *