messif.utility.reflection
Class FieldInstantiator<T>

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

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

This class allows to create instances of a given class. A field of the given type is encapsulated and used in subsequent calls. Note that FieldInstantiator instantiates by getting a value of a given filed on the instance provided in arguments (can be omitted if the field is static).

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


Constructor Summary
FieldInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> fieldClass, java.lang.String fieldName)
          Creates a new instance of FieldInstantiator for creating instances of objectClass.
FieldInstantiator(java.lang.Class<? extends T> objectClass, java.lang.reflect.Field field)
          Creates a new instance of FieldInstantiator for creating instances of objectClass via the given field.
 
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 field.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FieldInstantiator

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

Parameters:
objectClass - the class the instances of which will be created
field - the field used to create instances
Throws:
java.lang.IllegalArgumentException - if the provided field has not the given objectClass type

FieldInstantiator

public FieldInstantiator(java.lang.Class<? extends T> checkClass,
                         java.lang.Class<?> fieldClass,
                         java.lang.String fieldName)
                  throws java.lang.IllegalArgumentException
Creates a new instance of FieldInstantiator for creating instances of objectClass.

Parameters:
checkClass - the class the instances of which will be created
fieldClass - the class in which the field is looked up
fieldName - the name of the field within the objectClass
Throws:
java.lang.IllegalArgumentException - if the there is no field for the given name or if such field has not the given objectClass type
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 field. There must be a single argument with the instance from which to get the field.

Specified by:
instantiate in interface Instantiator<T>
Parameters:
arguments - the instance from which to get the field (or null if the field is static)
Returns:
the new instance
Throws:
java.lang.IllegalArgumentException - if the arguments are not compatible
java.lang.reflect.InvocationTargetException - if there was an exception thrown when the instance was created

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