com.senn.magic.util
Class ReflectionUtils

java.lang.Object
  extended by com.senn.magic.util.ReflectionUtils

public final class ReflectionUtils
extends Object

Useful class for Reflection actions.

Since:
3.3
Author:
Bart Thierens
Last modification: 07/05/2010

Constructor Summary
ReflectionUtils()
           
 
Method Summary
static Class getPrimitiveClass(Class wrapper)
          Returns a primitive class for the provided wrapper.
static Object invoke(Class clazz, String method, Object... args)
          Invoke a method of a class by providing the class, method and an array of arguments.
static Object invoke(Object instance, String method, Object... args)
          Invoke a method of a class by providing the object, method and an array of arguments.
static Object invokeUnaccessible(Class clazz, String method, Object... args)
          Invoke a method ,that normally is not visible for the calling class, of a class by providing the class, method and an array of arguments.
static Object invokeUnaccessible(Object instance, String method, Object... args)
          Invoke a method ,that normally is not visible for the calling class, of a class by providing the object, method and an array of arguments.
static boolean isPrimitive(Class clazz)
          Checks whether a Class is a primitive datatype.
static boolean isPrimitiveWrapper(Class clazz)
          Checks whether a Class is a Wrapper-class of a primitive datatype.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

invokeUnaccessible

public static Object invokeUnaccessible(Class clazz,
                                        String method,
                                        Object... args)
                                 throws Exception
Invoke a method ,that normally is not visible for the calling class, of a class by providing the class, method and an array of arguments. Be aware that calling private or protected methods can be dangerous!

As all the arguments in the array are Objects, primitives were wrapped by Java into their Wrapper-class.
Whether the method to invoke takes primitives or wrappers as arguments is decided on the fly for every argument!

Parameters:
clazz - the class that contains the method and will be instantiated
method - the name of the method (case sensitive!). The method must be marked public!
args - the arguments of the method an array of object
Returns:
Object - the return value as an object. null is returned when the method has no return value.
Throws:
Exception

invokeUnaccessible

public static Object invokeUnaccessible(Object instance,
                                        String method,
                                        Object... args)
                                 throws Exception
Invoke a method ,that normally is not visible for the calling class, of a class by providing the object, method and an array of arguments. Be aware that calling private or protected methods can be dangerous!

As all the arguments in the array are Objects, primitives were wrapped by Java into their Wrapper-class.
Whether the method to invoke takes primitives or wrappers as arguments is decided on the fly for every argument!

Parameters:
instance - the object that contains the method, leave null for static methods
method - the name of the method (case sensitive!). The method must be marked public!
args - the arguments of the method an array of object
Returns:
Object - the return value as an object. null is returned when the method has no return value.
Throws:
Exception

invoke

public static Object invoke(Class clazz,
                            String method,
                            Object... args)
                     throws Exception
Invoke a method of a class by providing the class, method and an array of arguments.

As all the arguments in the array are Objects, primitives were wrapped by Java into their Wrapper-class.
Whether the method to invoke takes primitives or wrappers as arguments is decided on the fly for every argument!

Parameters:
clazz - the class that contains the method and will be instantiated
method - the name of the method (case sensitive!). The method must be marked public!
args - the arguments of the method an array of object
Returns:
Object - the return value as an object. null is returned when the method has no return value.
Throws:
Exception

invoke

public static Object invoke(Object instance,
                            String method,
                            Object... args)
                     throws Exception
Invoke a method of a class by providing the object, method and an array of arguments.

As all the arguments in the array are Objects, primitives were wrapped by Java into their Wrapper-class.
Whether the method to invoke takes primitives or wrappers as arguments is decided on the fly for every argument!

Parameters:
instance - the object that contains the method, leave null for static methods
method - the name of the method (case sensitive!). The method must be marked public!
args - the arguments of the method an array of object
Returns:
Object - the return value as an object. null is returned when the method has no return value.
Throws:
Exception

getPrimitiveClass

public static Class getPrimitiveClass(Class wrapper)
Returns a primitive class for the provided wrapper.

Parameters:
wrapper - the Wrapper class
Returns:
Class - the matching primitive class, or null if the class is not a Wrapper at all

isPrimitiveWrapper

public static boolean isPrimitiveWrapper(Class clazz)
Checks whether a Class is a Wrapper-class of a primitive datatype.

Parameters:
clazz - the class to check
Returns:
boolean - true if it's a wrapper, false if it's not

isPrimitive

public static boolean isPrimitive(Class clazz)
Checks whether a Class is a primitive datatype.

Parameters:
clazz - the class to check
Returns:
boolean - true if it's a primitive, false if it's not