org.springframework.datastore.reflect
Class ReflectionUtils

java.lang.Object
  extended by org.springframework.datastore.reflect.ReflectionUtils

public class ReflectionUtils
extends Object

Provides methods to help with reflective operations

Since:
1.0

Field Summary
static Map<Class<?>,Class<?>> PRIMITIVE_TYPE_COMPATIBLE_CLASSES
           
 
Constructor Summary
ReflectionUtils()
           
 
Method Summary
static PropertyDescriptor[] getPropertiesOfType(Class<?> clazz, Class<?> propertyType)
          Retrieves all the properties of the given class for the given type
static Object instantiate(Class clazz)
          Instantiates an object catching any relevant exceptions and rethrowing as a runtime exception
static boolean isAssignableFrom(Class<?> leftType, Class<?> rightType)
          Tests whether or not the left hand type is compatible with the right hand type in Groovy terms, i.e.
static void makeAccessible(Field field)
          Make the given field accessible, explicitly setting it accessible if necessary.
static void makeAccessible(Method method)
          Make the given method accessible, explicitly setting it accessible if necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRIMITIVE_TYPE_COMPATIBLE_CLASSES

public static final Map<Class<?>,Class<?>> PRIMITIVE_TYPE_COMPATIBLE_CLASSES
Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

makeAccessible

public static void makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active). Based on the same method in Spring core.

Parameters:
field - the field to make accessible
See Also:
AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)

makeAccessible

public static void makeAccessible(Method method)
Make the given method accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active). Based on the same method in Spring core.

Parameters:
method - the method to make accessible
See Also:
AccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)

isAssignableFrom

public static boolean isAssignableFrom(Class<?> leftType,
                                       Class<?> rightType)

Tests whether or not the left hand type is compatible with the right hand type in Groovy terms, i.e. can the left type be assigned a value of the right hand type in Groovy.

This handles Java primitive type equivalence and uses isAssignableFrom for all other types, with a bit of magic for native types and polymorphism i.e. Number assigned an int. If either parameter is null an exception is thrown

Parameters:
leftType - The type of the left hand part of a notional assignment
rightType - The type of the right hand part of a notional assignment
Returns:
True if values of the right hand type can be assigned in Groovy to variables of the left hand type.

instantiate

public static Object instantiate(Class clazz)
Instantiates an object catching any relevant exceptions and rethrowing as a runtime exception

Parameters:
clazz - The class
Returns:
The instantiated object or null if the class parameter was null

getPropertiesOfType

public static PropertyDescriptor[] getPropertiesOfType(Class<?> clazz,
                                                       Class<?> propertyType)
Retrieves all the properties of the given class for the given type

Parameters:
clazz - The class to retrieve the properties from
propertyType - The type of the properties you wish to retrieve
Returns:
An array of PropertyDescriptor instances