Class BeanConverter<T>

java.lang.Object
org.kiwiproject.beans.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 convert 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 Details

    • BeanConverter

      public BeanConverter()
  • Method Details

    • convert

      public T convert(T input)
      This conversion method takes 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 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)
    • logOrFail

      protected void logOrFail(String msg, String propName, RuntimeException e)
    • 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
    • getMappers

      public Map<String,Function<T,?>> getMappers()
      Custom mappers that map specific field names to a function that will accept the data and convert it to the new value.
    • getExclusions

      public Set<String> getExclusions()
      Set of property names to exclude from copying over. Defaults to ["class", "new"]
    • setExclusions

      public void setExclusions(Set<String> exclusions)
      Set of property names to exclude from copying over. Defaults to ["class", "new"]
    • isFailOnError

      public boolean isFailOnError()
      Allows for a failed conversion to throw an exception if true; otherwise just logs a failure if false
    • setFailOnError

      public void setFailOnError(boolean failOnError)
      Allows for a failed conversion to throw an exception if true; otherwise just logs a failure if false