A set of utility methods to make working with Classes and Reflection a little easier.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
clearCache
(ClassLoader loader) Clears the cache for the specifiedClassLoader
.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".static void
initCache
(ClassLoader loader) static <T> T
Creates a new instance of the class represented by the given Class objectstatic <T> T
Creates an instance of a class with the given fully qualified class name.static Class
<?> lookupClass
(String className) Obtain aClass
instance based on the provided String name.static Constructor
<?> lookupConstructor
(Class<?> clazz, Class<?>... params) Returns theConstructor
appropriate to the specified Class and parameters.static Method
lookupMethod
(Class<?> clazz, String methodName, Class<?>... params) Returns theMethod
appropriate to the specified Class, method name, and parameters.static Method
lookupMethod
(Object object, String methodName, Class<?>... params) Returns theMethod
appropriate to the specified object instance, method name, and parameters.static Method
lookupReadMethod
(String className, String propertyName) static Method
lookupWriteMethod
(String className, String propertyName) static Object
newInstance
(String className) Constructs a new object instance based off the provided class name.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.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.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.
-
Method Details
-
setProperties
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:
object.setFoo("string");
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.
- Parameters:
object
- the object on which properties will be setpropertiesToSet
- the map containing properties and their values to be set on the object
-
setPropertiesWithCoercion
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:
object.setFoo("string");
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
Reflection#setProperties(Object, Map)
Here only the available writable properties of the object are matched against the map with properties to set. Properties in the map for which there isn't a corresponding writable property on the object are ignored.Following the above two notes, use this method when attempting to set properties on an object in a lenient best effort basis. Use
Reflection#setProperties(Object, Map)
when all properties need to be set with the exact type as the value appears in the map.- Parameters:
object
- the object on which properties will be setpropertiesToSet
- the map containing properties and their values to be set on the object
-
findMethod
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.
- Parameters:
base
- the object in which the method is to be foundmethodName
- name of the method to be foundparams
- the method parameters- Returns:
- a method if one is found, null otherwise
-
toClass
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.- Parameters:
className
- fully qualified class name of the class for which a Class instance needs to be created- Returns:
- the Class object for the class with the given name.
- Throws:
IllegalStateException
- if the class cannot be found.
-
instance
Creates an instance of a class with the given fully qualified class name.- Type Parameters:
T
- The generic object type.- Parameters:
className
- fully qualified class name of the class for which an instance needs to be created- Returns:
- an instance of the class denoted by className
- Throws:
IllegalStateException
- if the class cannot be found
-
instance
Creates a new instance of the class represented by the given Class object- Type Parameters:
T
- The generic object type.- Parameters:
clazz
- the Class object for which an instance needs to be created- Returns:
- an instance of the class as given by the clazz parameter
- Throws:
IllegalStateException
- if the class cannot be found, or cannot be instantiated or when a security manager prevents this operation
-
clearCache
Clears the cache for the specified
ClassLoader
.This method MUST be called when
ConfigureListener .contextDestroyed()
is called.- Parameters:
loader
- theClassLoader
whose associated cache should be cleared
-
initCache
-
lookupConstructor
Returns the
Constructor
appropriate to the specified Class and parameters.- Parameters:
clazz
- the Class of interestparams
- the parameters for the constructor of the provided Class- Returns:
- a Constructor that can be invoked with the specified parameters
-
lookupMethod
Returns the
Method
appropriate to the specified object instance, method name, and parameters.- Parameters:
object
- the Object instance of interestmethodName
- the name of the methodparams
- the parameters for the specified method- Returns:
- a Method that can be invoked with the specified parameters
-
lookupMethod
Returns the
Method
appropriate to the specified Class, method name, and parameters.- Parameters:
clazz
- the Class of interestmethodName
- the name of the methodparams
- the parameters for the specified method- Returns:
- a Method that can be invoked with the specified parameters
-
newInstance
public static Object newInstance(String className) throws IllegalArgumentException, ReflectiveOperationException, SecurityException Constructs a new object instance based off the provided class name.
- Parameters:
className
- the class of the object to instantiate- Returns:
- a new instances of said class
- Throws:
InstantiationException
- if the class cannot be instantiatedIllegalAccessException
- if there is a security violationIllegalArgumentException
ReflectiveOperationException
SecurityException
-
lookupClass
-
lookupWriteMethod
-
lookupReadMethod
- Parameters:
className
- the fully qualified class namepropertyName
- a JavaBeans property name- Returns:
- a method suitable for obtaining the value of a JavaBeans property, or
null
if the property doesn't exist or can't be read.
-