T
- the class the instances of which will be created by this MethodInstantiatorpublic class MethodInstantiator<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 repetable factory method retrieval and checking all the exceptions.
Constructor and Description |
---|
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Class<?> methodClass,
java.lang.String methodName,
boolean convertStringArguments,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object[] arguments)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a method with the specified name
and the number of parameters on the given callInstance . |
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Class<?> methodClass,
java.lang.String methodName,
java.lang.Class<?>... prototype)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a factory method with the specified name and prototype. |
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Class<?> methodClass,
java.lang.String methodName,
int argumentCount)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a factory method with the specified name
and the number of parameters. |
MethodInstantiator(java.lang.Class<? extends T> objectClass,
java.lang.reflect.Method method,
java.lang.Object callInstance)
Creates a new instance of MethodInstantiator for creating instances of
objectClass via the given method. |
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Object callInstance,
java.lang.String methodName,
boolean convertStringArguments,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object[] arguments)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a method with the specified name
and the number of parameters on the given callInstance . |
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Object callInstance,
java.lang.String methodName,
java.lang.Class<?>... prototype)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a method with the specified name and prototype
on the given callInstance . |
MethodInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Object callInstance,
java.lang.String methodName,
int argumentCount)
Creates a new instance of MethodInstantiator for creating instances of
objectClass by calling a method with the specified name
and the number of parameters on the given callInstance . |
Modifier and Type | Method and Description |
---|---|
static <T> T |
callFactoryMethod(java.lang.Class<? extends T> factoryClass,
java.lang.String name,
boolean convertStringArguments,
boolean publicOnlyMethods,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object... arguments)
Calls a method with the given name and prototype from the given method class.
|
static java.lang.Object |
callMethod(java.lang.Object methodInstanceOrClass,
java.lang.String name,
boolean convertStringArguments,
boolean publicOnlyMethods,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object... arguments)
Calls a method with the given name and prototype from the given method 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. |
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> methodClass,
java.lang.String name,
boolean convertStringArguments,
boolean publicOnlyMethods,
java.util.Map<java.lang.String,?> namedInstances,
java.lang.Object[] arguments)
Retrieves a method with the given name and prototype from the given method class.
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> methodClass,
java.lang.String name,
boolean publicOnlyMethods,
java.lang.Class<?>... prototype)
Retrieves a method with the given name and prototype from the given method class.
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> methodClass,
java.lang.String name,
boolean publicOnlyMethods,
int argumentCount)
Retrieves a method with the given name and argument count from the given method class.
|
T |
instantiate(java.lang.Object... arguments)
Creates a new instance using the encapsulated method.
|
java.lang.String |
toString() |
public MethodInstantiator(java.lang.Class<? extends T> objectClass, java.lang.reflect.Method method, java.lang.Object callInstance) throws NoSuchInstantiatorException
objectClass
via the given method.objectClass
- the class the instances of which will be createdmethod
- the factory method used to create instancescallInstance
- the instance on which the method is called or null if the method is staticNoSuchInstantiatorException
- if the provided method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> methodClass, java.lang.String methodName, java.lang.Class<?>... prototype) throws NoSuchInstantiatorException
objectClass
by calling a factory method with the specified name and prototype.checkClass
- the class the instances of which will be createdmethodClass
- the class from which the method is takenmethodName
- the name of the factory method within the objectClass
prototype
- the types of constructor argumentsNoSuchInstantiatorException
- if the there is no method for the given name and prototype or
if such method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Object callInstance, java.lang.String methodName, java.lang.Class<?>... prototype) throws NoSuchInstantiatorException
objectClass
by calling a method with the specified name and prototype
on the given callInstance
.checkClass
- the class the instances of which will be createdcallInstance
- the instance on which the method is calledmethodName
- the name of the factory method within the objectClass
prototype
- the types of constructor argumentsNoSuchInstantiatorException
- if the there is no method for the given name and prototype or
if such method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> methodClass, java.lang.String methodName, boolean convertStringArguments, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object[] arguments) throws NoSuchInstantiatorException
objectClass
by calling a method with the specified name
and the number of parameters on the given callInstance
.
Note that if there are several methods with the same name and number of
arguments, the first one inspected is selected.checkClass
- the class the instances of which will be createdmethodClass
- the class from which the method is takenmethodName
- the name of the factory method within the objectClass
convertStringArguments
- 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 methodNoSuchInstantiatorException
- if the there is no method for the given name and number of arguments or
if such method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Object callInstance, java.lang.String methodName, boolean convertStringArguments, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object[] arguments) throws NoSuchInstantiatorException
objectClass
by calling a method with the specified name
and the number of parameters on the given callInstance
.
Note that if there are several methods with the same name and number of
arguments, the first one inspected is selected.checkClass
- the class the instances of which will be createdcallInstance
- the instance on which the method is calledmethodName
- the name of the factory method within the objectClass
convertStringArguments
- 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 methodNoSuchInstantiatorException
- if the there is no method for the given name and number of arguments or
if such method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> methodClass, java.lang.String methodName, int argumentCount) throws NoSuchInstantiatorException
objectClass
by calling a factory method with the specified name
and the number of parameters. Note that if there are several methods with
the same name and number of arguments, the first one inspected is selected.checkClass
- the class the instances of which will be createdmethodClass
- the class from which the method is takenmethodName
- the name of the factory method within the objectClass
argumentCount
- the number of arguments that the method should haveNoSuchInstantiatorException
- if the there is no method for the given name and number of arguments or
if such method is not static or does not return the given objectClasspublic MethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Object callInstance, java.lang.String methodName, int argumentCount) throws NoSuchInstantiatorException
objectClass
by calling a method with the specified name
and the number of parameters on the given callInstance
.
Note that if there are several methods with the same name and number of
arguments, the first one inspected is selected.checkClass
- the class the instances of which will be createdcallInstance
- the instance on which the method is calledmethodName
- the name of the factory method within the objectClass
argumentCount
- the number of arguments that the method should haveNoSuchInstantiatorException
- if the there is no method for the given name and number of arguments or
if such method is not static or does not return the given objectClasspublic static java.lang.reflect.Method getMethod(java.lang.Class<?> methodClass, java.lang.String name, boolean publicOnlyMethods, java.lang.Class<?>... prototype) throws NoSuchInstantiatorException
methodClass
- the class in which to search for the methodname
- the name of the methodpublicOnlyMethods
- flag wheter to search for all declared methods (false) or only for the public ones (true)prototype
- the method prototypeNoSuchInstantiatorException
- if the there is no method for the given name and prototypepublic static java.lang.reflect.Method getMethod(java.lang.Class<?> methodClass, java.lang.String name, boolean publicOnlyMethods, int argumentCount) throws NoSuchInstantiatorException
methodClass
- the class in which to search for the methodname
- the name of the methodpublicOnlyMethods
- flag wheter to search for all declared methods (false) or only for the public ones (true)argumentCount
- the number of arguments that the method should haveNoSuchInstantiatorException
- if the there is no method for the given name and number of argumentspublic static java.lang.reflect.Method getMethod(java.lang.Class<?> methodClass, java.lang.String name, boolean convertStringArguments, boolean publicOnlyMethods, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object[] arguments) throws NoSuchInstantiatorException
methodClass
- the class in which to search for the methodname
- the name of the methodconvertStringArguments
- 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>)
publicOnlyMethods
- flag wheter to search for all declared methods (false) or only for the public ones (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 methodNoSuchInstantiatorException
- if the there is no method for the given name and prototypepublic static java.lang.Object callMethod(java.lang.Object methodInstanceOrClass, java.lang.String name, boolean convertStringArguments, boolean publicOnlyMethods, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object... arguments) throws NoSuchInstantiatorException, java.lang.reflect.InvocationTargetException
methodInstanceOrClass
- the instance or class in which to search for the methodname
- the name of the methodconvertStringArguments
- 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>)
publicOnlyMethods
- flag wheter to search for all declared methods (false) or only for the public ones (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 methodNoSuchInstantiatorException
- if the there is no method for the given name and prototypejava.lang.reflect.InvocationTargetException
- if there was an exception when calling the constructorpublic static <T> T callFactoryMethod(java.lang.Class<? extends T> factoryClass, java.lang.String name, boolean convertStringArguments, boolean publicOnlyMethods, java.util.Map<java.lang.String,?> namedInstances, java.lang.Object... arguments) throws NoSuchInstantiatorException, java.lang.reflect.InvocationTargetException
T
- the class created by the factory methodfactoryClass
- the class in which to search for the factory methodname
- the name of the methodconvertStringArguments
- 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>)
publicOnlyMethods
- flag wheter to search for all declared methods (false) or only for the public ones (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 methodNoSuchInstantiatorException
- if the there is no method for the given name and prototypejava.lang.reflect.InvocationTargetException
- if there was an exception when calling the constructorpublic T instantiate(java.lang.Object... arguments) throws java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
MethodInstantiator
class.instantiate
in interface Instantiator<T>
arguments
- the arguments for the encapsulated factory methodjava.lang.IllegalArgumentException
- if the arguments are not compatible with the method prototypejava.lang.reflect.InvocationTargetException
- if there was an exception thrown when the method 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