org.pf.reflect
Class ReflectUtil

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

public class ReflectUtil
extends java.lang.Object

The sole 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.

Version:
1.6
Author:
Manfred Duchrow

Field Summary
static java.lang.Class[] EMPTY_CLASS_ARRAY
          A reusable empty array of type Class[]
 
Constructor Summary
ReflectUtil(java.lang.ClassLoader classLoader)
          Initialize the new instance with a different class loader.
 
Method Summary
 boolean classExists(java.lang.String className)
          Returns true, if the class with the given name can be found in the classpath.
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.reflect.Constructor findConstructor(java.lang.Class aClass, java.lang.Class[] paramTypes)
          Returns the constructor of the given class for the specified parameter types of 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 getConstructorsOf(java.lang.Class aClass)
          Returns a list of all constructors the given class contains.
 java.lang.reflect.Field getField(java.lang.Object obj, java.lang.String name)
          Returns the field with the specified name in the given object.
 java.util.List getFieldsOf(java.lang.Class aClass)
          Returns a list of all fields the given class contains.
 java.util.List getFieldsOf(java.lang.Object obj)
          Returns a list of all fields the given objects contains.
 java.lang.reflect.Method getMethod(java.lang.Object obj, 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 getMethodsOf(java.lang.Class aClass)
          Returns a list of all methods the given class contains.
 java.util.List getMethodsOf(java.lang.Object obj)
          Returns a list of all methods the given objects contains.
 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.
 java.lang.Object 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 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 isDefaultVisibility(int modifiers)
          Returns true, if the visibility defined by the given modifiers is the default (package) visibility.
 boolean isPackageVisible(java.lang.reflect.Field field)
          Returns true if the given field is not null and is package visible.
 boolean isPackageVisible(java.lang.reflect.Method method)
          Returns true if the given method is not null and is package visible.
 java.lang.Object newInstance(java.lang.Class aClass)
          If the given class has a constructor without parameters it will be used to create a new instance.
 java.lang.Object newInstance(java.lang.Class 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.
 java.lang.Object newInstance(java.lang.Class 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.lang.Object newInstance(java.lang.Class 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.
 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 obj, java.lang.String name, java.lang.Object 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, short value)
          Sets the value of the field with the specified name to the given value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_CLASS_ARRAY

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

Constructor Detail

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.


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

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 obj,
                                          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:
obj - 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] ar the same)
Returns:
The method or null if not found
See Also:
findMethod(Class, String, Class[])

getMethodsOf

public java.util.List 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 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 getConstructorsOf(java.lang.Class aClass)
Returns a list of all constructors the given class contains. This includes all inherited methods, 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 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

getFieldsOf

public java.util.List 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

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 obj,
                                        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.

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

getValueOf

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

Parameters:
obj - The object that contains the field
name - The name of the field to look for
Throws:
java.lang.NoSuchFieldException - 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,
                       java.lang.Object value)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - 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)
                throws java.lang.NoSuchFieldException
Sets the value of the field with the specified name to the given value.

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:
java.lang.NoSuchFieldException - If the field is unknown in the given object
java.lang.IllegalArgumentException - If obj or name is 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

isPackageVisible

public boolean isPackageVisible(java.lang.reflect.Field field)
Returns true if the given field is not null and is package visible.


isPackageVisible

public boolean isPackageVisible(java.lang.reflect.Method method)
Returns true if the given method 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.


newInstance

public java.lang.Object newInstance(java.lang.Class 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.

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.

newInstance

public java.lang.Object newInstance(java.lang.Class 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 java.lang.Object newInstance(java.lang.Class 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 java.lang.Object newInstance(java.lang.Class 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.

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.

findConstructor

public java.lang.reflect.Constructor findConstructor(java.lang.Class aClass,
                                                     java.lang.Class[] paramTypes)
Returns the constructor of the given class for the specified parameter types of 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