Class BeanConverter<T>


  • public class BeanConverter<T>
    extends Object
    Simple way to convert one bean to another. This utility uses spring-beans to attempt the conversion at first. If attempting to maps, it will attempt to do simple copies of the key-value pairs.

    Exclusion lists can be provided to ignore specific fields.

    Also custom mappers can be provided per field for more control over how the fields are converted.

    NOTE: This class requires spring-beans as a dependency.

    • Constructor Detail

      • BeanConverter

        public BeanConverter()
    • Method Detail

      • convert

        public T convert​(T input)
        This conversion method taks a single parameter and modifies the object in place.
        Parameters:
        input - the object to perform the conversion on
        Returns:
        the same object that was passed in
      • convert

        public <R> R convert​(T input,
                             R target)
        This conversion method takes two parameters and copies properties from one object to another
        Type Parameters:
        R - the type of object being returned
        Parameters:
        input - the object to copy the properties from
        target - the object to copy the properties too (destination)
        Returns:
        the modified target object
      • getPropertySet

        protected Set<String> getPropertySet​(T input,
                                             org.springframework.beans.BeanWrapper inputWrapper)
      • hasPropertyMapper

        public boolean hasPropertyMapper​(String propertyName)
        Checks to see if a property mapper exists for a given property name
        Parameters:
        propertyName - the property name
        Returns:
        true if a property mapper was found for that property
      • getPropertyMapper

        public <R> Function<T,​R> getPropertyMapper​(String propertyName)
        Get the property mapper function for a specific property name.

        It is assumed the caller knows the correct return type for the mapper, otherwise a ClassCastException will be thrown at runtime.

        Type Parameters:
        R - the result type of the the mapper for the given property
        Parameters:
        propertyName - the property name
        Returns:
        the Function that will be triggered
      • readBeanValue

        protected Object readBeanValue​(T input,
                                       org.springframework.beans.BeanWrapper inputWrapper,
                                       String propName)
      • writeBeanValue

        protected <R> void writeBeanValue​(R target,
                                          org.springframework.beans.BeanWrapper targetWrapper,
                                          String propName,
                                          Object inputValue)
      • addPropertyMapper

        public void addPropertyMapper​(String propertyName,
                                      Function<T,​?> function)
        Adds a property mapper function for a specific property name
        Parameters:
        propertyName - the property name
        function - the Function that will be triggered
        Throws:
        IllegalStateException - if a mapper is already registered on the given property