com.ebmwebsourcing.easycommons.lang.reflect
Class ReflectionHelper

java.lang.Object
  extended by com.ebmwebsourcing.easycommons.lang.reflect.ReflectionHelper

public final class ReflectionHelper
extends Object

Author:
Adrien Ruffie - EBM WebSourcing

Method Summary
static Set<Class<?>> findAllImplementedInterfaces(Class<?> clazz)
          Find all implemented interfaces recursively.
static Collection<Method> findMethodsThatReturnType(Class<?> clazz, Class<?> returnType)
           
static Method getDeclaredMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Get a declared method and wrap exceptions into unchecked exceptions.
static Object getFieldValue(Object obj, Field field)
           
static Object getPrivateFieldValue(Class<?> clazz, Object instance, String fieldName)
           
static List<Method> getPublicDeclaredMethods(Class<?> clazz)
          Get all public declared methods and wrap exceptions into unchecked exceptions.
static Method getPublicMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
          Get a public method and wrap exceptions into unchecked exceptions.
static Object invokeMethod(Object obj, Method method, Object... args)
          Invoke a method by reflection.
static Object invokePrivateMethod(Object obj, String methodName, Object... args)
          Invoke a private method by reflection.
static boolean isOrInheritedFrom(Class<?> clazz, String canonicalName)
          Test if the specified class is or inherited from the class with the specified canonical name
static
<T> Class<T>
loadClass(String className, ClassLoader cl)
           
static
<T> T
newInstance(Class<T> clazz, Object... args)
           
static
<T> T
newInstance(String className, ClassLoader cl, Object... args)
           
static void setFieldValue(Object obj, Field field, Object newValue)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

findAllImplementedInterfaces

public static final Set<Class<?>> findAllImplementedInterfaces(Class<?> clazz)
Find all implemented interfaces recursively.

Parameters:
clazz - Class for which we want to know all implemented interfaces.
Returns:
An unmodifiable set of implemented interfaces.

getPublicMethod

public static final Method getPublicMethod(Class<?> clazz,
                                           String methodName,
                                           Class<?>... parameterTypes)
Get a public method and wrap exceptions into unchecked exceptions.

Parameters:
clazz -
methodName -
parameterTypes -
Returns:

getPublicDeclaredMethods

public static final List<Method> getPublicDeclaredMethods(Class<?> clazz)
Get all public declared methods and wrap exceptions into unchecked exceptions.

Parameters:
clazz -
Returns:

getDeclaredMethod

public static final Method getDeclaredMethod(Class<?> clazz,
                                             String methodName,
                                             Class<?>... parameterTypes)
Get a declared method and wrap exceptions into unchecked exceptions.

Parameters:
clazz -
methodName -
parameterTypes -
Returns:

invokeMethod

public static final Object invokeMethod(Object obj,
                                        Method method,
                                        Object... args)
                                 throws InvocationTargetException
Invoke a method by reflection. This is a convenience method, wrapping all related exceptions into an unchecked exception.

Parameters:
obj - Object on which to invoke method.
method - Method to be invoked.
args - Method arguments.
Returns:
Result of invoked method.
Throws:
InvocationTargetException

invokePrivateMethod

public static final Object invokePrivateMethod(Object obj,
                                               String methodName,
                                               Object... args)
                                        throws InvocationTargetException
Invoke a private method by reflection. This method can be used only in experimental projects This is a convenience method, wrapping all related exceptions into an unchecked exception.

Parameters:
obj - Object on which to invoke method.
method - Method to be invoked.
args - Method arguments.
Returns:
Result of invoked method.
Throws:
InvocationTargetException

newInstance

public static final <T> T newInstance(Class<T> clazz,
                                      Object... args)
                           throws InvocationTargetException
Throws:
InvocationTargetException

newInstance

public static final <T> T newInstance(String className,
                                      ClassLoader cl,
                                      Object... args)
                           throws InvocationTargetException
Throws:
InvocationTargetException

loadClass

public static final <T> Class<T> loadClass(String className,
                                           ClassLoader cl)

findMethodsThatReturnType

public static final Collection<Method> findMethodsThatReturnType(Class<?> clazz,
                                                                 Class<?> returnType)

getPrivateFieldValue

public static final Object getPrivateFieldValue(Class<?> clazz,
                                                Object instance,
                                                String fieldName)

getFieldValue

public static final Object getFieldValue(Object obj,
                                         Field field)

setFieldValue

public static final void setFieldValue(Object obj,
                                       Field field,
                                       Object newValue)

isOrInheritedFrom

public static final boolean isOrInheritedFrom(Class<?> clazz,
                                              String canonicalName)
Test if the specified class is or inherited from the class with the specified canonical name

Parameters:
clazz - the class to check
superClassName - the class name to look for
Returns:
true if the


Copyright © 2012 Petals Link. All Rights Reserved.