Package nl.colorize.util
Class ReflectionUtils
java.lang.Object
nl.colorize.util.ReflectionUtils
Various utility and convenience methods for working with reflection. Unless
stated otherwise, all methods in this class will throw unchecked exceptions
if an error occurs while attempting to locate, access, or call elements.
This prevents using code from having to deal with the several types of
checked exceptions thrown by the reflection APIs. Also, in some cases methods
may throw
SecurityException
if the environment in which the
application is used does not allow certain forms of reflection.-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
callMethod
(Object subject, String methodName, Object... args) Calls the object's method with the specified name using reflection.getFieldsWithAnnotation
(Object obj, Class<? extends Annotation> type) Returns a list containing all of an object's fields that are marked with the specified annotation.getMethodsWithAnnotation
(Object obj, Class<? extends Annotation> type) Returns a list containing all of an object's methods that are marked with the specified annotation.getProperties
(Object subject) Accesses an object's properties using reflection.static Object
getProperty
(Object subject, String propertyName) Accesses the object's property with the specified name using reflection, and returns its value.static <T> Comparator
<T> getPropertyComparator
(String propertyName) Returns a comparator that compares objects based on the value of one of their properties.getPropertyNames
(Class<?> forClass) Returns the names of the properties defined by the specified class.getPropertyTypes
(Class<?> forClass) Returns the names and types of the properties defined by the specified class.static void
setProperty
(Object subject, String propertyName, Object value) Accesses the object's property with the specified name using reflection, and updates its value.
-
Method Details
-
getProperty
Accesses the object's property with the specified name using reflection, and returns its value.- Throws:
IllegalArgumentException
- if the object does not have a property with that name.SecurityException
- if the property is private or protected, and the environment does not allow access to non-public properties.
-
getPropertyNames
Returns the names of the properties defined by the specified class. This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties. -
getPropertyTypes
Returns the names and types of the properties defined by the specified class. This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties.This method does not retrieve the actual property values for an instance of the class. Use
getProperties(Object)
for that purpose. -
getProperties
Accesses an object's properties using reflection. The properties are obtained using the approach described ingetProperty(Object, String)
. This does include private and protected properties, but does not include any properties defined by parent classes, or static or transient properties. -
setProperty
Accesses the object's property with the specified name using reflection, and updates its value.- Throws:
IllegalArgumentException
- if the object does not have a property with that name, or if the property's type and type ofvalue
don't match.SecurityException
- if the property is private or protected, and the environment does not allow access to non-public properties.
-
getPropertyComparator
Returns a comparator that compares objects based on the value of one of their properties. Property access follows the approach used bygetProperty(Object, String)
, including the exceptions that might be thrown when trying to access the property. -
callMethod
Calls the object's method with the specified name using reflection. The types of the arguments passed to the method (args
) are also used to find the method with the requested parameter types.- Throws:
RuntimeException
- if the calling the method results in an exception.IllegalArgumentException
- if no method with that name exists, or if the number of parameter or the parameter type don't match.
-
getMethodsWithAnnotation
Returns a list containing all of an object's methods that are marked with the specified annotation. -
getFieldsWithAnnotation
Returns a list containing all of an object's fields that are marked with the specified annotation.
-