T
- the class the instances of which will be created by this ConstructorInstantiatorpublic class ConstructorInstantiator<T> extends java.lang.Object implements Instantiator<T>
This class provides a convenient way of repeatable creation of instances of a given class without the need of repeatable constructor retrieval and checking all the exceptions.
Constructor and Description |
---|
ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
boolean convertStringArguments,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object[] arguments)
Creates a new instance of ConstructorInstantiator for creating instances of
objectClass that accepts the given arguments. |
ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
java.lang.Class<?>... prototype)
Creates a new instance of ConstructorInstantiator for creating instances of
objectClass that accepts parameters of the given prototype. |
ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
int argumentCount)
Creates a new instance of ConstructorInstantiator for creating instances of
objectClass that accepts the given number of arguments. |
ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
java.lang.Object[] arguments)
Creates a new instance of ConstructorInstantiator for creating instances of
objectClass that accepts the given arguments. |
ConstructorInstantiator(java.lang.reflect.Constructor<? extends T> constructor)
Creates a new instance of ConstructorInstantiator for creating instances of
objectClass via the specified constructor. |
Modifier and Type | Method and Description |
---|---|
static <T> T |
createInstanceWithStringArgs(java.lang.Class<? extends T> clazz,
java.lang.Object[] arguments,
int argStartIndex,
int argEndIndex,
java.util.Map<java.lang.String,java.lang.Object> namedInstances)
Creates a new instance of a class using string arguments for its constructor.
|
static <T> T |
createInstanceWithStringArgs(java.lang.reflect.Constructor<T>[] constructors,
java.lang.Object[] arguments,
int argStartIndex,
int argEndIndex,
java.util.Map<java.lang.String,java.lang.Object> namedInstances)
Creates a new instance of a class using string arguments for its constructor.
|
static <T> java.lang.reflect.Constructor<T> |
getConstructor(java.lang.Class<T> constructorClass,
boolean convertStringArguments,
boolean publicOnlyConstructors,
java.util.Map<java.lang.String,java.lang.Object> namedInstances,
java.util.List<java.lang.Object> arguments)
Returns a constructor for the specified class that accepts the specified arguments.
|
static <T> java.lang.reflect.Constructor<T> |
getConstructor(java.lang.Class<T> constructorClass,
boolean publicOnlyConstructors,
java.lang.Class<?>... prototype)
Retrieves a public constructor with the given prototype from the given class.
|
static <T> java.lang.reflect.Constructor<T> |
getConstructor(java.lang.Class<T> constructorClass,
boolean publicOnlyConstructors,
int argumentCount)
Retrieves a public constructor with the given number of arguments from the given class.
|
static <T> java.lang.reflect.Constructor<T> |
getConstructor(java.lang.reflect.Constructor<T>[] constructors,
int argumentCount)
Retrieves a public constructor with the given number of arguments from the given class.
|
java.lang.Class<? extends T> |
getInstantiatorClass()
Returns the class instantiated by this Instantiator.
|
java.lang.Class<?>[] |
getInstantiatorPrototype()
Returns the classes of arguments for the
Instantiator.instantiate(java.lang.Object[]) method. |
T |
instantiate(java.lang.Object... arguments)
Creates a new instance using the encapsulated constructor.
|
static <T> T |
instantiateByConstructor(java.lang.Class<? extends T> objectClass,
boolean convertStringArguments,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object[] arguments)
Creates a new instance using a constructor from the given class that matches the arguments.
|
java.lang.String |
toString() |
public ConstructorInstantiator(java.lang.reflect.Constructor<? extends T> constructor) throws NoSuchInstantiatorException
objectClass
via the specified constructor.constructor
- the constructor using which the instances will be createdNoSuchInstantiatorException
- if the provided class does not have a proper constructorpublic ConstructorInstantiator(java.lang.Class<? extends T> objectClass, java.lang.Class<?>... prototype) throws NoSuchInstantiatorException
objectClass
that accepts parameters of the given prototype.objectClass
- the class the instances of which will be createdprototype
- the types of constructor argumentsNoSuchInstantiatorException
- if the provided class does not have a proper constructorpublic ConstructorInstantiator(java.lang.Class<? extends T> objectClass, int argumentCount) throws NoSuchInstantiatorException
objectClass
that accepts the given number of arguments.
Note that if there are several constructors with the same number of arguments,
one is selected.objectClass
- the class the instances of which will be createdargumentCount
- the number of arguments that the constructor should haveNoSuchInstantiatorException
- if the provided class does not have a proper constructorpublic ConstructorInstantiator(java.lang.Class<? extends T> objectClass, boolean convertStringArguments, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object[] arguments) throws NoSuchInstantiatorException
objectClass
that accepts the given arguments.objectClass
- the class the instances of which will be createdconvertStringArguments
- if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the maparguments
- the arguments for the constructorNoSuchInstantiatorException
- if the provided class does not have a proper constructorpublic ConstructorInstantiator(java.lang.Class<? extends T> objectClass, java.lang.Object[] arguments) throws NoSuchInstantiatorException
objectClass
that accepts the given arguments.objectClass
- the class the instances of which will be createdarguments
- the arguments for the constructorNoSuchInstantiatorException
- if the provided class does not have a proper constructorpublic static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> constructorClass, boolean publicOnlyConstructors, java.lang.Class<?>... prototype) throws NoSuchInstantiatorException
T
- the class in which to search for the constructorconstructorClass
- the class in which to search for the constructorpublicOnlyConstructors
- flag whether to search in all declared constructors (false) or only in public constructors (true)prototype
- the constructor prototypeNoSuchInstantiatorException
- if the there is no constructor for the given prototypepublic static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.reflect.Constructor<T>[] constructors, int argumentCount) throws NoSuchInstantiatorException
T
- the class in which to search for the constructorconstructors
- the list of constructors to searchargumentCount
- the number of arguments that the method should haveNoSuchInstantiatorException
- if the there is no constructor for the given prototypepublic static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> constructorClass, boolean publicOnlyConstructors, int argumentCount) throws NoSuchInstantiatorException
T
- the class in which to search for the constructorconstructorClass
- the class in which to search for the constructorpublicOnlyConstructors
- flag whether to search in all declared constructors (false) or only in public constructors (true)argumentCount
- the number of arguments that the method should haveNoSuchInstantiatorException
- if the there is no constructor for the given prototypepublic static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> constructorClass, boolean convertStringArguments, boolean publicOnlyConstructors, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.util.List<java.lang.Object> arguments) throws NoSuchInstantiatorException
constructorClass
's declared constructors are searched for the one that
accepts the arguments.
If the convertStringArguments
is specified, the
arguments
elements are replaced with the converted types
if and only if a proper constructor is found. Their types then will be
compatible with the constructor.T
- the class in which to search for the constructorconstructorClass
- the class for which to get the constructorconvertStringArguments
- if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
publicOnlyConstructors
- flag whether to search in all declared constructors (false) or only in public constructors (true)namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the maparguments
- the arguments for the constructorNoSuchInstantiatorException
- if there was no constructor for the specified list of argumentspublic static <T> T createInstanceWithStringArgs(java.lang.reflect.Constructor<T>[] constructors, java.lang.Object[] arguments, int argStartIndex, int argEndIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances) throws NoSuchInstantiatorException, java.lang.reflect.InvocationTargetException
constructors
argument.
The constructors are tried one by one from this list and if the arguments
are convertible to the arguments of that constructor, a new instance is created.
Note that only constructors with the number of arguments equal to argEndIndex - argStartIndex + 1
are tried. If there are several constructors with the same number of arguments, the first (in the order
of the list) constructor that succeeds in converting string arguments will be used.
Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
method can be used in constructors.
T
- the type of the instantiated objectconstructors
- the list of constructors of the desired class to tryarguments
- the string arguments for the constructor that will be converted to correct typesargStartIndex
- index in the string arguments array from which to expect arguments (all the previous items are ignored)argEndIndex
- index in the string arguments array to which to expect arguments (all the following items are ignored)namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapNoSuchInstantiatorException
- if the constructor can't be found for the specified arguments or the argument string-to-type convertion has failedjava.lang.reflect.InvocationTargetException
- if there was an exception when calling the constructorpublic static <T> T createInstanceWithStringArgs(java.lang.Class<? extends T> clazz, java.lang.Object[] arguments, int argStartIndex, int argEndIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances) throws NoSuchInstantiatorException, java.lang.reflect.InvocationTargetException
clazz
are tried one by one and
if the arguments
are convertible to the arguments of that constructor,
a new instance is created.
Note that only constructors with the number of arguments equal to argEndIndex - argStartIndex + 1
are tried. If there are several constructors with the same number of arguments,
the first constructor that succeeds in converting string arguments will be used.
Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
method can be used in constructors.
T
- the type of the instantiated objectclazz
- the class of the instantiated objectarguments
- the string arguments for the constructor that will be converted to correct typesargStartIndex
- index in the string arguments array from which to expect arguments (all the previous items are ignored)argEndIndex
- index in the string arguments array to which to expect arguments (all the following items are ignored)namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the mapNoSuchInstantiatorException
- if the constructor can't be found for the specified arguments or the argument string-to-type convertion has failedjava.lang.reflect.InvocationTargetException
- if there was an exception when calling the constructorpublic static <T> T instantiateByConstructor(java.lang.Class<? extends T> objectClass, boolean convertStringArguments, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object[] arguments) throws NoSuchInstantiatorException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
objectClass
- the class the instances of which will be createdconvertStringArguments
- if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class<E>, java.util.Map<java.lang.String, ? extends java.lang.Object>)
namedInstances
- map of named instances - an instance from this map is returned if the string
matches a key in the maparguments
- the arguments for the constructorNoSuchInstantiatorException
- if the provided class does not have a proper constructorjava.lang.IllegalArgumentException
- if the arguments are not compatible with the constructor prototypejava.lang.reflect.InvocationTargetException
- if there was an exception thrown when the constructor was invokedpublic T instantiate(java.lang.Object... arguments) throws java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
creating
this
ConstructorInstantiator
class.instantiate
in interface Instantiator<T>
arguments
- the arguments for the encapsulated constructorjava.lang.IllegalArgumentException
- if the arguments are not compatible with the constructor prototypejava.lang.reflect.InvocationTargetException
- if there was an exception thrown when the constructor was invokedpublic java.lang.Class<?>[] getInstantiatorPrototype()
Instantiator
Instantiator.instantiate(java.lang.Object[])
method.getInstantiatorPrototype
in interface Instantiator<T>
public java.lang.Class<? extends T> getInstantiatorClass()
Instantiator
getInstantiatorClass
in interface Instantiator<T>
public java.lang.String toString()
toString
in class java.lang.Object