net.sf.jsefa.common.util
Class ReflectionUtil

java.lang.Object
  extended by net.sf.jsefa.common.util.ReflectionUtil

public final class ReflectionUtil
extends Object

Utility class for reflection.

Author:
Norman Lahme-Huetig

Method Summary
static
<T> T
callMethod(Object object, Method method, Object... parameters)
          Calls the given method with the given parameters on the given object.
static
<T> T
callMethod(Object object, String methodName)
          Calls the parameterless method with the given name on the given object.
static
<T> T
createInstance(Class<T> objectType)
          Creates a new instance of the given object type using the default constructor.
static Class<?> getActualTypeParameter(Field field, int index)
          Returns the actual type parameter with the given index of the given field or null if it does not exist.
static Collection<Field> getAllFields(Class<?> objectType)
          Returns the fields of the given class.
static Class<?> getClass(String clazz)
          Returns a Class object which represents the class with the given name.
static Constructor<?> getDefaultConstructor(Class<?> objectType)
          Returns the default constructor of the given object type.
static Method getMethod(Class<?> objectType, String methodName, Class<?>... parameterTypes)
          Returns the method on the objectType with the given name and given parameter types.
static
<T> T
getNearest(Class<?> objectType, Map<Class<?>,T> map)
          Returns the value mapped to the given object type or one of its supertypes (classes or interfaces) using a nearest match algorithm.
static List<Class<?>> getTypesInReverseOrder(Class<?> objectType)
          Returns the path of classes to the given object type in reverse order.
static boolean hasClass(String clazz)
          Checks whether a given Class is available on the current platform.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDefaultConstructor

public static Constructor<?> getDefaultConstructor(Class<?> objectType)
Returns the default constructor of the given object type.

Parameters:
objectType - the object type
Returns:
the default constructor

createInstance

public static <T> T createInstance(Class<T> objectType)
Creates a new instance of the given object type using the default constructor.

Type Parameters:
T - the type of the object to create
Parameters:
objectType - the object type
Returns:
an object

getAllFields

public static Collection<Field> getAllFields(Class<?> objectType)
Returns the fields of the given class. These include the inherited fields, too.

Parameters:
objectType - the class
Returns:
the list of fields.

getTypesInReverseOrder

public static List<Class<?>> getTypesInReverseOrder(Class<?> objectType)
Returns the path of classes to the given object type in reverse order. The first element is the given object type, the second its superclass etc. Returns an empty collection if the given object type is an interface or is primitive. The class Object is not included.

Parameters:
objectType - the object type
Returns:
a list of classes

getMethod

public static Method getMethod(Class<?> objectType,
                               String methodName,
                               Class<?>... parameterTypes)
Returns the method on the objectType with the given name and given parameter types.

Parameters:
objectType - the object type
methodName - the method name
parameterTypes - the parameter types
Returns:
a method or null if it does not exist.

callMethod

public static <T> T callMethod(Object object,
                               String methodName)
Calls the parameterless method with the given name on the given object.

Type Parameters:
T - the expected type of the return value
Parameters:
object - the object to invoke the method on
methodName - the name of the method
Returns:
the return value of the method call

callMethod

public static <T> T callMethod(Object object,
                               Method method,
                               Object... parameters)
Calls the given method with the given parameters on the given object.

Type Parameters:
T - the expected type of the return value
Parameters:
object - the object to invoke the method on
method - the method
parameters - the parameters
Returns:
the return value of the method call

getActualTypeParameter

public static Class<?> getActualTypeParameter(Field field,
                                              int index)
Returns the actual type parameter with the given index of the given field or null if it does not exist.

Parameters:
field - the field
index - the index of the requested actual type parameter
Returns:
a class or null

getNearest

public static <T> T getNearest(Class<?> objectType,
                               Map<Class<?>,T> map)
Returns the value mapped to the given object type or one of its supertypes (classes or interfaces) using a nearest match algorithm. I. e. it returns the value mapped to the object type nearest to the given one.

The used "nearest" relation has the following properties:
1. An object type is the nearest object type to itself.
2. Any direct super type (class or interface) is nearer to a given type then any indirect super type. 3. A direct super class of a class A is nearer than any interface implemented by A.
4. For two interfaces I1 and I2 directly implemented by a given class A, I1 is nearer to A than I2 if I1 is mentioned before I2 in the the implements clause of the declaration of the class.

Type Parameters:
T - the expected type of the return value
Parameters:
objectType - the object type
map - the map
Returns:
an instance of T or null

hasClass

public static boolean hasClass(String clazz)
Checks whether a given Class is available on the current platform.

Parameters:
clazz - The full qualified name of the class to check.
Returns:
true if class is available, false otherwise.

getClass

public static Class<?> getClass(String clazz)
Returns a Class object which represents the class with the given name.

Parameters:
clazz - The full qualified name of the class.
Returns:
The Class object representing the class with the given name.


Copyright © 2014. All rights reserved.