messif.utility.reflection
Class FactoryMethodInstantiator<T>

java.lang.Object
  extended by messif.utility.reflection.FactoryMethodInstantiator<T>
Type Parameters:
T - the class the instances of which will be created by this FactoryMethodInstantiator
All Implemented Interfaces:
Instantiator<T>

public class FactoryMethodInstantiator<T>
extends java.lang.Object
implements Instantiator<T>

This class allows to create instances of a given class. A factory method with the given prototype is encapsulated and used in subsequent calls. Note that a factory method must be static and must return the object of the given class.

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 Summary
FactoryMethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> methodClass, java.lang.String methodName, java.lang.Class<?>... prototype)
          Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass that accepts parameters of the given prototype.
FactoryMethodInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> methodClass, java.lang.String methodName, int argumentCount)
          Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass that accepts the given number of parameters.
FactoryMethodInstantiator(java.lang.Class<? extends T> objectClass, java.lang.reflect.Method method)
          Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass via the given method.
 
Method Summary
 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 factory method.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FactoryMethodInstantiator

public FactoryMethodInstantiator(java.lang.Class<? extends T> objectClass,
                                 java.lang.reflect.Method method)
                          throws java.lang.IllegalArgumentException
Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass via the given method.

Parameters:
objectClass - the class the instances of which will be created
method - the factory method used to create instances
Throws:
java.lang.IllegalArgumentException - if the provided method is not static or does not return the given objectClass

FactoryMethodInstantiator

public FactoryMethodInstantiator(java.lang.Class<? extends T> checkClass,
                                 java.lang.Class<?> methodClass,
                                 java.lang.String methodName,
                                 java.lang.Class<?>... prototype)
                          throws java.lang.IllegalArgumentException
Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass that accepts parameters of the given prototype.

Parameters:
checkClass - the class the instances of which will be created
methodClass - the class from which the method is taken
methodName - the name of the factory method within the objectClass
prototype - the types of constructor arguments
Throws:
java.lang.IllegalArgumentException - 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 objectClass

FactoryMethodInstantiator

public FactoryMethodInstantiator(java.lang.Class<? extends T> checkClass,
                                 java.lang.Class<?> methodClass,
                                 java.lang.String methodName,
                                 int argumentCount)
                          throws java.lang.IllegalArgumentException
Creates a new instance of FactoryMethodInstantiator for creating instances of objectClass that accepts the given number of parameters. Note that if there are several methods of the same name and number of arguments, one is selected.

Parameters:
checkClass - the class the instances of which will be created
methodClass - the class from which the method is taken
methodName - the name of the factory method within the objectClass
argumentCount - the number of arguments that the method should have
Throws:
java.lang.IllegalArgumentException - 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 objectClass
Method Detail

instantiate

public T instantiate(java.lang.Object... arguments)
              throws java.lang.IllegalArgumentException,
                     java.lang.reflect.InvocationTargetException
Creates a new instance using the encapsulated factory method. The arguments must be compatible with the prototype that was given while creating this FactoryMethodInstantiator class.

Specified by:
instantiate in interface Instantiator<T>
Parameters:
arguments - the arguments for the encapsulated factory method
Returns:
the new instance
Throws:
java.lang.IllegalArgumentException - if the arguments are not compatible with the method prototype
java.lang.reflect.InvocationTargetException - if there was an exception thrown when the method was invoked

getInstantiatorPrototype

public java.lang.Class<?>[] getInstantiatorPrototype()
Description copied from interface: Instantiator
Returns the classes of arguments for the Instantiator.instantiate(java.lang.Object[]) method.

Specified by:
getInstantiatorPrototype in interface Instantiator<T>
Returns:
the prototype of instantiatior arguments

getInstantiatorClass

public java.lang.Class<? extends T> getInstantiatorClass()
Description copied from interface: Instantiator
Returns the class instantiated by this Instantiator.

Specified by:
getInstantiatorClass in interface Instantiator<T>
Returns:
the instantiated class

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object