Class ReflectionUtils


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

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> AUTOBOXING_CONVERTER
      A map of primitives and their associated wrapper classes, to cater for autoboxing.
      static java.lang.String GET_PREFIX  
      static java.lang.String IS_PREFIX  
      static java.lang.String SET_PREFIX  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Method findGetter​(java.lang.Object object, java.lang.String fieldName)
      Returns the getter method associated with the object's field.
      private static java.lang.reflect.Method findGetterWithCompatibleReturnType​(java.lang.String getterName, java.lang.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 java.lang.reflect.Method findSetter​(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> argumentType)
      Returns the setter method associated with the object's field.
      private static java.lang.reflect.Method findSetterWithCompatibleParamType​(java.lang.Class<?> clazz, java.lang.String setterName, java.lang.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 java.lang.String getMethodNameForField​(java.lang.String prefix, java.lang.String fieldName)
      Gets the camelcase getter/setter method name for a field.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • AUTOBOXING_CONVERTER

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Class<?>> AUTOBOXING_CONVERTER
        A map of primitives and their associated wrapper classes, to cater for autoboxing.
    • Constructor Detail

      • ReflectionUtils

        private ReflectionUtils()
    • Method Detail

      • findGetter

        public static java.lang.reflect.Method findGetter​(java.lang.Object object,
                                                          java.lang.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:
        java.lang.NullPointerException - if object or fieldName is null
        SuperCsvReflectionException - if the getter doesn't exist or is not visible
      • findGetterWithCompatibleReturnType

        private static java.lang.reflect.Method findGetterWithCompatibleReturnType​(java.lang.String getterName,
                                                                                   java.lang.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 java.lang.reflect.Method findSetter​(java.lang.Object object,
                                                          java.lang.String fieldName,
                                                          java.lang.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:
        java.lang.NullPointerException - if object, fieldName or fieldType is null
        SuperCsvReflectionException - if the setter doesn't exist or is not visible
      • findSetterWithCompatibleParamType

        private static java.lang.reflect.Method findSetterWithCompatibleParamType​(java.lang.Class<?> clazz,
                                                                                  java.lang.String setterName,
                                                                                  java.lang.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 java.lang.String getMethodNameForField​(java.lang.String prefix,
                                                              java.lang.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