org.ijsberg.iglu.util.reflection
Class ReflectionSupport

java.lang.Object
  extended by org.ijsberg.iglu.util.reflection.ReflectionSupport

public class ReflectionSupport
extends Object

Helper class containing methods to be used to investigate method declarations, invoke methods, instantiate or clone objects.


Constructor Summary
ReflectionSupport()
           
 
Method Summary
static ArrayList<Class<?>> getAllSuperClassesFromClass(Class<?> clasz)
           
static ArrayList<Class<?>> getInterfacesForClass(Class<?> clasz)
           
static Set<Method> getMethodsByName(Class<?> clasz, String methodName, int requiredNrofParameters)
           
static Object instantiateClass(Class<?> clasz, Object... initArgs)
          Instantiates a class by invoking a constructor with the given init parameters.
static Object instantiateClass(ClassLoader classloader, String className, Object... initArgs)
          Instantiates a class by invoking a constructor with certain init parameters.
static Object instantiateClass(String className)
          Instantiates a class by its default constructor.
static Object instantiateClass(String className, Object... initArgs)
          Instantiates a class by invoking a constructor with the given init parameters.
static Object invokeMethod(Object impl, String methodName, Object... arguments)
          Arguments do not have to match exactly; they will be converted if possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionSupport

public ReflectionSupport()
Method Detail

getAllSuperClassesFromClass

public static ArrayList<Class<?>> getAllSuperClassesFromClass(Class<?> clasz)
Parameters:
clasz -
Returns:
a list of all classes the given class extends

getInterfacesForClass

public static ArrayList<Class<?>> getInterfacesForClass(Class<?> clasz)
Parameters:
clasz -
Returns:
all interfaces the given class implements directly or implicitly

invokeMethod

public static Object invokeMethod(Object impl,
                                  String methodName,
                                  Object... arguments)
                           throws NoSuchMethodException,
                                  InvocationTargetException
Arguments do not have to match exactly; they will be converted if possible.

Parameters:
impl - the object on which the method is invoked
methodName - name of method to be invoked
arguments - zero or more arguments
Returns:
whatever the method returns
Throws:
InvocationTargetException - if the invoked method throws
NoSuchMethodException - if no suitable method is found

instantiateClass

public static Object instantiateClass(String className)
                               throws InstantiationException
Instantiates a class by its default constructor.

Parameters:
className -
Returns:
Throws:
IllegalAccessException
InstantiationException

instantiateClass

public static Object instantiateClass(String className,
                                      Object... initArgs)
                               throws InstantiationException
Instantiates a class by invoking a constructor with the given init parameters.

Parameters:
className -
initArgs -
Returns:
Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException

instantiateClass

public static Object instantiateClass(ClassLoader classloader,
                                      String className,
                                      Object... initArgs)
                               throws InstantiationException
Instantiates a class by invoking a constructor with certain init parameters. Uses classloader to load class if class has not been loaded previously.

Parameters:
classloader -
className -
initArgs -
Returns:
Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException

instantiateClass

public static Object instantiateClass(Class<?> clasz,
                                      Object... initArgs)
                               throws InstantiationException
Instantiates a class by invoking a constructor with the given init parameters.

Parameters:
clasz -
initArgs -
Returns:
Throws:
InstantiationException

getMethodsByName

public static Set<Method> getMethodsByName(Class<?> clasz,
                                           String methodName,
                                           int requiredNrofParameters)
Parameters:
clasz -
methodName -
requiredNrofParameters -
Returns:
a set of methods with the given name and number of parameters


Copyright © 2011. All Rights Reserved.