T
- the class the instances of which will be created by this FieldInstantiatorpublic class FieldInstantiator<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 field retrieval and checking all the exceptions.
Constructor and Description |
---|
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,
java.lang.Object fieldInstance)
Creates a new instance of FieldInstantiator for creating instances of
objectClass via the given field. |
FieldInstantiator(java.lang.Class<? extends T> checkClass,
java.lang.Object fieldInstance,
java.lang.String fieldName)
Creates a new instance of FieldInstantiator for creating instances of
objectClass . |
Modifier and Type | Method and Description |
---|---|
static java.lang.reflect.Field |
getField(java.lang.Class<?> fieldClass,
boolean publicOnlyField,
java.lang.String name)
Retrieves a field with the given name 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)
Returns the instance in the encapsulated field.
|
java.lang.String |
toString() |
public FieldInstantiator(java.lang.Class<? extends T> objectClass, java.lang.reflect.Field field, java.lang.Object fieldInstance) throws NoSuchInstantiatorException
objectClass
via the given field.objectClass
- the class the instances of which will be createdfield
- the field used to create instancesfieldInstance
- the instance from which the field is taken or null if the field is staticNoSuchInstantiatorException
- if the provided field has not the given objectClass typepublic FieldInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Class<?> fieldClass, java.lang.String fieldName) throws NoSuchInstantiatorException
objectClass
.checkClass
- the class the instances of which will be createdfieldClass
- the class in which the field is looked upfieldName
- the name of the field within the objectClass
NoSuchInstantiatorException
- if the there is no field for the given name or
if such field has not the given objectClass typepublic FieldInstantiator(java.lang.Class<? extends T> checkClass, java.lang.Object fieldInstance, java.lang.String fieldName) throws NoSuchInstantiatorException
objectClass
.checkClass
- the class the instances of which will be createdfieldInstance
- the instance from which the field is taken or null if the field is staticfieldName
- the name of the field within the objectClass
NoSuchInstantiatorException
- if the there is no field for the given name or
if such field has not the given objectClass typepublic static java.lang.reflect.Field getField(java.lang.Class<?> fieldClass, boolean publicOnlyField, java.lang.String name) throws NoSuchInstantiatorException
fieldClass
- the class in which to search for the fieldpublicOnlyField
- flag wheter to search for all declared fields (false) or only for the public ones (true)name
- the name of the fieldNoSuchInstantiatorException
- if the there is no field for the given namepublic T instantiate(java.lang.Object... arguments) throws java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
instantiate
in interface Instantiator<T>
arguments
- arguments are ignoredjava.lang.IllegalArgumentException
- if the arguments are not compatiblejava.lang.reflect.InvocationTargetException
- if there was an exception thrown when the instance was createdpublic 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