public final class ReflectionUtility extends Object
Copyright 2006 Sans Pareil Technologies, Inc.
| Modifier and Type | Method and Description |
|---|---|
static Collection<String> |
classesInPackage(String packageName)
Retrieve the fully qualified class names for all classes that are part
of the specified package.
|
static Object |
execute(Method method,
Object object,
List<Object> parameters)
Invoke the specified method on the specified object instance using the
specified parameters.
|
static Object |
execute(Object object,
String name,
Object... parameters)
Invoke the specified method on the specified object instance using the
specified parameters.
|
static Method |
fetchAccessor(String property,
Class cls)
Fetches the accessor method for the specified property in
the specified class.
|
static Method |
fetchAccessor(String property,
Object object)
Fetches the accessor method for the specified property in
the specified object.
|
static Constructor |
fetchConstructor(Class cls,
Class... parameterTypes)
Return the
Constructor instance for the specified
class. |
static Constructor |
fetchConstructor(String cls,
Class... parameterTypes)
Return the
Constructor instance for the specified
class. |
static Field |
fetchField(String name,
Class cls)
|
static Field |
fetchField(String name,
Object object)
Return the
Field instance with the specified name
from the object. |
static Map<String,Field> |
fetchFields(Class cls)
Return the declared fields for the specified
Class. |
static Map<String,Field> |
fetchFields(Object object)
Return the declared fields for the specified
object. |
static Method |
fetchMethod(Class cls,
String name,
Class... parameters)
Fetch the
Method defined in the specified class with
the specified name that accepts the specified
parameters. |
static Method |
fetchMethod(Class cls,
String name,
List<Class> parameters)
Fetch the
Method defined in the specified object with
the specified name that accepts the specified
parameters. |
static Method |
fetchMethod(Field field,
String name,
Class... parameters)
Fetch the
Method defined in the specified field with
the specified name that accepts the specified
parameters. |
static Method |
fetchMethod(Object object,
String name,
Class... parameters)
Fetch the
Method defined in the specified object with
the specified name that accepts the specified
parameters. |
static Method |
fetchMethod(Object object,
String name,
List<Class> parameters)
Fetch the
Method defined in the specified object with
the specified name that accepts the specified
parameters. |
static Method |
fetchMutator(String property,
Class cls)
Fetches the mutator method for the specified property in
the specified class.
|
static Method |
fetchMutator(String property,
Object object)
Fetches the mutator method for the specified property in
the specified object.
|
static Object |
fetchObject(String name,
Object object)
Return the
Object instance represented by the
specified name from object. |
static Map<String,Object> |
fetchObjects(Object object)
Return the
Object instances representing the declared
fields for the specified object. |
static PropertyDescriptor |
fetchPropertyDescriptor(String name,
Class cls)
Return the
PropertyDescriptor for the specified
class identified by the specified property name. |
static Map<String,PropertyDescriptor> |
fetchPropertyDescriptors(Class cls)
Return the
PropertyDescriptors for the specified
class. |
static Map<String,PropertyDescriptor> |
fetchPropertyDescriptors(Object object)
Return the
PropertyDescriptors for the specified
object. |
static Collection<String> |
fetchPropertyNames(Class cls)
Return a collection of the property names that are accessible in the
specified class.
|
static Collection<String> |
fetchPropertyNames(Object object)
Return a collection of the property names that are accessible in the
specified object.
|
static boolean |
isChildClass(Class child,
Class parent)
Determine if the specified class is a subclass of the specified parent.
|
static Object |
newInstance(String cls,
Object... parameters)
Create a new instance of the specified class type using the specified
parameters.
|
static void |
setField(Object source,
String field,
Object value)
Set the value of the
Field to the specified
value. |
public static Constructor fetchConstructor(String cls, Class... parameterTypes) throws ClassNotFoundException
Constructor instance for the specified
class.cls - The fully qualified name of the class.parameterTypes - The class type(s) of the constructor parameter(s).null if no matching
constructor is found.ClassNotFoundException - If the specified class could not be located.public static Constructor fetchConstructor(Class cls, Class... parameterTypes)
Constructor instance for the specified
class. Note that this method returns null and does not
throw a NoSuchMethodException.cls - The class whose constructor is to be fetched.parameterTypes - The class type(s) of the constructor parameter(s).null if a constructor cannot be initialised (interface,
enum, primitive, ...), or if a matching constructor does not exist.public static Object newInstance(String cls, Object... parameters) throws ClassNotFoundException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException
Constructor.cls - The fully qualified name of the class to tbe instantiated.parameters - The arguments for the constructor.null if the cls specified represents an
abstract class, or is an interface.ClassNotFoundException - If class is not found.NoSuchMethodException - If method is not found.IllegalArgumentException - If argument is improper.IllegalAccessException - If access protection is in place.InstantiationException - If new instance cannot be created.InvocationTargetException - If invocation semantics are wrong.public static Field fetchField(String name, Object object)
Field instance with the specified name
from the object. Sets the field accessible if
it is not accessible.name - The name of the field to look up.object - The object from which the field is to be retrieved.null
if no matching field is found. Returns null if no
matching field exists. In particular this method does not throw a
NoSuchFieldException.fetchField( String, Class )public static Field fetchField(String name, Class cls)
Field instance with the specified name
from the Class specified. Sets the field accessible if
it is not accessible.name - The name of the field to look up.cls - The class from which the field is to be retrieved.null if no such field exists. This method does not
throw a NoSuchFieldException.public static Object fetchObject(String name, Object object) throws NoSuchFieldException, IllegalAccessException
Object instance represented by the
specified name from object.name - The name of the object to fetch.object - The object from which the field is to be retrieved.NoSuchFieldException - If no field with the specified name
exists in object.IllegalAccessException - If the field cannot be accessed
due to custom security policies.fetchField(java.lang.String, java.lang.Object)public static Map<String,Field> fetchFields(Object object)
object.object - The object whose declared fields are to be
retrieved.key and
the field as valuefetchFields( Class )public static Map<String,Field> fetchFields(Class cls)
Class.cls - The class whose declared fields are to be
retrieved.key and
the field as valuepublic static Map<String,Object> fetchObjects(Object object) throws IllegalAccessException
Object instances representing the declared
fields for the specified object.object - The object for which objects for the declared fields
are to be retrieved.key and
the object as valueIllegalAccessException - If a custom security policy prevents
access to the fields.fetchFields(java.lang.Object)public static void setField(Object source, String field, Object value) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException
Field to the specified
value.source - The object whose field is to be set.field - The name of the field that is to be set.value - The new value to set for the field.IllegalAccessException - If the underlying field is inaccessible.IllegalArgumentException - If the specified object is not an
instance of the class or interface declaring the underlying field
(or a subclass
or implementor thereof), or if an unwrapping conversion fails.NoSuchFieldException - If no field with the specified name
exists in source.fetchField( String, Object )public static Method fetchMethod(Field field, String name, Class... parameters)
Method defined in the specified field with
the specified name that accepts the specified
parameters. Please note that this method does not throw a
NoSuchMethodException, but returns null
instead.field - The field whose method is to be retrieved.name - The name of the method to retrieve.parameters - The parameters accepted by the method.null.fetchMethod( Class, String, Class[] )public static Method fetchMethod(Object object, String name, Class... parameters)
Method defined in the specified object with
the specified name that accepts the specified
parameters. Please note that this method does not throw a
NoSuchMethodException, but returns null
instead.object - The object whose method is to be retrieved.name - The name of the method to retrieve.parameters - The parameters accepted by the method.null.fetchMethod( Class, String, Class[] )public static Method fetchMethod(Object object, String name, List<Class> parameters)
Method defined in the specified object with
the specified name that accepts the specified
parameters. Please note that this method does not throw a
NoSuchMethodException, but returns null
instead.object - The object whose method is to be retrieved.name - The name of the method to retrieve.parameters - The parameters accepted by the method.null.fetchMethod( Class, String, Class[] )public static Method fetchMethod(Class cls, String name, List<Class> parameters)
Method defined in the specified object with
the specified name that accepts the specified
parameters. Please note that this method does not throw a
NoSuchMethodException, but returns null
instead.cls - The cloass whose method is to be retrieved.name - The name of the method to retrieve.parameters - The parameters accepted by the method.null.fetchMethod( Class, String, Class[] )public static Method fetchMethod(Class cls, String name, Class... parameters)
Method defined in the specified class with
the specified name that accepts the specified
parameters. Please note that this method does not throw a
NoSuchMethodException, but returns null
instead.cls - The cls whose method is to be retrieved.name - The name of the method to retrieve.parameters - The parameters accepted by the method.null
if no such method could be found.public static Method fetchAccessor(String property, Object object) throws IntrospectionException
property - The property whose accessor is to be fetched.object - The object to which the property belongs.IntrospectionException - If errors are encountered while
introspecting the specified class.fetchPropertyNames(java.lang.Object),
fetchAccessor( String, Class )public static Method fetchAccessor(String property, Class cls) throws IntrospectionException
property - The property whose accessor is to be fetched.cls - The class to which the property belongs.IntrospectionException - If errors are encountered while
introspecting the specified class.public static Method fetchMutator(String property, Object object) throws IntrospectionException
property - The property whose mutator is to be fetched.object - The object to which the property belongs.IntrospectionException - If errors are encountered while
introspecting the specified class.fetchPropertyNames(java.lang.Object),
fetchMutator( String, Class )public static Method fetchMutator(String property, Class cls) throws IntrospectionException
property - The property whose mutator is to be fetched.cls - The class to which the property belongs.IntrospectionException - If errors are encountered while
introspecting the specified class.public static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Object object) throws IntrospectionException
PropertyDescriptors for the specified
object.object - The object whose property descriptors are to be
retrieved.key and
property descriptor as value.IntrospectionException - If reflection errors occur.fetchPropertyDescriptors( Class )public static Map<String,PropertyDescriptor> fetchPropertyDescriptors(Class cls) throws IntrospectionException
PropertyDescriptors for the specified
class.cls - The class whose property descriptors are to be
retrieved.key and
property descriptor as value.IntrospectionException - If reflection errors occur.public static PropertyDescriptor fetchPropertyDescriptor(String name, Class cls) throws IntrospectionException
PropertyDescriptor for the specified
class identified by the specified property name.name - The property name to use to look up the descriptor.cls - The class in which the property is declared.null if no such
property is found.IntrospectionException - If reflection errors occur.public static Collection<String> fetchPropertyNames(Object object) throws IntrospectionException
object - The object whose accessible property name are to be
retrieved.IntrospectionException - If reflection errors occur.fetchPropertyNames( Class )public static Collection<String> fetchPropertyNames(Class cls) throws IntrospectionException
cls - The class whose accessible property name are to be
retrieved.IntrospectionException - If reflection errors occur.public static Object execute(Method method, Object object, List<Object> parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
method - The method to invokeobject - The object on which the method is to be executed.parameters - The parameters to specify to the method when executing.IllegalArgumentException - If the arguments are improper.InvocationTargetException - If the method invocation fails.IllegalAccessException - If access protection is in effect.public static Object execute(Object object, String name, Object... parameters) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
object - The object on which the method is to be executed.name - The name of the method to invoke.parameters - The parameters to specify to the method when executing.IllegalArgumentException - If the arguments are improper.InvocationTargetException - If the method invocation fails.IllegalAccessException - If access protection is in effect.fetchMethod(Object, String, Class[])public static boolean isChildClass(Class child, Class parent)
child - The child class to be checked for inheritance.parent - The parent class against which the check is performed.true if a child.public static Collection<String> classesInPackage(String packageName)
packageName - The name of the package.