org.infinispan.util
Class ReflectionUtil

java.lang.Object
  extended by org.infinispan.util.ReflectionUtil

public class ReflectionUtil
extends Object

Basic reflection utilities to enhance what the JDK provides.

Since:
4.0
Author:
Manik Surtani (manik@jboss.org)

Field Summary
static Class<?>[] EMPTY_CLASS_ARRAY
           
 
Constructor Summary
ReflectionUtil()
           
 
Method Summary
static String extractFieldName(String setterOrGetter)
           
static Method findGetterForField(Class<?> c, String fieldName)
           
static Method findMethod(Class<?> type, String methodName)
           
static Method findMethod(Class<?> type, String methodName, Class<?>[] parameters)
           
static Method findMethod(Class<?> type, String methodName, String[] parameters)
           
static Method findSetterForField(Class<?> c, String fieldName)
           
static List<Method> getAllMethods(Class<?> c, Class<? extends Annotation> annotationType)
          Returns a set of Methods that contain the given method annotation.
static List<Method> getAllMethodsShallow(Class<?> c, Class<? extends Annotation> annotationType)
          Returns a set of Methods that contain the given method annotation.
static List<Field> getAnnotatedFields(Class<?> c, Class<? extends Annotation> annotationType)
           
static
<T extends Annotation>
T
getAnnotation(Class<?> clazz, Class<T> ann)
          Inspects the class passed in for the class level annotation specified.
static Class<?> getClassForName(String name, ClassLoader cl)
           
static Field getField(String fieldName, Class<?> objectClass)
           
static List<Field> getFields(Class<?> c, Class<?> type)
           
static Object getValue(Object instance, String fieldName)
          Retrieves the value of a field of an object instance via reflection
static Object invokeAccessibly(Object instance, Method method, Object[] parameters)
          Invokes a method using reflection, in an accessible manner (by using AccessibleObject.setAccessible(boolean)
static boolean isAnnotationPresent(Class<?> clazz, Class<? extends Annotation> annotation)
          Tests whether an annotation is present on a class.
static boolean isGetMethod(Method method)
           
static boolean isIsMethod(Method method)
           
static boolean isSetMethod(Method method)
           
static void setValue(Object instance, String fieldName, Object value)
           
static Class<?>[] toClassArray(String[] typeList)
           
static String[] toStringArray(Class<?>[] classes)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_CLASS_ARRAY

public static final Class<?>[] EMPTY_CLASS_ARRAY
Constructor Detail

ReflectionUtil

public ReflectionUtil()
Method Detail

getAllMethods

public static List<Method> getAllMethods(Class<?> c,
                                         Class<? extends Annotation> annotationType)
Returns a set of Methods that contain the given method annotation. This includes all public, protected, package and private methods, as well as those of superclasses. Note that this does *not* include overridden methods.

Parameters:
c - class to inspect
annotationType - the type of annotation to look for
Returns:
List of Method objects that require injection.

getAllMethodsShallow

public static List<Method> getAllMethodsShallow(Class<?> c,
                                                Class<? extends Annotation> annotationType)
Returns a set of Methods that contain the given method annotation. This includes all public, protected, package and private methods, but not those of superclasses and interfaces.

Parameters:
c - class to inspect
annotationType - the type of annotation to look for
Returns:
List of Method objects that require injection.

getAnnotatedFields

public static List<Field> getAnnotatedFields(Class<?> c,
                                             Class<? extends Annotation> annotationType)

getFields

public static List<Field> getFields(Class<?> c,
                                    Class<?> type)

findMethod

public static Method findMethod(Class<?> type,
                                String methodName)

findMethod

public static Method findMethod(Class<?> type,
                                String methodName,
                                Class<?>[] parameters)

findMethod

public static Method findMethod(Class<?> type,
                                String methodName,
                                String[] parameters)
                         throws ClassNotFoundException
Throws:
ClassNotFoundException

setValue

public static void setValue(Object instance,
                            String fieldName,
                            Object value)

invokeAccessibly

public static Object invokeAccessibly(Object instance,
                                      Method method,
                                      Object[] parameters)
Invokes a method using reflection, in an accessible manner (by using AccessibleObject.setAccessible(boolean)

Parameters:
instance - instance on which to execute the method
method - method to execute
parameters - parameters

findGetterForField

public static Method findGetterForField(Class<?> c,
                                        String fieldName)

findSetterForField

public static Method findSetterForField(Class<?> c,
                                        String fieldName)

extractFieldName

public static String extractFieldName(String setterOrGetter)

getValue

public static Object getValue(Object instance,
                              String fieldName)
Retrieves the value of a field of an object instance via reflection

Parameters:
instance - to inspect
fieldName - name of field to retrieve
Returns:
a value

getAnnotation

public static <T extends Annotation> T getAnnotation(Class<?> clazz,
                                                     Class<T> ann)
Inspects the class passed in for the class level annotation specified. If the annotation is not available, this method recursively inspects superclasses and interfaces until it finds the required annotation.

Returns null if the annotation cannot be found.

Parameters:
clazz - class to inspect
ann - annotation to search for. Must be a class-level annotation.
Returns:
the annotation instance, or null

isAnnotationPresent

public static boolean isAnnotationPresent(Class<?> clazz,
                                          Class<? extends Annotation> annotation)
Tests whether an annotation is present on a class. The order tested is:

Parameters:
clazz - class to test
annotation - annotation to look for
Returns:
true if the annotation is found, false otherwise

isSetMethod

public static boolean isSetMethod(Method method)

isGetMethod

public static boolean isGetMethod(Method method)

isIsMethod

public static boolean isIsMethod(Method method)

toClassArray

public static Class<?>[] toClassArray(String[] typeList)
                               throws ClassNotFoundException
Throws:
ClassNotFoundException

getClassForName

public static Class<?> getClassForName(String name,
                                       ClassLoader cl)
                                throws ClassNotFoundException
Throws:
ClassNotFoundException

toStringArray

public static String[] toStringArray(Class<?>[] classes)

getField

public static Field getField(String fieldName,
                             Class<?> objectClass)

-->

Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.