jodd.util
Class ReflectUtil

java.lang.Object
  extended by jodd.util.ReflectUtil

public class ReflectUtil
extends java.lang.Object

Various java.lang.reflect utilities.


Field Summary
static java.lang.String METHOD_GET_PREFIX
           
static java.lang.String METHOD_IS_PREFIX
           
static java.lang.String METHOD_SET_PREFIX
           
static java.lang.Object[] NO_ARGUMENTS
          an empty object array
static java.lang.Class[] NO_PARAMETERS
          an empty class array
static java.lang.reflect.Type[] NO_TYPES
          an empty object array
 
Constructor Summary
ReflectUtil()
           
 
Method Summary
static boolean compareDeclarations(java.lang.reflect.Method first, java.lang.reflect.Method second)
          Compares method declarations: signature and return types.
static boolean compareParameters(java.lang.Class[] first, java.lang.Class[] second)
          Compares method or ctor parameters.
static boolean compareSignatures(java.lang.reflect.Constructor first, java.lang.reflect.Constructor second)
          Compares constructor signatures: names and parameters.
static boolean compareSignatures(java.lang.reflect.Field first, java.lang.reflect.Field second)
           
static boolean compareSignatures(java.lang.reflect.Method first, java.lang.reflect.Method second)
          Compares method signatures: names and parameters.
static java.lang.reflect.Method findDeclaredMethod(java.lang.Class c, java.lang.String methodName)
           
static java.lang.reflect.Method findMethod(java.lang.Class c, java.lang.String methodName)
          Returns method from an object, matched by name.
static void forceAccess(java.lang.reflect.AccessibleObject accObject)
          Suppress access check against a reflection object.
static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz)
           
static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz, java.lang.Class limit)
           
static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
          Returns array of all methods that are accessible from given class.
static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz, java.lang.Class limit)
          Returns array of all methods that are accessible from given class, upto limit (usually Object.class).
static java.lang.String getBeanPropertyGetterName(java.lang.reflect.Method method)
          Returns beans property getter name or null if method is not a real getter.
static java.lang.String getBeanPropertySetterName(java.lang.reflect.Method method)
          Returns beans property setter name or null if method is not a real setter.
static java.lang.Class[] getClasses(java.lang.Object... objects)
          Returns classes from array of specified objects.
static java.lang.Class getComponentType(java.lang.reflect.Type type)
          Returns component type of the given type.
static java.lang.Class getComponentType(java.lang.reflect.Type type, int index)
          Returns the component type of the given type.
static java.lang.Class getGenericSupertype(java.lang.Class type)
           
static java.lang.Class getGenericSupertype(java.lang.Class type, int index)
           
static java.lang.reflect.Method getMethod0(java.lang.Class c, java.lang.String name, java.lang.Class... parameterTypes)
          Invokes private Class.getMethod0() without throwing NoSuchMethodException.
static java.lang.Class[] getSuperclasses(java.lang.Class type)
          Returns all superclasses.
static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz)
           
static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz, java.lang.Class limit)
           
static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz)
           
static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz, java.lang.Class limit)
          Returns a Method array of the methods to which instances of the specified respond except for those methods defined in the class specified by limit or any of its superclasses.
static java.lang.Object invoke(java.lang.Class c, java.lang.Object obj, java.lang.String method, java.lang.Class[] paramClasses, java.lang.Object[] params)
          Invokes accessible method of an object.
static java.lang.Object invoke(java.lang.Class c, java.lang.Object obj, java.lang.String method, java.lang.Object[] params)
           
static java.lang.Object invoke(java.lang.Object obj, java.lang.String method, java.lang.Class[] paramClasses, java.lang.Object[] params)
          Invokes accessible method of an object.
static java.lang.Object invoke(java.lang.Object obj, java.lang.String method, java.lang.Object[] params)
          Invokes accessible method of an object without specifying parameter types.
static java.lang.Object invokeDeclared(java.lang.Class c, java.lang.Object obj, java.lang.String method, java.lang.Class[] paramClasses, java.lang.Object[] params)
          Invokes any method of a class, even private ones.
static java.lang.Object invokeDeclared(java.lang.Class c, java.lang.Object obj, java.lang.String method, java.lang.Object[] params)
           
static java.lang.Object invokeDeclared(java.lang.Object obj, java.lang.String method, java.lang.Class[] paramClasses, java.lang.Object[] params)
          Invokes any method of a class suppressing java access checking.
static java.lang.Object invokeDeclared(java.lang.Object obj, java.lang.String method, java.lang.Object[] params)
           
static boolean isAssignableFrom(java.lang.reflect.Member member1, java.lang.reflect.Member member2)
          Returns true if the first member is accessible from second one.
static boolean isBeanProperty(java.lang.reflect.Method method)
          Returns true if method is a bean property.
static boolean isBeanPropertyGetter(java.lang.reflect.Method method)
          Returns true if method is bean getter.
static boolean isBeanPropertySetter(java.lang.reflect.Method method)
          Returns true if method is bean setter.
static boolean isInstanceOf(java.lang.Object o, java.lang.Class target)
          Dynamic version of instanceof.
static boolean isInterfaceImpl(java.lang.Class thisClass, java.lang.Class targetInterface)
          Returns true if provided class is interface implementation.
static boolean isObjectMethod(java.lang.reflect.Method method)
          Returns true if method defined in Object class.
static boolean isPublic(java.lang.Class c)
          Returns true if class is public.
static boolean isPublic(java.lang.reflect.Member member)
          Returns true if class member is public.
static boolean isPublicPublic(java.lang.reflect.Member member)
          Returns true if class member is public and if its declaring class is also public.
static boolean isSubclass(java.lang.Class thisClass, java.lang.Class target)
          Determines if first class match the destination and simulates kind of instanceof.
static boolean isUserDefinedMethod(java.lang.reflect.Method method)
          Returns true if method is user defined and not defined in Object class.
static java.lang.Object newInstance(java.lang.Class type)
          Creates new instances including for common mutable classes that do not have a default constructor.
static java.lang.Object readAnnotationValue(java.lang.annotation.Annotation annotation, java.lang.String name)
          Reads annotation value.
static java.lang.Class toClass(java.lang.reflect.Type type)
          Returns Class for the given type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_PARAMETERS

public static final java.lang.Class[] NO_PARAMETERS
an empty class array


NO_ARGUMENTS

public static final java.lang.Object[] NO_ARGUMENTS
an empty object array


NO_TYPES

public static final java.lang.reflect.Type[] NO_TYPES
an empty object array


METHOD_GET_PREFIX

public static final java.lang.String METHOD_GET_PREFIX
See Also:
Constant Field Values

METHOD_IS_PREFIX

public static final java.lang.String METHOD_IS_PREFIX
See Also:
Constant Field Values

METHOD_SET_PREFIX

public static final java.lang.String METHOD_SET_PREFIX
See Also:
Constant Field Values
Constructor Detail

ReflectUtil

public ReflectUtil()
Method Detail

getMethod0

public static java.lang.reflect.Method getMethod0(java.lang.Class c,
                                                  java.lang.String name,
                                                  java.lang.Class... parameterTypes)
Invokes private Class.getMethod0() without throwing NoSuchMethodException. Returns only public methods or null if method not found. Since no exception is throwing, it works faster.

Parameters:
c - class to inspect
name - name of method to find
parameterTypes - parameter types
Returns:
founded method, or null

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class c,
                                                  java.lang.String methodName)
Returns method from an object, matched by name. This may be considered as a slow operation, since methods are matched one by one. Returns only accessible methods. Only first method is matched.

Parameters:
c - class to examine
methodName - Full name of the method.
Returns:
null if method not found

findDeclaredMethod

public static java.lang.reflect.Method findDeclaredMethod(java.lang.Class c,
                                                          java.lang.String methodName)

getClasses

public static java.lang.Class[] getClasses(java.lang.Object... objects)
Returns classes from array of specified objects.


invoke

public static java.lang.Object invoke(java.lang.Class c,
                                      java.lang.Object obj,
                                      java.lang.String method,
                                      java.lang.Class[] paramClasses,
                                      java.lang.Object[] params)
                               throws java.lang.IllegalAccessException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.reflect.InvocationTargetException
Invokes accessible method of an object.

Parameters:
c - class that contains method
obj - object to execute
method - method to invoke
paramClasses - classes of parameters
params - parameters
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invoke

public static java.lang.Object invoke(java.lang.Object obj,
                                      java.lang.String method,
                                      java.lang.Class[] paramClasses,
                                      java.lang.Object[] params)
                               throws java.lang.IllegalAccessException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.reflect.InvocationTargetException
Invokes accessible method of an object.

Parameters:
obj - object
method - name of the objects method
params - method parameters
paramClasses - method parameter types
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invoke

public static java.lang.Object invoke(java.lang.Object obj,
                                      java.lang.String method,
                                      java.lang.Object[] params)
                               throws java.lang.IllegalAccessException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.reflect.InvocationTargetException
Invokes accessible method of an object without specifying parameter types.

Parameters:
obj - object
method - method of an object
params - method parameters
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invoke

public static java.lang.Object invoke(java.lang.Class c,
                                      java.lang.Object obj,
                                      java.lang.String method,
                                      java.lang.Object[] params)
                               throws java.lang.IllegalAccessException,
                                      java.lang.NoSuchMethodException,
                                      java.lang.reflect.InvocationTargetException
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.Class c,
                                              java.lang.Object obj,
                                              java.lang.String method,
                                              java.lang.Class[] paramClasses,
                                              java.lang.Object[] params)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.reflect.InvocationTargetException
Invokes any method of a class, even private ones.

Parameters:
c - class to examine
obj - object to inspect
method - method to invoke
paramClasses - parameter types
params - parameters
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.Object obj,
                                              java.lang.String method,
                                              java.lang.Class[] paramClasses,
                                              java.lang.Object[] params)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.reflect.InvocationTargetException
Invokes any method of a class suppressing java access checking.

Parameters:
obj - object to inspect
method - method to invoke
paramClasses - parameter types
params - parameters
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.Object obj,
                                              java.lang.String method,
                                              java.lang.Object[] params)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.reflect.InvocationTargetException
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

invokeDeclared

public static java.lang.Object invokeDeclared(java.lang.Class c,
                                              java.lang.Object obj,
                                              java.lang.String method,
                                              java.lang.Object[] params)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.reflect.InvocationTargetException
Throws:
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException

isSubclass

public static boolean isSubclass(java.lang.Class thisClass,
                                 java.lang.Class target)
Determines if first class match the destination and simulates kind of instanceof. All subclasses and interface of first class are examined against second class. Method is not symmetric.


isInterfaceImpl

public static boolean isInterfaceImpl(java.lang.Class thisClass,
                                      java.lang.Class targetInterface)
Returns true if provided class is interface implementation.


isInstanceOf

public static boolean isInstanceOf(java.lang.Object o,
                                   java.lang.Class target)
Dynamic version of instanceof.

Parameters:
o - object to match
target - target class
Returns:
true if object is an instance of target class

getAccessibleMethods

public static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz)
Returns array of all methods that are accessible from given class.

See Also:
getAccessibleMethods(Class, Class)

getAccessibleMethods

public static java.lang.reflect.Method[] getAccessibleMethods(java.lang.Class clazz,
                                                              java.lang.Class limit)
Returns array of all methods that are accessible from given class, upto limit (usually Object.class). Abstract methods are ignored.


getAccessibleFields

public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz)

getAccessibleFields

public static java.lang.reflect.Field[] getAccessibleFields(java.lang.Class clazz,
                                                            java.lang.Class limit)

getSupportedMethods

public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz)

getSupportedMethods

public static java.lang.reflect.Method[] getSupportedMethods(java.lang.Class clazz,
                                                             java.lang.Class limit)
Returns a Method array of the methods to which instances of the specified respond except for those methods defined in the class specified by limit or any of its superclasses. Note that limit is usually used to eliminate them methods defined by java.lang.Object. If limit is null then all methods are returned.


getSupportedFields

public static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz)

getSupportedFields

public static java.lang.reflect.Field[] getSupportedFields(java.lang.Class clazz,
                                                           java.lang.Class limit)

compareDeclarations

public static boolean compareDeclarations(java.lang.reflect.Method first,
                                          java.lang.reflect.Method second)
Compares method declarations: signature and return types.


compareSignatures

public static boolean compareSignatures(java.lang.reflect.Method first,
                                        java.lang.reflect.Method second)
Compares method signatures: names and parameters.


compareSignatures

public static boolean compareSignatures(java.lang.reflect.Constructor first,
                                        java.lang.reflect.Constructor second)
Compares constructor signatures: names and parameters.


compareSignatures

public static boolean compareSignatures(java.lang.reflect.Field first,
                                        java.lang.reflect.Field second)

compareParameters

public static boolean compareParameters(java.lang.Class[] first,
                                        java.lang.Class[] second)
Compares method or ctor parameters.


forceAccess

public static void forceAccess(java.lang.reflect.AccessibleObject accObject)
Suppress access check against a reflection object. SecurityException is silently ignored. Checks first if the object is already accessible.


isPublic

public static boolean isPublic(java.lang.reflect.Member member)
Returns true if class member is public.


isPublicPublic

public static boolean isPublicPublic(java.lang.reflect.Member member)
Returns true if class member is public and if its declaring class is also public.


isPublic

public static boolean isPublic(java.lang.Class c)
Returns true if class is public.


newInstance

public static java.lang.Object newInstance(java.lang.Class type)
                                    throws java.lang.IllegalAccessException,
                                           java.lang.InstantiationException
Creates new instances including for common mutable classes that do not have a default constructor. more user-friendly. It examines if class is a map, list, String, Character, Boolean or a Number. Immutable instances are cached and not created again. Arrays are also created with no elements. Note that this bunch of if blocks is faster then using a HashMap.

Throws:
java.lang.IllegalAccessException
java.lang.InstantiationException

isAssignableFrom

public static boolean isAssignableFrom(java.lang.reflect.Member member1,
                                       java.lang.reflect.Member member2)
Returns true if the first member is accessible from second one.


getSuperclasses

public static java.lang.Class[] getSuperclasses(java.lang.Class type)
Returns all superclasses.


isUserDefinedMethod

public static boolean isUserDefinedMethod(java.lang.reflect.Method method)
Returns true if method is user defined and not defined in Object class.


isObjectMethod

public static boolean isObjectMethod(java.lang.reflect.Method method)
Returns true if method defined in Object class.


isBeanProperty

public static boolean isBeanProperty(java.lang.reflect.Method method)
Returns true if method is a bean property.


isBeanPropertyGetter

public static boolean isBeanPropertyGetter(java.lang.reflect.Method method)
Returns true if method is bean getter.


getBeanPropertyGetterName

public static java.lang.String getBeanPropertyGetterName(java.lang.reflect.Method method)
Returns beans property getter name or null if method is not a real getter.


isBeanPropertySetter

public static boolean isBeanPropertySetter(java.lang.reflect.Method method)
Returns true if method is bean setter.


getBeanPropertySetterName

public static java.lang.String getBeanPropertySetterName(java.lang.reflect.Method method)
Returns beans property setter name or null if method is not a real setter.


getComponentType

public static java.lang.Class getComponentType(java.lang.reflect.Type type)
Returns component type of the given type. For ParameterizedType it returns the last type in array.


getComponentType

public static java.lang.Class getComponentType(java.lang.reflect.Type type,
                                               int index)
Returns the component type of the given type.
For example the following types all have the component-type MyClass:

Parameters:
type - is the type where to get the component type from.
Returns:
the component type of the given type or null if the given type does NOT have a single (component) type.

getGenericSupertype

public static java.lang.Class getGenericSupertype(java.lang.Class type,
                                                  int index)

getGenericSupertype

public static java.lang.Class getGenericSupertype(java.lang.Class type)

toClass

public static java.lang.Class toClass(java.lang.reflect.Type type)
Returns Class for the given type.
Examples:
type getSimpleType(type)
String String
List<String> List
<T extends MyClass> T[] MyClass[]

Parameters:
type - is the type to convert.
Returns:
the closest class representing the given type.

readAnnotationValue

public static java.lang.Object readAnnotationValue(java.lang.annotation.Annotation annotation,
                                                   java.lang.String name)
Reads annotation value. Returns null on error.



Copyright © 2003-2012 Jodd Team