EchoPoint API - 3.0.0b5
App Webcontainer

echopoint.util
Class ReflectionKit

java.lang.Object
  extended by echopoint.util.ReflectionKit

public class ReflectionKit
extends Object

ReflectionKit provides methods that help when using reflection on Java code.


Nested Class Summary
static class ReflectionKit.ClassConstructorComparator
          A Comparator that can be used when comparing and sorting Constructor objects by most specific declaring class order, then followed by Constructor name and parameters.
static class ReflectionKit.ClassDerivationComparator
          A Comparator that can be used when comparing and sorting Class objects by most specific class order.
static class ReflectionKit.ClassFieldComparator
          A Comparator that can be used when comparing and sorting Field objects by most specific declaring class order, then followed by Field name and parameters.
static class ReflectionKit.ClassMemberComparator
          A Comparator that can be used when comparing and sorting Member objects by most specific declaring class order, then followed by member name.
static class ReflectionKit.ClassMethodComparator
          A Comparator that can be used when comparing and sorting Method objects by most specific declaring class order, then followed by method name and parameters.
static class ReflectionKit.ClassNameComparator
          A Comparator that can be used when comparing and sorting Class objects by class name.
static class ReflectionKit.FieldClassComparator
          A Comparator that can be used when comparing and sorting Field objects by name, modifier and class order.
static class ReflectionKit.MemberClassComparator
          A Comparator that can be used when comparing and sorting Member objects by name, modifier and finally declaring class order.
static class ReflectionKit.MethodClassComparator
          A Comparator that can be used when comparing and sorting Method objects by name, modifier and class order.
static interface ReflectionKit.MethodSearchCriteria
          MethodSearchCriteria is an interface used to determine if a method matches some search criteria.
 
Method Summary
static String decapitalize(String name)
          Takes a bean property method name and removes any 'get'/'is'/'set' at the front and then decapitalizes the rest of the name according to the Java Bean Spec.
static Method[] getAllBeanGetterMethods(Class targetClass, Class stopClass)
          Returns an array containing getter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.
static Method[] getAllBeanMethods(Class targetClass, Class stopClass)
          Returns an array containing getter and setter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.
static Method[] getAllBeanSetterMethods(Class targetClass, Class stopClass)
          Returns an array containing setter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.
static Method[] getAllDeclaredMethods(Class targetClass)
          Shorthand method for ReflectionKit.getAllMethods(targetClass,Object.class);
static Method[] getAllDeclaredMethods(Class targetClass, Class stopClass)
          Returns an array containing Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.
static Method[] getAllPublicMethods(Class targetClass, Class stopClass)
          Returns an array containing Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.
static Method getBeanGetter(Method beanSetter)
          Returns the getter method for a given setter method.
static Class[] getClassHierarchy(Class targetClass)
           
static Class[] getClassHierarchy(Class targetClass, Class stopClass)
          Returns an array containing their hierarchy of class objects for the given class object.
static Method[] getMethods(Class targetClass, Class stopClass, ReflectionKit.MethodSearchCriteria methodSearchCriteria)
          This method will returns member methods of the targetClass that meet a specified search criteria.
static boolean hasMethod(String methodName, Class[] paramTypes, Class returnType, Object targetObj)
          This method can be called to determine whether an object has the specific named method.
static Object invokeIfPresent(String methodName, Class[] paramTypes, Class returnType, Object targetObj, Object[] params)
          This method can be called to invoke a specific named method of an object.
static boolean isGetter(Method method)
          Returns true if the method is in fact a Java Bean getter method. ie is starts with 'get' or 'is' and takes no parameters and returns a value and is not static.
static boolean isSetter(Method method)
          Returns true if the method is in fact a Java Bean setter method. ie is starts with 'set', takes one parameter and has a return value of void and is not static.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isGetter

public static boolean isGetter(Method method)
Returns true if the method is in fact a Java Bean getter method. ie is starts with 'get' or 'is' and takes no parameters and returns a value and is not static.

Note that it does NOT check for public access because its valid to have a getter that isnt public.

Parameters:
method - - the method to examine
Returns:
true if the method is a Java Bean getter.

isSetter

public static boolean isSetter(Method method)
Returns true if the method is in fact a Java Bean setter method. ie is starts with 'set', takes one parameter and has a return value of void and is not static.

Note that it does NOT check for public access because its valid to have a setter that isnt public.

Parameters:
method - - the method to examine
Returns:
true if the method is a Java Bean setter.

decapitalize

public static String decapitalize(String name)
Takes a bean property method name and removes any 'get'/'is'/'set' at the front and then decapitalizes the rest of the name according to the Java Bean Spec.

Parameters:
name - - the name of the method or field name to change
Returns:
- the decapitalized name

getMethods

public static Method[] getMethods(Class targetClass,
                                  Class stopClass,
                                  ReflectionKit.MethodSearchCriteria methodSearchCriteria)
This method will returns member methods of the targetClass that meet a specified search criteria. The targetClass will be search up to and including the stopClass.

The results are sorted by method name within derived class. The most specific class methods will be returned first.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
methodSearchCriteria - - the MethodSearchCirteria to use
Returns:
and array of Methods or Method[0] if there are none that match the criteria
See Also:
ReflectionKit.MethodSearchCriteria

getAllDeclaredMethods

public static Method[] getAllDeclaredMethods(Class targetClass,
                                             Class stopClass)
Returns an array containing Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.

All public, protected, default (package) access, and private methods are returned.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
Returns:
and array of Methods or Method[0] if there are none

getAllDeclaredMethods

public static Method[] getAllDeclaredMethods(Class targetClass)
Shorthand method for ReflectionKit.getAllMethods(targetClass,Object.class);

See Also:
getAllDeclaredMethods(Class, Class)

getAllPublicMethods

public static Method[] getAllPublicMethods(Class targetClass,
                                           Class stopClass)
Returns an array containing Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.

Only public methods are returned.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
Returns:
and array of Methods or Method[0] if there are none

getAllBeanGetterMethods

public static Method[] getAllBeanGetterMethods(Class targetClass,
                                               Class stopClass)
Returns an array containing getter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.

Only methods matching the Java Bean specifiction for a getter method are returned.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

The methods are returned in method name order using the MethodComparator comparator.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
Returns:
and array of Methods or Method[0] if there are none

getAllBeanSetterMethods

public static Method[] getAllBeanSetterMethods(Class targetClass,
                                               Class stopClass)
Returns an array containing setter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.

Only methods matching the Java Bean specifiction for a setter method are returned.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
Returns:
and array of Methods or Method[0] if there are none

getAllBeanMethods

public static Method[] getAllBeanMethods(Class targetClass,
                                         Class stopClass)
Returns an array containing getter and setter Method objects reflecting all the member methods of the class or interface represented by the targetClass object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces up until stopClass.

Only methods matching the Java Bean specifiction for a getter or setter method are returned.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to check for methods
stopClass - - the supertype to stop at.
Returns:
and array of Methods or Method[0] if there are none

getBeanGetter

public static Method getBeanGetter(Method beanSetter)
Returns the getter method for a given setter method. It is determined by getting the best matching property name as well as matching return type to setter parameter type.

Parameters:
beanSetter - - a bean setter method of class in question
Returns:
the bean getter method or null if it cant be found
Throws:
IllegalArgumentException - - if the method passed in is null

getClassHierarchy

public static Class[] getClassHierarchy(Class targetClass)
See Also:
getClassHierarchy(Class, Class)

getClassHierarchy

public static Class[] getClassHierarchy(Class targetClass,
                                        Class stopClass)
Returns an array containing their hierarchy of class objects for the given class object. The array is sorted in most specific class order, ie the first class is the class object itself, followed by its super class, all the way down to stopClass.

If stopClass is not a superclass or superinterface of targetClass, then Object.class is used.

Parameters:
targetClass - - the class to start the hierarchial search from
stopClass - - the supertype to stop at.
Returns:
and array of Class or Class[0] if there are none

hasMethod

public static boolean hasMethod(String methodName,
                                Class[] paramTypes,
                                Class returnType,
                                Object targetObj)
This method can be called to determine whether an object has the specific named method. This is useful in allowing you to conditionally call a method that may be present in a future version of a class. For example when you code is built to Java 1.3, you could call a Java 1.4 API conditionally if its present.

Parameters:
methodName - - the name of the method to invoke
paramTypes - - the types of the methods parameters if this is null then it is deemed Class[0]
returnType - - the methods return type, if this is null then it is deemed Void.TYPE
targetObj - - the object to invoke the method on
Returns:
- the return value of the method if any or null.

invokeIfPresent

public static Object invokeIfPresent(String methodName,
                                     Class[] paramTypes,
                                     Class returnType,
                                     Object targetObj,
                                     Object[] params)
This method can be called to invoke a specific named method of an object. This is useful in allowing you to conditionally call a method that may be present in a future version of a class. For example when you code is built to Java 1.3, you could call a Java 1.4 API conditionally if its present.

Parameters:
methodName - - the name of the method to invoke
paramTypes - - the types of the methods parameters if this is null then it is deemed Class[0]
returnType - - the methods return type, if this is null then it is deemed Void.TYPE
targetObj - - the object to invoke the method on
params - - the parameters for the method if this is null then it is deemed Object[0]
Returns:
- the return value of the method if any or null.

EchoPoint API - 3.0.0b5
App Webcontainer