Package org.supercsv.util
Class ReflectionUtils
java.lang.Object
org.supercsv.util.ReflectionUtils
Provides useful utility methods for reflection.
- Since:
- 2.0.0
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Method
findGetter
(Object object, String fieldName) Returns the getter method associated with the object's field.private static Method
findGetterWithCompatibleReturnType
(String getterName, Class<?> clazz, boolean enforceBooleanReturnType) Helper method for findGetter() that finds a getter with the supplied name, optionally enforcing that the method must have a Boolean/boolean return type.static Method
findSetter
(Object object, String fieldName, Class<?> argumentType) Returns the setter method associated with the object's field.private static Method
findSetterWithCompatibleParamType
(Class<?> clazz, String setterName, Class<?> argumentType) Helper method for findSetter() that returns the setter method of the supplied name, whose parameter type is compatible with the supplied argument type (will allow an object of that type to be used when invoking the setter), or returns null if no match is found.private static String
getMethodNameForField
(String prefix, String fieldName) Gets the camelcase getter/setter method name for a field.
-
Field Details
-
SET_PREFIX
- See Also:
-
GET_PREFIX
- See Also:
-
IS_PREFIX
- See Also:
-
AUTOBOXING_CONVERTER
A map of primitives and their associated wrapper classes, to cater for autoboxing.
-
-
Constructor Details
-
ReflectionUtils
private ReflectionUtils()
-
-
Method Details
-
findGetter
Returns the getter method associated with the object's field.- Parameters:
object
- the objectfieldName
- the name of the field- Returns:
- the getter method
- Throws:
NullPointerException
- if object or fieldName is nullSuperCsvReflectionException
- if the getter doesn't exist or is not visible
-
findGetterWithCompatibleReturnType
private static Method findGetterWithCompatibleReturnType(String getterName, Class<?> clazz, boolean enforceBooleanReturnType) Helper method for findGetter() that finds a getter with the supplied name, optionally enforcing that the method must have a Boolean/boolean return type. Developer note: this method could have accepted an actual return type to enforce, but it was more efficient to cater for only Booleans (as they're the only type that has differently named getters).- Parameters:
getterName
- the getter nameclazz
- the classenforceBooleanReturnType
- if true, the method must return a Boolean/boolean, otherwise it's return type doesn't matter- Returns:
- the getter, or null if none is found
-
findSetter
Returns the setter method associated with the object's field.This method handles any autoboxing/unboxing of the argument passed to the setter (e.g. if the setter type is a primitive
int
but the argument passed to the setter is anInteger
) by looking for a setter with the same type, and failing that checking for a setter with the corresponding primitive/wrapper type.It also allows for an argument type that is a subclass or implementation of the setter type (when the setter type is an
Object
orinterface
respectively).- Parameters:
object
- the objectfieldName
- the name of the fieldargumentType
- the type to be passed to the setter- Returns:
- the setter method
- Throws:
NullPointerException
- if object, fieldName or fieldType is nullSuperCsvReflectionException
- if the setter doesn't exist or is not visible
-
findSetterWithCompatibleParamType
private static Method findSetterWithCompatibleParamType(Class<?> clazz, String setterName, Class<?> argumentType) Helper method for findSetter() that returns the setter method of the supplied name, whose parameter type is compatible with the supplied argument type (will allow an object of that type to be used when invoking the setter), or returns null if no match is found. Preference is given to setters whose parameter type is an exact match, but if there is none, then the first compatible method found is returned.- Parameters:
clazz
- the class containing the settersetterName
- the name of the setterargumentType
- the type to be passed to the setter- Returns:
- the setter method, or null if none is found
-
getMethodNameForField
Gets the camelcase getter/setter method name for a field.- Parameters:
prefix
- the method prefixfieldName
- the field name- Returns:
- the method name
-