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(Class<?> clazz, Object obj, String methodName, Object[] args, Class<?>[] types)
          Invoke a private method by reflection.
static Object invokePrivateMethod(Object obj, String methodName, Object... args)
          Deprecated. Does not work correctly with private methods having parameter as List. Replaced by #invokePrivateMethod(Object, String, Object[], Object[])
static Object invokePrivateMethod(Object obj, String methodName, Object[] args, Class<?>[] types)
          Invoke a private method of an object instance, 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)
           
static void setPrivateField(Object obj, String fieldName, Object value)
           
 
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

@Deprecated
public static final Object invokePrivateMethod(Object obj,
                                                          String methodName,
                                                          Object... args)
                                        throws InvocationTargetException
Deprecated. Does not work correctly with private methods having parameter as List. Replaced by #invokePrivateMethod(Object, String, Object[], Object[])

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

invokePrivateMethod

public static final Object invokePrivateMethod(Object obj,
                                               String methodName,
                                               Object[] args,
                                               Class<?>[] types)
                                        throws InvocationTargetException
Invoke a private method of an object instance, 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.
types - Method argument types.
Returns:
Result of invoked method.
Throws:
InvocationTargetException

invokePrivateMethod

public static final Object invokePrivateMethod(Class<?> clazz,
                                               Object obj,
                                               String methodName,
                                               Object[] args,
                                               Class<?>[] types)
                                        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:
clazz - The class of the object obj, can be null if the private method is not static.
obj - Object on which to invoke method. Is null if the private method to invoke is static.
method - Method to be invoked.
args - Method arguments.
types - Method argument types.
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)

setPrivateField

public static void setPrivateField(Object obj,
                                   String fieldName,
                                   Object value)
                            throws IllegalArgumentException
Throws:
IllegalArgumentException

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 © 2013 Petals Link. All rights reserved.