Class ReflectionUtils

java.lang.Object
org.supercsv.util.ReflectionUtils

public final class ReflectionUtils extends Object
Provides useful utility methods for reflection.
Since:
2.0.0
  • Field Details

  • Constructor Details

    • ReflectionUtils

      private ReflectionUtils()
  • Method Details

    • findGetter

      public static Method findGetter(Object object, String fieldName)
      Returns the getter method associated with the object's field.
      Parameters:
      object - the object
      fieldName - the name of the field
      Returns:
      the getter method
      Throws:
      NullPointerException - if object or fieldName is null
      SuperCsvReflectionException - 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 name
      clazz - the class
      enforceBooleanReturnType - 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

      public static Method findSetter(Object object, String fieldName, Class<?> argumentType)
      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 an Integer) 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 or interface respectively).

      Parameters:
      object - the object
      fieldName - the name of the field
      argumentType - the type to be passed to the setter
      Returns:
      the setter method
      Throws:
      NullPointerException - if object, fieldName or fieldType is null
      SuperCsvReflectionException - 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 setter
      setterName - the name of the setter
      argumentType - the type to be passed to the setter
      Returns:
      the setter method, or null if none is found
    • getMethodNameForField

      private static String getMethodNameForField(String prefix, String fieldName)
      Gets the camelcase getter/setter method name for a field.
      Parameters:
      prefix - the method prefix
      fieldName - the field name
      Returns:
      the method name