A set of utility methods to make working with Classes and Reflection a little easier.
| Modifier and Type | Class and Description |
|---|---|
| private static class | ReflectionUtils.
MetaData contains lookup methods for |
| Modifier and Type | Field and Description |
|---|---|
| private static final Logger | |
| private static final Map | REFLECTION_CACHE
Cache |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static synchronized void | clearCache(ClassLoader
the loader)ClassLoader whose associated cache should be cleared
Clears the cache for the specified |
| public static Method | Returns: a method if one is found, null otherwisethe object in which the method is to be found base, String name of the method to be found methodName, Object[] the method parameters params)Finds a method based on the method name, amount of parameters and limited typing, if necessary prefixed with "get". |
| private static ReflectionUtils. | Returns: aMetaData object for the specified ClassClassLoaderthe Class of interest clazz)
Return the |
| private static ReflectionUtils. | Returns: aMetaData object for the specified ClassClassLoaderthe class of interest className)
Return the |
| public static synchronized void | |
| public static < The generic object type. T> T | |
| public static < The generic object type. T> T | |
| public static Class | Returns: theClass corresponding to classNamethe class to look up className)
Obtain a |
| public static Constructor | Returns: a Constructor that can be invoked with the specified parametersthe Class of interest clazz, Class<?>... the parameters for the constructor of the provided Class params)
Returns the |
| public static Method | Returns: a Method that can be invoked with the specified parametersthe Object instance of interest object, String the name of the method methodName, Class<?>... the parameters for the specified method params)
Returns the |
| public static Method | Returns: a Method that can be invoked with the specified parametersthe Class of interest clazz, String the name of the method methodName, Class<?>... the parameters for the specified method params)
Returns the |
| public static Method | Returns: a method suitable for obtaining the value of a JavaBeans property, ornull if the property
doesn't exist or can't be read.the fully qualified class name className, String a JavaBeans property name propertyName)
|
| public static Method | Returns: a method suitable for setting a JavaBeans property, ornull if the property doesn't exist or is
readonly.the fully qualified class name className, String a JavaBeans property name propertyName)
|
| public static Object | Returns: a new instances of said classthe class of the object to instantiate className)Constructs a new object instance based off the provided class name. |
| public static void | setProperties(Object
the object on which properties will be set object, Map<String, Object> the map containing properties and their values to be set on the object propertiesToSet)Sets a collection of properties of a given object to the values associated with those properties. |
| public static void | setPropertiesWithCoercion(Object
the object on which properties will be set object, Map<String, Object> the map containing properties and their values to be set on the object propertiesToSet)Sets a collection of properties of a given object to the (optionally coerced) values associated with those properties. |
| public static Class | Returns: the Class object for the class with the given name.fully qualified class name of the class for which a Class instance needs to be created className)Returns the Class instance associated with the class of the given string, using the context class loader and if that fails the defining class loader of the current class. |
| LOGGER | back to summary |
|---|---|
| private static final Logger LOGGER | |
| REFLECTION_CACHE | back to summary |
|---|---|
| private static final Map<ClassLoader, ConcurrentMap<String, ReflectionUtils. Cache | |
| ReflectionUtils | back to summary |
|---|---|
| private ReflectionUtils() | |
| clearCache | back to summary |
|---|---|
| public static synchronized void clearCache(ClassLoader loader)
Clears the cache for the specified
This method MUST be called when
| |
| findMethod | back to summary |
|---|---|
| public static Method findMethod(Object base, String methodName, Object[] params) Finds a method based on the method name, amount of parameters and limited typing, if necessary prefixed with "get". Note that this supports overloading, but a limited one. Given an actual parameter of type Long, this will select a method accepting Number when the choice is between Number and a non-compatible type like String. However, it will NOT select the best match if the choice is between Number and Long. | |
| getMetaData | back to summary |
|---|---|
| private static ReflectionUtils.
Return the
This will check the cache associated with the specified
| |
| getMetaData | back to summary |
|---|---|
| private static ReflectionUtils.
Return the
This will check the cache associated with the specified
| |
| initCache | back to summary |
|---|---|
| public static synchronized void initCache(ClassLoader loader) | |
| instance | back to summary |
|---|---|
| public static <T> T instance(String className) Creates an instance of a class with the given fully qualified class name.
| |
| instance | back to summary |
|---|---|
| public static <T> T instance(Class<T> clazz) Creates a new instance of the class represented by the given Class object
| |
| lookupClass | back to summary |
|---|---|
| public static Class
Obtain a | |
| lookupConstructor | back to summary |
|---|---|
| public static Constructor
Returns the
| |
| lookupMethod | back to summary |
|---|---|
| public static Method lookupMethod(Object object, String methodName, Class<?>... params)
Returns the | |
| lookupMethod | back to summary |
|---|---|
| public static Method lookupMethod(Class<?> clazz, String methodName, Class<?>... params)
Returns the | |
| lookupReadMethod | back to summary |
|---|---|
| public static Method lookupReadMethod(String className, String propertyName) | |
| lookupWriteMethod | back to summary |
|---|---|
| public static Method lookupWriteMethod(String className, String propertyName) | |
| newInstance | back to summary |
|---|---|
| public static Object newInstance(String className) throws IllegalArgumentException, ReflectiveOperationException, SecurityException Constructs a new object instance based off the provided class name.
| |
| setProperties | back to summary |
|---|---|
| public static void setProperties(Object object, Map<String, Object> propertiesToSet) Sets a collection of properties of a given object to the values associated with those properties. In the map that represents these properties, each key represents the name of the property, with the value associated with that key being the value that is set for the property.
E.g. map entry key = foo, value = "bar", which "bar" an instance of String, will conceptually result in the following
call: Note This particular method assumes that there's a write method for each property in the map with the right type. No specific checking is done whether this is indeed the case. | |
| setPropertiesWithCoercion | back to summary |
|---|---|
| public static void setPropertiesWithCoercion(Object object, Map<String, Object> propertiesToSet) Sets a collection of properties of a given object to the (optionally coerced) values associated with those properties. In the map that represents these properties, each key represents the name of the property, with the value associated with that key being the value that is set for the property.
E.g. map entry key = foo, value = "bar", which "bar" an instance of String, will conceptually result in the following
call: NOTE 1: In case the value is a String, and the target type is not String, the standard property editor mechanism will be used to attempt a conversion.
Note 2: This method operates somewhat as the reverse of
Following the above two notes, use this method when attempting to set properties on an object in a lenient best
effort basis. Use | |
| toClass | back to summary |
|---|---|
| public static Class Returns the Class instance associated with the class of the given string, using the context class loader and if that fails the defining class loader of the current class.
| |
MetaData contains lookup methods for Constructors and Methods of a particular Class.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Class | |
| pack-priv Map | |
| pack-priv Map | |
| pack-priv Map | |
| pack-priv Map |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static Integer | |
| public Class | Returns: theClass for this MetaData instanceLooks up the class for this MetaData instance. |
| public Constructor | Returns: theConstructor appropriate to the specified parameters or nullconstructor parameters params)
Looks up a |
| public Method | Returns: theMethod appropriate to the specified name and parameters or nullthe name of the name, Class<?>... the params)Method parameters
Looks up a |
| public Method | Returns: a method suitable for obtaining the value of a JavaBeans property, ornull if the property
doesn't exist or can't be read.a JavaBeans property name propName)
|
| public Method | Returns: a method suitable for setting a JavaBeans property, ornull if the property doesn't exist or is
readonly.a JavaBeans property name propName)
|
| clazz | back to summary |
|---|---|
| pack-priv Class<?> clazz | |
| constructors | back to summary |
|---|---|
| pack-priv Map<Integer, Constructor<T>> constructors | |
| declaredMethods | back to summary |
|---|---|
| pack-priv Map<String, HashMap<Integer, Method>> declaredMethods | |
| methods | back to summary |
|---|---|
| pack-priv Map<String, HashMap<Integer, Method>> methods | |
| propertyDescriptors | back to summary |
|---|---|
| pack-priv Map<String, PropertyDescriptor> propertyDescriptors | |
| MetaData | back to summary |
|---|---|
| public MetaData(Class<?> clazz)
Constructs a new
| |
| getKey | back to summary |
|---|---|
| private static Integer getKey(Class<?>... params) Return a hashcode of all the class parameters. | |
| lookupClass | back to summary |
|---|---|
| public Class Looks up the class for this MetaData instance.
| |
| lookupConstructor | back to summary |
|---|---|
| public Constructor
Looks up a
| |
| lookupMethod | back to summary |
|---|---|
| public Method lookupMethod(String name, Class<?>... params)
Looks up a | |
| lookupReadMethod | back to summary |
|---|---|
| public Method lookupReadMethod(String propName) | |
| lookupWriteMethod | back to summary |
|---|---|
| public Method lookupWriteMethod(String propName) | |