org.pfsw.reflect
Class ReflectUtil

java.lang.Object
  extended by org.pfsw.reflect.ReflectUtil

public class ReflectUtil
extends java.lang.Object

The current instance (Singleton) of this class can be accessed by method current().

It provides convenience methods on top of the normal standard Java reflection API. However, it allows access to fields, methods and constructors regardless of their visibility (i.e. private, protected, default, public). Of course ignoring the visibility is only possible in environments that have not Java 2 Security turned on. With a security manager present such access to normally invisible members will cause an exception.


Field Summary
static java.lang.Class<?>[] EMPTY_CLASS_ARRAY
          A reusable empty array of type Class[]
static java.lang.reflect.Constructor<?>[] EMPTY_CONSTRUCTOR_ARRAY
          A reusable empty array of type Constructor[]
static java.lang.reflect.Field[] EMPTY_FIELD_ARRAY
          A reusable empty array of type Field[]
static java.lang.reflect.Method[] EMPTY_METHOD_ARRAY
          A reusable empty array of type Method[]
static ReflectUtil RU
          An instance of this class which can be easily included as static import and then used like RU.findClass("java.util.ArrayList").
 
Constructor Summary
protected ReflectUtil()
          Initialize the new instance with default values.
  ReflectUtil(java.lang.ClassLoader classLoader)
          Initialize the new instance with a different class loader.
 
Method Summary
protected  void addFieldsToList(java.util.List<java.lang.reflect.Field> fieldList, java.lang.reflect.Field[] fields)
           
protected  void addInheritedFields(java.util.List<java.lang.reflect.Field> fields, java.lang.Class<?> aClass)
           
protected  void addInheritedMethods(java.util.List<java.lang.reflect.Method> methods, java.lang.Class<?> aClass)
           
protected  void addMethodsToList(java.util.List<java.lang.reflect.Method> methodList, java.lang.reflect.Method[] methods)
           
 java.util.List<IObjectProperty> asObjectProperties(java.util.List<java.lang.reflect.Field> fields)
           
 IObjectProperty asObjectProperty(java.lang.reflect.Field field)
           
 boolean classExists(java.lang.String className)
          Returns true, if the class with the given name can be found in the classpath.
protected  void collectInterfaces(java.util.Set<java.lang.Class<?>> result, java.lang.Class<?> aClass)
           
protected  boolean compatibleTypes(java.lang.Class<?>[] paramTypes, java.lang.Class<?>[] signatureTypes)
          Returns true if the types of the first array are assignable to the types of the second array.
 boolean contains(java.lang.Class<?>[] classes, java.lang.Class<?> aClass)
          Returns true if the given class is found in the provided class array.
 java.lang.Object createInstanceOf(java.lang.String className, java.lang.Object caller, java.lang.Object... params)
          Tries to create an instance of the class with the given name.
<T> T
createInstanceOfType(java.lang.Class<T> type, java.lang.String className, java.lang.Object caller, java.lang.Object... params)
          Tries to create an instance of the class with the given name.
<T> T
createNewInstance(java.lang.Class<T> aClass, java.lang.Object... args)
          If the given class has a constructor matching the given arguments it will be used to create a new instance.
static ReflectUtil current()
          Returns an instance this class supports.
 java.lang.Class<?> findClass(java.lang.String className)
          Returns the class object corresponding to the given class name or null, if the class can't be found.
 java.lang.Class<?>[] findClasses(java.lang.String... classNames)
          Returns the classes corresponding to the given (full qualified) class names.
<T> java.lang.reflect.Constructor<T>
findConstructor(java.lang.Class<T> aClass, java.lang.Class<?>... paramTypes)
          Returns the constructor of the given class for the specified parameter types or null if no such constructor can be found.
<T> java.lang.reflect.Constructor<T>
findDefaultConstructor(java.lang.Class<T> aClass)
          Returns the default constructor of the given class or null if no such constructor can be found.
 java.lang.reflect.Field findField(java.lang.Class<?> aClass, java.lang.String name)
          Returns the field with the specified name in the given class.
 java.lang.reflect.Field findField(java.lang.Class<?> aClass, java.lang.String name, int modifiers)
          Returns the field with the specified name in the given class and all the specified modifiers set.
 java.lang.reflect.Method findMethod(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Returns the method with the specified name if it exists in the given class or any of its superclasses regardless of the method's visibility.
 java.lang.reflect.Method findMethod(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>[] paramTypes, int modifiers)
          Returns the method with the specified name if it exists in the given class.
 java.util.List<java.lang.Class> getAllTypesOf(java.lang.Object object)
          Returns all types of the given object or an empty collection if the object is null.
 java.lang.String getAnnotationValueFrom(java.lang.Class<?> aClass, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
          Tries to find the annotation of the specified type and return its "value" as String.
 java.util.List<java.lang.reflect.Constructor> getConstructorsOf(java.lang.Class<?> aClass)
          Returns a list of all constructors the given class contains.
 java.util.List<java.lang.reflect.Constructor> getConstructorsOf(java.lang.Object object)
          Returns a list of all constructors the class of the given object contains.
<E> E
getEnumValueOf(java.lang.Class<E> enumType, java.lang.String enumValueName)
          A generic mechanism to get for a given name the corresponding enum constant.
 java.lang.reflect.Field getField(java.lang.Object object, java.lang.String name)
          Returns the field with the specified name in the given object.
 java.util.List<java.lang.reflect.Field> getFieldsOf(java.lang.Class<?> aClass)
          Returns a list of all fields the given class contains.
 java.util.List<java.lang.reflect.Field> getFieldsOf(java.lang.Object obj)
          Returns a list of all fields the given objects contains.
 java.lang.Class<?>[] getInterfacesOf(java.lang.Class<?> aClass)
          Returns all interfaces the given class implements.
 java.lang.Class<?>[] getInterfacesOf(java.lang.Object object)
          Returns all interfaces the given object's class implements.
protected  java.lang.ClassLoader getLoader()
          Returns the externally assigned class loader or if not present the class load of this class.
 java.lang.reflect.Method getMethod(java.lang.Object object, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Returns the method with the specified name if it exists in the class of the given object or any of its superclasses regardless the method's visibility.
 java.util.List<java.lang.reflect.Method> getMethodsOf(java.lang.Class<?> aClass)
          Returns a list of all methods the given class contains.
 java.util.List<java.lang.reflect.Method> getMethodsOf(java.lang.Object obj)
          Returns a list of all methods the given objects contains.
 java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Class<?> aClass)
          Returns a list of all properties the given objects contains.
 java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Class<?> aClass, IObjectPropertyFilter filter)
          Returns a list of all properties of the given class and that match the specified filter criteria.
 java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Object object)
          Returns a list of all properties the given objects contains.
 java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Object object, IObjectPropertyFilter filter)
          Returns a list of all properties the given objects contains and that match the specified filter criteria.
 java.lang.Class<?> getTypeOf(java.lang.Object object)
          Returns the type of the given object.
 java.lang.Class<?>[] getTypesFromParameters(java.lang.Object... params)
          Returns an array of the types (classes) corresponding to the parameter objects given to this methods.
<T> T
getValueOf(java.lang.Object obj, java.lang.reflect.Field field)
          Returns the current value of the field in the given object.
<T> T
getValueOf(java.lang.Object obj, java.lang.String name)
          Returns the current value of the field with the specified name in the given object.
 java.lang.String getVisibility(int modifiers)
          Returns the visibility defined by the given modifiers as string.
 boolean hasGetter(java.lang.Object object, java.lang.reflect.Field field)
          Returns whether or not the given object has a getter method (any visibility) for the specified field.
 boolean hasPublicGetter(java.lang.Object object, java.lang.reflect.Field field)
          Returns whether or not the given object has a public getter method for the specified field.
 boolean hasPublicMethod(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Returns true if a public method with the specified name exists in the given class or any of its superclasses.
 boolean hasPublicMethod(java.lang.Object obj, java.lang.String methodName, java.lang.Class<?>... paramTypes)
          Returns true if a public method with the specified name exists in the class of the given object or any of its superclasses.
 boolean hasPublicSetter(java.lang.Object object, java.lang.reflect.Field field)
          Returns whether or not the given object has a public setter method for the specified field.
 boolean hasSetter(java.lang.Object object, java.lang.reflect.Field field)
          Returns whether or not the given object has a setter method (any visibility) for the specified field.
 boolean implementsInterface(java.lang.Class<?> aClass, java.lang.Class<?> anInterface)
          Returns true if the given class implements the specified interfaceType.
 boolean implementsInterface(java.lang.Object object, java.lang.Class<?> anInterface)
          Returns true if the class of the given object implements the specified interfaceType.
 int indexOf(java.lang.Class<?>[] classes, java.lang.Class<?> aClass)
          Returns the index of the given class in the provided class array or -1 if the class is not in the array.
protected
<T> T
internalCreateNewInstanceOfClass(boolean returnNull, java.lang.Class<T> aClass, java.lang.Object... params)
          If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance.
 boolean isDefaultVisibility(int modifiers)
          Returns true, if the visibility defined by the given modifiers is the default (package) visibility.
protected  boolean isNullOrEmpty(java.util.Collection<?> collection)
           
protected  boolean isNullOrEmpty(java.lang.Object[] objects)
           
 boolean isPackageVisible(java.lang.reflect.Member member)
          Returns true if the given member (Field, Method, Constructor) is not null and is package visible.
 boolean isSubTypeOf(java.lang.Class<?> superType, java.lang.Class<?> type)
          Returns whether or not the given type extends the given superType.
 java.lang.String makeAccessMethodName(java.lang.String prefix, java.lang.String fieldName)
          Returns the method name with the specified prefix for the given field name.
 java.lang.String makeGetterName(java.lang.String fieldName)
          Returns the getter method name for the given field name.
 java.lang.String makeSetterName(java.lang.String fieldName)
          Returns the setter method name for the given field name.
<T> T
newInstance(java.lang.Class<T> aClass)
          If the given class has a constructor without parameters it will be used to create a new instance.
<T> T
newInstance(java.lang.Class<T> aClass, java.lang.Object param)
          If the given class has a constructor with one parameter type matching the given parameter it will be used to create a new instance.
<T> T
newInstance(java.lang.Class<T> aClass, java.lang.Object[] params)
          If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance.
<T> T
newInstance(java.lang.Class<T> aClass, java.lang.Object param1, java.lang.Object param2)
          If the given class has a constructor with two parameter type matching the given parameters it will be used to create a new instance.
 java.lang.Object newInstance(java.lang.String className)
          Tries to find the class with the given name and to create an instance of it.
 java.lang.Object newInstance(java.lang.String className, java.lang.Object param)
          Tries to find the class with the given name and to create an instance of it.
 java.lang.Object newInstance(java.lang.String className, java.lang.Object[] params)
          Tries to find the class with the given name and to create an instance of it.
 java.lang.Object newInstance(java.lang.String className, java.lang.Object param1, java.lang.Object param2)
          Tries to find the class with the given name and to create an instance of it.
<T> T
newInstanceOf(java.lang.Class<T> aClass, java.lang.Object... params)
          If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance.
 java.util.List<IObjectProperty> onlyMatching(java.util.List<IObjectProperty> properties, IObjectPropertyFilter filter)
          Returns a list of all object properties that match the given filter criteria.
 void setValueOf(java.lang.Object obj, java.lang.reflect.Field field, java.lang.Object value)
          Sets the specified field of the given object to the provided value.
protected  void setValueOf(java.lang.Object obj, java.lang.reflect.Field field, java.lang.Object value, boolean isPrimitive)
           
 void setValueOf(java.lang.Object obj, java.lang.String name, boolean value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, byte value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, char value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, double value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, float value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, int value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object obj, java.lang.String name, long value)
          Sets the value of the field with the specified name to the given value.
 void setValueOf(java.lang.Object targetObject, java.lang.String name, java.lang.Object value)
          Sets the value of the field with the specified name to the given value.
protected  void setValueOf(java.lang.Object obj, java.lang.String name, java.lang.Object value, boolean isPrimitive)
           
 void setValueOf(java.lang.Object obj, java.lang.String name, short value)
          Sets the value of the field with the specified name to the given value.
<T> T[]
toArray(java.util.Collection<?> coll, java.lang.String methodName, java.lang.Class<T> elementType)
          Returns an array containing all objects that are returned by the specified method name executed against each element in the given collection.
 java.lang.String[] toStringArray(java.util.Collection<?> coll, java.lang.String methodName)
          Returns a string array containing all objects that are returned by the specified method name executed against each element in the given collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RU

public static final ReflectUtil RU
An instance of this class which can be easily included as static import and then used like RU.findClass("java.util.ArrayList").


EMPTY_CLASS_ARRAY

public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
A reusable empty array of type Class[]


EMPTY_FIELD_ARRAY

public static final java.lang.reflect.Field[] EMPTY_FIELD_ARRAY
A reusable empty array of type Field[]


EMPTY_METHOD_ARRAY

public static final java.lang.reflect.Method[] EMPTY_METHOD_ARRAY
A reusable empty array of type Method[]


EMPTY_CONSTRUCTOR_ARRAY

public static final java.lang.reflect.Constructor<?>[] EMPTY_CONSTRUCTOR_ARRAY
A reusable empty array of type Constructor[]

Constructor Detail

ReflectUtil

protected ReflectUtil()
Initialize the new instance with default values.


ReflectUtil

public ReflectUtil(java.lang.ClassLoader classLoader)
Initialize the new instance with a different class loader.

Parameters:
classLoader - The class loader to be used to load classes by name.
Method Detail

current

public static ReflectUtil current()
Returns an instance this class supports. It uses this calls' class loader.


classExists

public boolean classExists(java.lang.String className)
Returns true, if the class with the given name can be found in the classpath.


findClasses

public java.lang.Class<?>[] findClasses(java.lang.String... classNames)
                                 throws java.lang.ClassNotFoundException
Returns the classes corresponding to the given (full qualified) class names.

Parameters:
classNames - The full qualified names of the classes to look for.
Returns:
An array with the same number of elements as the input parameter containing the class object corresponding to the given class names.
Throws:
java.lang.ClassNotFoundException - If any of the classes cannot be found.

findClass

public java.lang.Class<?> findClass(java.lang.String className)
Returns the class object corresponding to the given class name or null, if the class can't be found. For primitive types the names "boolean", "int", "float" and so on can be used. The corresponding Boolean.TYPE, Integer.TYPE, Float.TYPE and so on will be returned.

Parameters:
className - The full qualified name of the class

isSubTypeOf

public boolean isSubTypeOf(java.lang.Class<?> superType,
                           java.lang.Class<?> type)
Returns whether or not the given type extends the given superType.

Parameters:
superType - The type the that the other type potentially extends (must not be null).
type - The type to be checked if it is a sub-type of the given superType (must not be null).
Returns:
true if type extends superType.

createInstanceOf

public java.lang.Object createInstanceOf(java.lang.String className,
                                         java.lang.Object caller,
                                         java.lang.Object... params)
Tries to create an instance of the class with the given name. It uses the given caller to find the class (via forName()) to ensure that the correct classloader is used. If the caller is null it uses this class for the lookup.

Parameters:
className - The name of the class to instantiate.
caller - The object which class will be used to search for the className.
params - 0-n parameters that define the constructor to be used.
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

createInstanceOfType

public <T> T createInstanceOfType(java.lang.Class<T> type,
                                  java.lang.String className,
                                  java.lang.Object caller,
                                  java.lang.Object... params)
Tries to create an instance of the class with the given name. It uses the given caller to find the class (via forName()) to ensure that the correct classloader is used. If the caller is null it uses this class for the lookup.

Parameters:
type - The expected type of the instance (could be an interface or also java.lang.Object)
className - The name of the class to instantiate.
caller - The object which class will be used to search for the className.
params - 0-n parameters that define the constructor to be used.
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

getInterfacesOf

public java.lang.Class<?>[] getInterfacesOf(java.lang.Object object)
Returns all interfaces the given object's class implements. The resulting array contains all directly implemented interfaces as well as those that are only indirectly implemented by extension. If no interface is found an empty array will be returned.


getInterfacesOf

public java.lang.Class<?>[] getInterfacesOf(java.lang.Class<?> aClass)
Returns all interfaces the given class implements. The resulting array contains all directly implemented interfaces as well as those that are only indirectly implemented by extension. If no interface is found an empty array will be returned.


findMethod

public java.lang.reflect.Method findMethod(java.lang.Class<?> aClass,
                                           java.lang.String methodName,
                                           java.lang.Class<?>[] paramTypes,
                                           int modifiers)
Returns the method with the specified name if it exists in the given class. The method will only be found if it has all modifiers set that are defined in parameter modifiers.

Parameters:
aClass - The class in which to search the method
methodName - The name of the searched method
paramTypes - The types of the method's parameters (null and Class[0] ar the same)
modifiers - The modifiers that must be set at the method too look for
Returns:
The method or null if not found
See Also:
getMethod(Object, String, Class[]), findMethod(Class, String, Class[])

findMethod

public java.lang.reflect.Method findMethod(java.lang.Class<?> aClass,
                                           java.lang.String methodName,
                                           java.lang.Class<?>... paramTypes)
Returns the method with the specified name if it exists in the given class or any of its superclasses regardless of the method's visibility.

Parameters:
aClass - The class in which to search the method
methodName - The name of the searched method
paramTypes - The types of the method's parameters (null and Class[0] ar the same)
Returns:
The method or null if not found
See Also:
getMethod(Object, String, Class[])

getMethod

public java.lang.reflect.Method getMethod(java.lang.Object object,
                                          java.lang.String methodName,
                                          java.lang.Class<?>... paramTypes)
Returns the method with the specified name if it exists in the class of the given object or any of its superclasses regardless the method's visibility.

Parameters:
object - The object in which the method should be found
methodName - The name of the searched method
paramTypes - The types of the method's parameters (null and Class[0] are the same)
Returns:
The method or null if not found
See Also:
findMethod(Class, String, Class[])

getMethodsOf

public java.util.List<java.lang.reflect.Method> getMethodsOf(java.lang.Object obj)
Returns a list of all methods the given objects contains. This includes all inherited methods, regardless of their visibility or other modifiers.

Parameters:
obj - The object of which to get the methods
Returns:
A List of java.lang.reflect.Method

getMethodsOf

public java.util.List<java.lang.reflect.Method> getMethodsOf(java.lang.Class<?> aClass)
Returns a list of all methods the given class contains. This includes all inherited methods, regardless of their visibility or other modifiers.

Parameters:
aClass - The class of which to get the methods
Returns:
A List of java.lang.reflect.Method

getConstructorsOf

public java.util.List<java.lang.reflect.Constructor> getConstructorsOf(java.lang.Object object)
Returns a list of all constructors the class of the given object contains. This includes all constructors, regardless of their visibility or other modifiers. Even if it has only the default constructor it will be returned.

Parameters:
object - The object of which to get the constructors
Returns:
A List of java.lang.reflect.Constructor

getConstructorsOf

public java.util.List<java.lang.reflect.Constructor> getConstructorsOf(java.lang.Class<?> aClass)
Returns a list of all constructors the given class contains. This includes all constructors, regardless of their visibility or other modifiers. Even if it has only the default constructor it will be returned.

Parameters:
aClass - The class of which to get the constructors
Returns:
A List of java.lang.reflect.Constructor

getFieldsOf

public java.util.List<java.lang.reflect.Field> getFieldsOf(java.lang.Object obj)
Returns a list of all fields the given objects contains. This includes all inherited fields, regardless their visibility or other modifier states.

Parameters:
obj - The object to get the fields from
Returns:
A List of java.lang.reflect.Field

getObjectPropertiesOf

public java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Object object)
Returns a list of all properties the given objects contains. This includes all inherited fields, regardless their visibility or other modifier states.

Parameters:
object - The object to get the fields from.
Returns:
A List of object properties that correspond to the declared fields of the given object.

getObjectPropertiesOf

public java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Object object,
                                                             IObjectPropertyFilter filter)
Returns a list of all properties the given objects contains and that match the specified filter criteria.

Parameters:
object - The object to get the fields from (must not be null).
filter - The filter criteria the object properties must match (must not be null).
Returns:
A List of filtered object properties.

getFieldsOf

public java.util.List<java.lang.reflect.Field> getFieldsOf(java.lang.Class<?> aClass)
Returns a list of all fields the given class contains. This includes all inherited fields, regardless their visibility or other modifier states.

Parameters:
aClass - The class to get the fields from
Returns:
A List of java.lang.reflect.Field

getObjectPropertiesOf

public java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Class<?> aClass)
Returns a list of all properties the given objects contains. This includes all inherited fields, regardless their visibility or other modifier states.

Parameters:
aClass - The class to get the fields from
Returns:
A List of object properties that correspond to the declared fields of the given object.

getObjectPropertiesOf

public java.util.List<IObjectProperty> getObjectPropertiesOf(java.lang.Class<?> aClass,
                                                             IObjectPropertyFilter filter)
Returns a list of all properties of the given class and that match the specified filter criteria.

Parameters:
aClass - The class to get the fields from (must not be null).
filter - The filter criteria the object properties must match (must not be null).
Returns:
A List of filtered object properties.

findField

public java.lang.reflect.Field findField(java.lang.Class<?> aClass,
                                         java.lang.String name,
                                         int modifiers)
Returns the field with the specified name in the given class and all the specified modifiers set. If the field can't be found, null is returned.

Parameters:
aClass - The class that might contain the field
name - The name of the field to look for
modifiers - The modifiers the field must have set
Throws:
java.lang.IllegalArgumentException - If aClass or name is null

findField

public java.lang.reflect.Field findField(java.lang.Class<?> aClass,
                                         java.lang.String name)
Returns the field with the specified name in the given class. If the field can't be found, null is returned.

Parameters:
aClass - The class that might contain the field
name - The name of the field to look for
Throws:
java.lang.IllegalArgumentException - If aClass or name is null

getField

public java.lang.reflect.Field getField(java.lang.Object object,
                                        java.lang.String name)
Returns the field with the specified name in the given object. If the field can't be found, null is returned.

If the given object is an instance of Class, the field will be looked-up in the type represented by the class and not in the class object itself.

Parameters:
object - The object that (perhaps) contains the field. This can also be an instance of Class.
name - The name of the field to look for
Throws:
java.lang.IllegalArgumentException - If object or name is null

getValueOf

public <T> T getValueOf(java.lang.Object obj,
                        java.lang.String name)
Returns the current value of the field with the specified name in the given object.

Parameters:
obj - The object that contains the field (must not be null).
name - The name of the field to look for (must not be null).
Throws:
UnknownFieldException - If the field is unknown in the given object.
java.lang.IllegalArgumentException - If obj or name is null.

getValueOf

public <T> T getValueOf(java.lang.Object obj,
                        java.lang.reflect.Field field)
Returns the current value of the field in the given object.

Parameters:
obj - The object that contains the field (must not be null).
field - The field to look for (must not be null).
Throws:
java.lang.IllegalArgumentException - If obj or name is null.

setValueOf

public void setValueOf(java.lang.Object targetObject,
                       java.lang.String name,
                       java.lang.Object value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
targetObject - The object that contains the field.
name - The name of the field to set.
value - The value to assign to the field.
Throws:
UnknownFieldException - If the field is unknown in the given object.
java.lang.IllegalArgumentException - If obj or name is null.

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       char value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       int value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       byte value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       boolean value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object.
java.lang.IllegalArgumentException - If obj or name is null.

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       long value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       short value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       double value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.String name,
                       float value)
Sets the value of the field with the specified name to the given value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field
name - The name of the field to set
value - The value to assign to the field
Throws:
UnknownFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is null

setValueOf

public void setValueOf(java.lang.Object obj,
                       java.lang.reflect.Field field,
                       java.lang.Object value)
Sets the specified field of the given object to the provided value. Be aware that setting final fields this way only works up to Java 11 - afterwards it is not possible to set final fields anymore!

Parameters:
obj - The object that contains the field (must not be null).
field - The field to be modified (must not be null).
value - The new value to assign to the field (may be null).

hasPublicMethod

public boolean hasPublicMethod(java.lang.Class<?> aClass,
                               java.lang.String methodName,
                               java.lang.Class<?>... paramTypes)
Returns true if a public method with the specified name exists in the given class or any of its superclasses.

Parameters:
aClass - The class in which to look for the method
methodName - The name of the method to look for
paramTypes - The types of the method's parameters (null and Class[0] ar the same)
Returns:
true if the method was found and is public

hasPublicMethod

public boolean hasPublicMethod(java.lang.Object obj,
                               java.lang.String methodName,
                               java.lang.Class<?>... paramTypes)
Returns true if a public method with the specified name exists in the class of the given object or any of its superclasses.

Parameters:
obj - The object in which the method should be found
methodName - The name of the method to look for
paramTypes - The types of the method's parameters (null and Class[0] ar the same)
Returns:
true if the method was found and is public

hasPublicGetter

public boolean hasPublicGetter(java.lang.Object object,
                               java.lang.reflect.Field field)
Returns whether or not the given object has a public getter method for the specified field.


hasPublicSetter

public boolean hasPublicSetter(java.lang.Object object,
                               java.lang.reflect.Field field)
Returns whether or not the given object has a public setter method for the specified field.


hasGetter

public boolean hasGetter(java.lang.Object object,
                         java.lang.reflect.Field field)
Returns whether or not the given object has a getter method (any visibility) for the specified field.


hasSetter

public boolean hasSetter(java.lang.Object object,
                         java.lang.reflect.Field field)
Returns whether or not the given object has a setter method (any visibility) for the specified field.


isPackageVisible

public boolean isPackageVisible(java.lang.reflect.Member member)
Returns true if the given member (Field, Method, Constructor) is not null and is package visible.


isDefaultVisibility

public boolean isDefaultVisibility(int modifiers)
Returns true, if the visibility defined by the given modifiers is the default (package) visibility.


getVisibility

public java.lang.String getVisibility(int modifiers)
Returns the visibility defined by the given modifiers as string. That is, "" for the default (package) visibility and "public", "protected", "private" for the others.


createNewInstance

public <T> T createNewInstance(java.lang.Class<T> aClass,
                               java.lang.Object... args)
If the given class has a constructor matching the given arguments it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance. If no such constructor is available a ReflectionException will be thrown.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
args - The arguments to be passed to the constructor. If omitted the default constructor will be used.
Returns:
The created instance.
Throws:
ReflectionException - A runtime exception that indicates that the class has no default constructor or it wraps another exception that occurred during instantiation.

newInstance

public <T> T newInstance(java.lang.Class<T> aClass)
If the given class has a constructor without parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

It is recommended to use method createNewInstance(Class, Object...) instead because it throws a ReflectionException if the constructor cannot be found.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.
See Also:
createNewInstance(java.lang.Class, java.lang.Object...)

newInstance

public <T> T newInstance(java.lang.Class<T> aClass,
                         java.lang.Object param)
If the given class has a constructor with one parameter type matching the given parameter it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
param - The initialization parameter for the constructor (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public <T> T newInstance(java.lang.Class<T> aClass,
                         java.lang.Object param1,
                         java.lang.Object param2)
If the given class has a constructor with two parameter type matching the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
param1 - The first initialization parameter for the constructor (must not be null)
param2 - The second initialization parameter for the constructor (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public <T> T newInstance(java.lang.Class<T> aClass,
                         java.lang.Object[] params)
If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.
This method is exactly the same as newInstanceOf(Class, Object...). It is just kept for compatibility.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
params - The initialization parameters for the constructor (may be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstanceOf

public <T> T newInstanceOf(java.lang.Class<T> aClass,
                           java.lang.Object... params)
If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.
This method is exactly the same as newInstance(Class, Object[]). It is just uses the varargs declaration that is available since Java 1.5.

Parameters:
aClass - The class of which a new instance must be created (must not be null)
params - The initialization parameters for the constructor (may be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public java.lang.Object newInstance(java.lang.String className)
Tries to find the class with the given name and to create an instance of it. If the given class has a constructor without parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
className - The name of the class of which a new instance must be created (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public java.lang.Object newInstance(java.lang.String className,
                                    java.lang.Object param)
Tries to find the class with the given name and to create an instance of it. If the given class has a constructor with one parameter type matching the given parameter it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
className - The name of the class of which a new instance must be created (must not be null)
param - The initialization parameter for the constructor (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public java.lang.Object newInstance(java.lang.String className,
                                    java.lang.Object param1,
                                    java.lang.Object param2)
Tries to find the class with the given name and to create an instance of it. If the given class has a constructor with two parameter type matching the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
className - The name of the class of which a new instance must be created (must not be null)
param1 - The first initialization parameter for the constructor (must not be null)
param2 - The second initialization parameter for the constructor (must not be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

newInstance

public java.lang.Object newInstance(java.lang.String className,
                                    java.lang.Object[] params)
Tries to find the class with the given name and to create an instance of it. If the found class has a constructor with types corresponding to the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.

Parameters:
className - The name of the class of which a new instance must be created (must not be null)
params - The initialization parameters for the constructor (may be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

findDefaultConstructor

public <T> java.lang.reflect.Constructor<T> findDefaultConstructor(java.lang.Class<T> aClass)
Returns the default constructor of the given class or null if no such constructor can be found. The visibility of the constructor is not relevant.

Returns:
A constructor or null.

findConstructor

public <T> java.lang.reflect.Constructor<T> findConstructor(java.lang.Class<T> aClass,
                                                            java.lang.Class<?>... paramTypes)
Returns the constructor of the given class for the specified parameter types or null if no such constructor can be found. The visibility of the constructor is ignored. A private constructor can be used with the newInstance() methods of this class to create instances.

Returns:
A constructor or null.
See Also:
newInstance(Class)

getTypesFromParameters

public java.lang.Class<?>[] getTypesFromParameters(java.lang.Object... params)
Returns an array of the types (classes) corresponding to the parameter objects given to this methods.

Parameters:
params - The parameters to derive the types from (may be null)
Returns:
The types or an empty array if params == null

getTypeOf

public java.lang.Class<?> getTypeOf(java.lang.Object object)
Returns the type of the given object. For the special objects like Integer, Boolean, ... it returns the primitive type. If the given object is null it returns Object.class

Parameters:
object - The object of which to determine the type

getAllTypesOf

public java.util.List<java.lang.Class> getAllTypesOf(java.lang.Object object)
Returns all types of the given object or an empty collection if the object is null. The returned array contains the object's class and all interfaces it implements, including all inherited interfaces.

Parameters:
object - The object to derive all types of

contains

public boolean contains(java.lang.Class<?>[] classes,
                        java.lang.Class<?> aClass)
Returns true if the given class is found in the provided class array.


indexOf

public int indexOf(java.lang.Class<?>[] classes,
                   java.lang.Class<?> aClass)
Returns the index of the given class in the provided class array or -1 if the class is not in the array.


implementsInterface

public boolean implementsInterface(java.lang.Object object,
                                   java.lang.Class<?> anInterface)
Returns true if the class of the given object implements the specified interfaceType.


implementsInterface

public boolean implementsInterface(java.lang.Class<?> aClass,
                                   java.lang.Class<?> anInterface)
Returns true if the given class implements the specified interfaceType.


toArray

public <T> T[] toArray(java.util.Collection<?> coll,
                       java.lang.String methodName,
                       java.lang.Class<T> elementType)
Returns an array containing all objects that are returned by the specified method name executed against each element in the given collection.

Parameters:
coll - The collection with elements of which the objects have to be extracted
methodName - The name of the method to be executed on the collections's elements (must not be null) This method must have no argument and must return an object of the specified elementType.
elementType - The type of the elements returned by the method and of the elements in the return array
Returns:
Returns an array of the specified elementType or null if the given collection is null.

toStringArray

public java.lang.String[] toStringArray(java.util.Collection<?> coll,
                                        java.lang.String methodName)
Returns a string array containing all objects that are returned by the specified method name executed against each element in the given collection.

Parameters:
coll - The collection with elements of which the strings have to be extracted
methodName - The name of the method to be executed on the collections's elements (must not be null). This method must have no argument and must return a string.
Returns:
Returns a string array or null if the given collection is null.

getAnnotationValueFrom

public java.lang.String getAnnotationValueFrom(java.lang.Class<?> aClass,
                                               java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Tries to find the annotation of the specified type and return its "value" as String.

Parameters:
aClass - The potentially annotated class
annotationType - The annotation to look for
Returns:
The value string of the annotation of null if not found

asObjectProperty

public IObjectProperty asObjectProperty(java.lang.reflect.Field field)

asObjectProperties

public java.util.List<IObjectProperty> asObjectProperties(java.util.List<java.lang.reflect.Field> fields)

onlyMatching

public java.util.List<IObjectProperty> onlyMatching(java.util.List<IObjectProperty> properties,
                                                    IObjectPropertyFilter filter)
Returns a list of all object properties that match the given filter criteria.

Parameters:
properties - The list of properties to be filtered (must not be null).
filter - The filter that decides which property goes into the result list (must not be null).
Returns:
A List of object properties that correspond to the declared fields of the given object.

getEnumValueOf

public <E> E getEnumValueOf(java.lang.Class<E> enumType,
                            java.lang.String enumValueName)
A generic mechanism to get for a given name the corresponding enum constant.

Parameters:
enumType - The enum type class (must not be null).
enumValueName - The name of the enum constant to be retrieved (may be null).
Returns:
null if enumValueName is null or if no corresponding enum constant can be found, otherwise it returns the enum constant.
Throws:
ReflectionException - If the given type is no enum type or accessing the enum constants caused a problem.

makeGetterName

public java.lang.String makeGetterName(java.lang.String fieldName)
Returns the getter method name for the given field name.


makeSetterName

public java.lang.String makeSetterName(java.lang.String fieldName)
Returns the setter method name for the given field name.


makeAccessMethodName

public java.lang.String makeAccessMethodName(java.lang.String prefix,
                                             java.lang.String fieldName)
Returns the method name with the specified prefix for the given field name.


internalCreateNewInstanceOfClass

protected <T> T internalCreateNewInstanceOfClass(boolean returnNull,
                                                 java.lang.Class<T> aClass,
                                                 java.lang.Object... params)
If the given class has a constructor with types corresponding to the given parameters it will be used to create a new instance. The constructor will be called regardless of its visibility. That is, even private, protected and default constructors are used to create the new instance.
This method is exactly the same as newInstance(Class, Object[]). It is just uses the varargs declaration that is available since Java 1.5.

Parameters:
returnNull - If true null will be returned if constructor cannot be found, otherwise a ReflectionException will be thrown in that case.
aClass - The class of which a new instance must be created (must not be null)
params - The initialization parameters for the constructor (may be null)
Returns:
The new created instance or null if no matching constructor can be found
Throws:
ReflectionException - A runtime exception that wraps the original exception.

addMethodsToList

protected void addMethodsToList(java.util.List<java.lang.reflect.Method> methodList,
                                java.lang.reflect.Method[] methods)

addInheritedMethods

protected void addInheritedMethods(java.util.List<java.lang.reflect.Method> methods,
                                   java.lang.Class<?> aClass)
                            throws java.lang.SecurityException
Throws:
java.lang.SecurityException

addFieldsToList

protected void addFieldsToList(java.util.List<java.lang.reflect.Field> fieldList,
                               java.lang.reflect.Field[] fields)

addInheritedFields

protected void addInheritedFields(java.util.List<java.lang.reflect.Field> fields,
                                  java.lang.Class<?> aClass)
                           throws java.lang.SecurityException
Throws:
java.lang.SecurityException

setValueOf

protected void setValueOf(java.lang.Object obj,
                          java.lang.String name,
                          java.lang.Object value,
                          boolean isPrimitive)

setValueOf

protected void setValueOf(java.lang.Object obj,
                          java.lang.reflect.Field field,
                          java.lang.Object value,
                          boolean isPrimitive)

compatibleTypes

protected boolean compatibleTypes(java.lang.Class<?>[] paramTypes,
                                  java.lang.Class<?>[] signatureTypes)
Returns true if the types of the first array are assignable to the types of the second array. The second array is immutable.


collectInterfaces

protected void collectInterfaces(java.util.Set<java.lang.Class<?>> result,
                                 java.lang.Class<?> aClass)

isNullOrEmpty

protected boolean isNullOrEmpty(java.lang.Object[] objects)

isNullOrEmpty

protected boolean isNullOrEmpty(java.util.Collection<?> collection)

getLoader

protected java.lang.ClassLoader getLoader()
Returns the externally assigned class loader or if not present the class load of this class.