de.akquinet.jbosscc.needle.reflection
Class ReflectionUtil

java.lang.Object
  extended by de.akquinet.jbosscc.needle.reflection.ReflectionUtil

public final class ReflectionUtil
extends Object


Method Summary
static
<T> T
createInstance(Class<T> clazz, Object... parameter)
           
static Class<?> forName(String className)
          Returns the Class object associated with the class or interface with the given string name.
static Map<Class<? extends Annotation>,List<Field>> getAllAnnotatedFields(Class<?> clazz)
           
static List<Field> getAllFields(Class<?> clazz)
           
static List<Field> getAllFieldsAssinableFrom(Class<?> assinableType, Class<?> clazz)
           
static List<Field> getAllFieldsWithAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
           
static List<Field> getAllFieldsWithAnnotation(Object instance, Class<? extends Annotation> annotation)
           
static Set<Class<?>> getClasses(String... classNames)
           
static Field getField(Class<?> clazz, String fieldName)
           
static Object getFieldValue(Object object, Class<?> clazz, String fieldName)
          Get the value of a given field on a given object via reflection.
static Object getFieldValue(Object object, Field field)
          Get the value of a given field on a given object via reflection.
static Object getFieldValue(Object object, String fieldName)
          Get the value of a given field on a given object via reflection.
static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
           
static List<Method> getMethods(Class<?> clazz)
           
static Object invokeMethod(Method method, Object instance, Object... arguments)
           
static Object invokeMethod(Object object, Class<?> clazz, String methodName, Object... arguments)
          Invoke a given method with given arguments on a given object via reflection.
static Object invokeMethod(Object object, String methodName, Object... arguments)
          Invoke a given method with given arguments on a given object via reflection.
static void setField(Field field, Object target, Object value)
           
static void setField(String fieldName, Object target, Object value)
           
static void setFieldValue(Object object, Class<?> clazz, String fieldName, Object value)
          Changing the value of a given field.
static void setFieldValue(Object object, String fieldName, Object value)
          Changing the value of a given field.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAllFieldsWithAnnotation

public static List<Field> getAllFieldsWithAnnotation(Class<?> clazz,
                                                     Class<? extends Annotation> annotation)

getAllAnnotatedFields

public static Map<Class<? extends Annotation>,List<Field>> getAllAnnotatedFields(Class<?> clazz)

getAllFieldsAssinableFrom

public static List<Field> getAllFieldsAssinableFrom(Class<?> assinableType,
                                                    Class<?> clazz)

getAllFieldsWithAnnotation

public static List<Field> getAllFieldsWithAnnotation(Object instance,
                                                     Class<? extends Annotation> annotation)

getAllFields

public static List<Field> getAllFields(Class<?> clazz)

getMethods

public static List<Method> getMethods(Class<?> clazz)
Parameters:
class - object
Returns:
list of method objects
See Also:
Class.getMethods()

setFieldValue

public static void setFieldValue(Object object,
                                 Class<?> clazz,
                                 String fieldName,
                                 Object value)
                          throws NoSuchFieldException
Changing the value of a given field.

Parameters:
object - -- target object of injection
clazz - -- type of argument object
fieldName - -- name of field whose value is to be set
value - -- object that is injected
Throws:
NoSuchFieldException

setFieldValue

public static void setFieldValue(Object object,
                                 String fieldName,
                                 Object value)
Changing the value of a given field.

Parameters:
object - -- target object of injection
fieldName - -- name of field whose value is to be set
value - -- object that is injected

getFieldValue

public static Object getFieldValue(Object object,
                                   Class<?> clazz,
                                   String fieldName)
Get the value of a given field on a given object via reflection.

Parameters:
object - -- target object of field access
clazz - -- type of argument object
fieldName - -- name of the field
Returns:
-- the value of the represented field in object; primitive values are wrapped in an appropriate object before being returned

getFieldValue

public static Object getFieldValue(Object object,
                                   Field field)
Get the value of a given field on a given object via reflection.

Parameters:
object - -- target object of field access
field - -- target field
Returns:
-- the value of the represented field in object; primitive values are wrapped in an appropriate object before being returned

getFieldValue

public static Object getFieldValue(Object object,
                                   String fieldName)
Get the value of a given field on a given object via reflection.

Parameters:
object - -- target object of field access
fieldName - -- name of the field
Returns:
-- the value of the represented field in object; primitive values are wrapped in an appropriate object before being returned

invokeMethod

public static Object invokeMethod(Object object,
                                  Class<?> clazz,
                                  String methodName,
                                  Object... arguments)
                           throws Exception
Invoke a given method with given arguments on a given object via reflection.

Parameters:
object - -- target object of invocation
clazz - -- type of argument object
methodName - -- name of method to be invoked
arguments - -- arguments for method invocation
Returns:
-- method object to which invocation is actually dispatched
Throws:
Exception - - operation exception

invokeMethod

public static Object invokeMethod(Method method,
                                  Object instance,
                                  Object... arguments)
                           throws Exception
Throws:
Exception

getMethod

public static Method getMethod(Class<?> clazz,
                               String methodName,
                               Class<?>... parameterTypes)
                        throws NoSuchMethodException
Throws:
NoSuchMethodException

invokeMethod

public static Object invokeMethod(Object object,
                                  String methodName,
                                  Object... arguments)
                           throws Exception
Invoke a given method with given arguments on a given object via reflection.

Parameters:
object - -- target object of invocation
methodName - -- name of method to be invoked
arguments - -- arguments for method invocation
Returns:
-- method object to which invocation is actually dispatched
Throws:
Exception

getClasses

public static Set<Class<?>> getClasses(String... classNames)

forName

public static Class<?> forName(String className)
Returns the Class object associated with the class or interface with the given string name.

Parameters:
className - the fully qualified name of the desired class.
Returns:
Class or null

setField

public static void setField(Field field,
                            Object target,
                            Object value)
                     throws Exception
Throws:
Exception

setField

public static void setField(String fieldName,
                            Object target,
                            Object value)
                     throws Exception
Throws:
Exception

getField

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

createInstance

public static <T> T createInstance(Class<T> clazz,
                                   Object... parameter)
                        throws Exception
Throws:
Exception


Copyright © 2013 akquinet AG. All Rights Reserved.