messif.utility.reflection
Class ConstructorInstantiator<T>

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

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

This class allows to create instances of a given class. A constructor with the given prototype is encapsulated and used in subsequent calls.

This class provides a convenient way of repeatable creation of instances of a given class without the need of repetable constructor retrieval and checking all the exceptions.


Constructor Summary
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.
 
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 constructor.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConstructorInstantiator

public ConstructorInstantiator(java.lang.reflect.Constructor<? extends T> constructor)
                        throws java.lang.IllegalArgumentException
Creates a new instance of ConstructorInstantiator for creating instances of objectClass via the specified constructor.

Parameters:
constructor - the constructor using which the instances will be created
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper constructor

ConstructorInstantiator

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

Parameters:
objectClass - the class the instances of which will be created
prototype - the types of constructor arguments
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper constructor

ConstructorInstantiator

public ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
                               int argumentCount)
                        throws java.lang.IllegalArgumentException
Creates a new instance of ConstructorInstantiator for creating instances of objectClass that accepts the given number of arguments. Note that if there are several constructors with the same number of arguments, one is selected.

Parameters:
objectClass - the class the instances of which will be created
argumentCount - the number of arguments that the constructor should have
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper constructor

ConstructorInstantiator

public ConstructorInstantiator(java.lang.Class<? extends T> objectClass,
                               java.lang.Object... arguments)
                        throws java.lang.IllegalArgumentException
Creates a new instance of ConstructorInstantiator for creating instances of objectClass that accepts the given arguments.

Parameters:
objectClass - the class the instances of which will be created
arguments - the arguments for the constructor
Throws:
java.lang.IllegalArgumentException - if the provided class does not have a proper constructor
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 constructor. The arguments must be compatible with the prototype that was given while creating this ConstructorInstantiator class.

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