eu.beesoft.gaia.util
Class Reflection

java.lang.Object
  extended by eu.beesoft.gaia.util.Reflection

public class Reflection
extends java.lang.Object

Utility class for Java reflection.


Method Summary
static java.lang.Object createArrayInstance(java.lang.Class<?> componentType, int length)
          Creates an array instance with the given component type and length.
static java.lang.Object createArrayInstance(java.lang.String componentTypeName, int length)
          Creates an array instance with the type of the given name and length.
static java.lang.Object createInstance(java.lang.Class<?> creator)
          Creates a new instance of the given class.
static java.lang.Object createInstance(java.lang.Class<?> creator, java.lang.Object... arguments)
          Creates a new instance of the given class with the given arguments for constructor.
static java.lang.Object createInstance(java.lang.Class<?> creator, java.lang.Object argument)
          Creates a new instance of the given class with the given argument for constructor.
static java.lang.Object createInstance(java.lang.String className)
          Creates a new instance of the class with given name.
static java.lang.Class<?> getClass(java.lang.String className)
          Returns class with given className.
static java.lang.reflect.Field getField(java.lang.String fieldName, java.lang.Object objectOrClass)
          Returns a field with given fieldName in an objectOrClass.
static java.lang.reflect.Field getFieldIgnoreCase(java.lang.String fieldName, java.lang.Object objectOrClass)
          Returns a field with given fieldName in an objectOrClass.
static java.lang.Object getFieldValue(java.lang.reflect.Field field, java.lang.Object object)
          Returns value from given field of the object.
static java.lang.reflect.Method getGetter(java.lang.String fieldName, java.lang.Object objectOrClass)
          Returns getter method for property with given name in the given object or class.
static java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Object objectOrClass)
          Returns the method with given methodName and without any parameters.
static java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Object objectOrClass, java.lang.Class<?>... argumentTypes)
          Returns the method with given methodName and requested argumentTypes.
static java.lang.reflect.Method getMethod(java.lang.String methodName, java.lang.Object objectOrClass, java.lang.Object... arguments)
          Returns the method with given methodName and requested arguments.
static java.lang.reflect.Method[] getMethods(java.lang.String methodName, java.lang.Object objectOrClass)
          Returns all methods with given methodName from the objectOrClass (which can be object or its class) and its superclasses.
static java.lang.reflect.Method getSetter(java.lang.String fieldName, java.lang.Object objectOrClass)
          Returns setter method for property with given name in the given object or class.
static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object objectOrClass)
          Invokes given method on the objectOrClass.
static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object objectOrClass, java.lang.Object... arguments)
          Invokes given method on the objectOrClass with arguments.
static void setFieldValue(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)
          Sets the field represented by the field on the specified object argument to the specified new value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getField

public static java.lang.reflect.Field getField(java.lang.String fieldName,
                                               java.lang.Object objectOrClass)
Returns a field with given fieldName in an objectOrClass. This can be any object or its class. The given class, and if not found, its superclass(es), is scanned for a field.

Parameters:
fieldName - - name of field to search
objectOrClass - - object or its class
Returns:
found field or null

getFieldIgnoreCase

public static java.lang.reflect.Field getFieldIgnoreCase(java.lang.String fieldName,
                                                         java.lang.Object objectOrClass)
Returns a field with given fieldName in an objectOrClass. This can be any object or its class. The given class, and if not found, its superclass(es), is scanned for a field. The case of the field name is ignored.

Parameters:
fieldName - - name of field to search
objectOrClass - - object or its class
Returns:
found field or null

getFieldValue

public static java.lang.Object getFieldValue(java.lang.reflect.Field field,
                                             java.lang.Object object)
Returns value from given field of the object. No exception is thrown.

Parameters:
field - - a field instance used for access
object - - object from which the represented field's value is to be extracted
Returns:
the value of the represented field in object; primitive values are wrapped in an appropriate object before being returned
Throws:
java.lang.RuntimeException - if some error occurs

setFieldValue

public static void setFieldValue(java.lang.reflect.Field field,
                                 java.lang.Object object,
                                 java.lang.Object value)
Sets the field represented by the field on the specified object argument to the specified new value. The new value is automatically unwrapped if the underlying field has a primitive type. No exception is thrown.

Parameters:
field - - a field instance used for access
object - - the object whose field should be modified
value - - the new value for the field of object being modified

getMethod

public static java.lang.reflect.Method getMethod(java.lang.String methodName,
                                                 java.lang.Object objectOrClass)
Returns the method with given methodName and without any parameters. Method is searched in object or class represented by objectOrClass and its superclasses. Returns null, if no method was found.

Parameters:
methodName - - name of method to search
objectOrClass - - object or its class to search
Returns:
found method or null

getMethod

public static java.lang.reflect.Method getMethod(java.lang.String methodName,
                                                 java.lang.Object objectOrClass,
                                                 java.lang.Object... arguments)
Returns the method with given methodName and requested arguments. First are obtained classes from given arguments. Method is searched in object or class represented by objectOrClass and its superclasses. Returns null, if no method was found.

Parameters:
methodName - - name of method to search
objectOrClass - - object or its class to search
arguments - - objects that are aguments for method (in this array may be objects or their classes
Returns:
found method or null

getMethod

public static java.lang.reflect.Method getMethod(java.lang.String methodName,
                                                 java.lang.Object objectOrClass,
                                                 java.lang.Class<?>... argumentTypes)
Returns the method with given methodName and requested argumentTypes. Method is searched in object or class represented by objectOrClass and its superclasses. Returns null, if no method was found.

Parameters:
methodName - - name of method to search
objectOrClass - - object or its class to search
argumentTypes - - an array of classes that are agument types for the searched method
Returns:
found method or null

getMethods

public static java.lang.reflect.Method[] getMethods(java.lang.String methodName,
                                                    java.lang.Object objectOrClass)
Returns all methods with given methodName from the objectOrClass (which can be object or its class) and its superclasses.

Parameters:
methodName - - name of method to search
objectOrClass - - object or its class
Returns:
array of the found methods (never null)

invoke

public static java.lang.Object invoke(java.lang.reflect.Method method,
                                      java.lang.Object objectOrClass)
Invokes given method on the objectOrClass.

Parameters:
method - - method to invoke
objectOrClass - - object or its class
Returns:
result of method invocation or null if void

invoke

public static java.lang.Object invoke(java.lang.reflect.Method method,
                                      java.lang.Object objectOrClass,
                                      java.lang.Object... arguments)
Invokes given method on the objectOrClass with arguments.

Parameters:
method - - method to invoke
objectOrClass - - object or its class
arguments - - arguments to pass to method to invoke
Returns:
result of method invocation or null if void

getClass

public static java.lang.Class<?> getClass(java.lang.String className)
Returns class with given className. This method works also for primitives (boolean, byte, int, ...) and returns correct types.

Parameters:
className - - the name of the class
Returns:
class with given name
Throws:
java.lang.RuntimeException - if cannot find such class

createInstance

public static java.lang.Object createInstance(java.lang.String className)
Creates a new instance of the class with given name.

Parameters:
className - - name of the class
Returns:
a new instance of the given class
Throws:
java.lang.RuntimeException - if cannot create a new instance

createInstance

public static java.lang.Object createInstance(java.lang.Class<?> creator)
Creates a new instance of the given class.

Parameters:
creator - - class to create instance
Returns:
a new instance of the given class
Throws:
java.lang.RuntimeException - if cannot create a new instance

createInstance

public static java.lang.Object createInstance(java.lang.Class<?> creator,
                                              java.lang.Object argument)
Creates a new instance of the given class with the given argument for constructor.

Parameters:
creator - - class to create instance
argument - - argument to create instance
Returns:
a new instance of the given class
Throws:
java.lang.RuntimeException - if cannot create a new instance

createInstance

public static java.lang.Object createInstance(java.lang.Class<?> creator,
                                              java.lang.Object... arguments)
Creates a new instance of the given class with the given arguments for constructor.

Parameters:
creator - - class to create instance
arguments - - arguments to create instance
Returns:
a new instance of the given class
Throws:
java.lang.RuntimeException - if cannot create a new instance

createArrayInstance

public static java.lang.Object createArrayInstance(java.lang.String componentTypeName,
                                                   int length)
Creates an array instance with the type of the given name and length.

Parameters:
componentTypeName - - class name of the array component type
length - - length of the array
Returns:
new instance of array

createArrayInstance

public static java.lang.Object createArrayInstance(java.lang.Class<?> componentType,
                                                   int length)
Creates an array instance with the given component type and length.

Parameters:
componentType - - class of the array component type
length - - length of the array
Returns:
new instance of array

getGetter

public static java.lang.reflect.Method getGetter(java.lang.String fieldName,
                                                 java.lang.Object objectOrClass)
Returns getter method for property with given name in the given object or class.

Parameters:
fieldName - - name of the property
objectOrClass - - object or its class from which should be a getter obtained
Returns:
found getter method or null if not found

getSetter

public static java.lang.reflect.Method getSetter(java.lang.String fieldName,
                                                 java.lang.Object objectOrClass)
Returns setter method for property with given name in the given object or class.

Parameters:
fieldName - - name of the property
objectOrClass - - object or its class from which should be a setter obtained
Returns:
found setter method or null if not found