public class GobblinConstructorUtils extends Object
| Constructor and Description |
|---|
GobblinConstructorUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
invokeConstructor(Class<T> superType,
String clsName,
Object... args)
Utility method to create an instance of
clsName using the constructor matching the arguments, args |
static <T> T |
invokeFirstConstructor(Class<T> cls,
List<Object>... constructorArgs)
Convenience method on top of
ConstructorUtils.invokeConstructor(Class, Object[]) that returns a new
instance of the cls based on a constructor priority order. |
static <T> T |
invokeLongestConstructor(Class<T> cls,
Object... args)
Returns a new instance of the
cls based on a set of arguments. |
@SafeVarargs public static <T> T invokeFirstConstructor(Class<T> cls, List<Object>... constructorArgs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
ConstructorUtils.invokeConstructor(Class, Object[]) that returns a new
instance of the cls based on a constructor priority order. Each List in the
constructorArgs array contains the arguments for a constructor of cls. The first
constructor whose signature matches the argument types will be invoked.cls - the class to be instantiatedconstructorArgs - An array of constructor argument list. Order defines the priority of a constructor.NoSuchMethodException - if no constructor matched was foundIllegalAccessExceptionInvocationTargetExceptionInstantiationExceptionpublic static <T> T invokeLongestConstructor(Class<T> cls, Object... args) throws ReflectiveOperationException
cls based on a set of arguments. The method will search for a
constructor accepting the first k arguments in args for every k from args.length to 0, and will
invoke the first constructor found.
For example, invokeLongestConstructor(java.lang.Class<T>, java.lang.Object...)(cls, myString, myInt) will first attempt to create an object with
of class cls with constructor cls - the class to instantiate.args - the arguments to use for instantiation.ReflectiveOperationExceptionpublic static <T> T invokeConstructor(Class<T> superType, String clsName, Object... args)
clsName using the constructor matching the arguments, argssuperType - of clsName. The new instance is cast to superTypeclsName - complete cannonical name of the class to be instantiatedargs - constructor args to be usedclsNameIllegalArgumentException - if there was an issue creating the instance due to
NoSuchMethodException, InvocationTargetException,InstantiationException,
ClassNotFoundException