korat.utils
Class ReflectionUtils

java.lang.Object
  extended by korat.utils.ReflectionUtils

public class ReflectionUtils
extends Object

Author:
Aleksandar Milicevic

Field Summary
(package private) static Map<Class,Map<String,Field>> cache
           
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static Field[] getAllFields(Class clz)
           
private static void getAllFieldsRecursive(Class clz, List<Field> fldLst)
           
static Field[] getAllNonStaticFields(Class clz)
           
static Field[] getDeclaredNonStaticFields(Class clz)
           
static Field getField(Class cls, String fieldName)
          Gets field from class cls, either declared in cls or in one of its superclasses
static Object getFieldValue(Object obj, Field field)
           
static Field getFieldWithAccess(Class cls, String fieldName)
           
static Field getFieldWithAccess(Object obj, String fieldName)
           
static Method getMethod(Class<? extends Object> clz, String methodName, Class[] methodArgs)
          Gets method from class clz or any of its superclasses.
static Object invoke(Object obj, String method, Class[] params, Object[] args)
          Invokes method of a given obj and set of parameters
static void setFieldValue(Object obj, Field field, Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

static Map<Class,Map<String,Field>> cache
Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

getFieldWithAccess

public static Field getFieldWithAccess(Object obj,
                                       String fieldName)

getFieldWithAccess

public static Field getFieldWithAccess(Class cls,
                                       String fieldName)

getField

public static Field getField(Class cls,
                             String fieldName)
Gets field from class cls, either declared in cls or in one of its superclasses

Parameters:
cls - - declaring class of the field
fieldName - - name of the field
Returns:
requested field

getFieldValue

public static Object getFieldValue(Object obj,
                                   Field field)

setFieldValue

public static void setFieldValue(Object obj,
                                 Field field,
                                 Object value)

getAllFieldsRecursive

private static void getAllFieldsRecursive(Class clz,
                                          List<Field> fldLst)

getAllFields

public static Field[] getAllFields(Class clz)

getAllNonStaticFields

public static Field[] getAllNonStaticFields(Class clz)

getDeclaredNonStaticFields

public static Field[] getDeclaredNonStaticFields(Class clz)

getMethod

public static Method getMethod(Class<? extends Object> clz,
                               String methodName,
                               Class[] methodArgs)
                        throws NoSuchMethodException
Gets method from class clz or any of its superclasses. If no method can be found NoSuchMethodException is raised.

Parameters:
clz - - declaring class of the method
methodName - - name of the method
methodArgs - - method arguments
Returns:
requested method
Throws:
NoSuchMethodException

invoke

public static Object invoke(Object obj,
                            String method,
                            Class[] params,
                            Object[] args)

Invokes method of a given obj and set of parameters

Parameters:
obj - - Object for which the method is going to be executed. Special if obj is of type java.lang.Class, method is executed as static method of class given as obj parameter
method - - name of the object
params - - actual parameters for the method
Returns:
- result of the method execution

If there is any problem with method invocation, a RuntimeException is thrown