Class Mapper


  • public class Mapper
    extends Object
    This class can be used to map params to Java bean using reflection. It also provides convenient methods to get/set values to a bean instance.
    • Method Detail

      • of

        public static Mapper of​(Class<?> klass)
        Create a Mapper for the given Java Bean class by introspecting all it's properties.

        If the Mapper class has been previously created for the given class, then the Mapper class is retrieved from the cache.

        Parameters:
        klass - the bean class
        Returns:
        an instance of Mapper for the given class.
      • getProperties

        public Property[] getProperties()
        Get all the properties.
        Returns:
        an array of Property
      • getProperty

        public Property getProperty​(String name)
        Get the Property of the given name.
        Parameters:
        name - name of the property
        Returns:
        a Property or null if property doesn't exist.
      • getProperty

        public Property getProperty​(Object bean,
                                    String name)
        Get the Property or JsonProperty of the given name if it exists.
        Parameters:
        bean - the bean
        name - name of the property
        Returns:
        a Property or null if property doesn't exist.
      • getProperty

        public Property getProperty​(Method method)
        Get Property by it's getter, setter or compute method.
        Parameters:
        method - the getter, setter or compute method
        Returns:
        the property associated with the method
      • getNameField

        public Property getNameField()
        Get the property of the name field.

        A name field annotated with NameColumn or a field with name name is considered name field.

        Returns:
        a property
      • getSequenceFields

        public Property[] getSequenceFields()
        Get all the Sequence fields.
        Returns:
        copy of the original set of fields.
      • getComputeDependencies

        public Set<String> getComputeDependencies​(Property property)
        Find the fields directly accessed by the compute method of the given computed property.
        Parameters:
        property - the computed property
        Returns:
        set of fields accessed by computed property
      • getBeanClass

        public Class<?> getBeanClass()
        Get the bean class this mapper operates on.
        Returns:
        the bean class
      • getGetter

        public Method getGetter​(String name)
        Get the getter method of the given property.
        Parameters:
        name - name of the property
        Returns:
        getter method or null if property is write-only
      • getSetter

        public Method getSetter​(String name)
        Get the setter method of the given property.
        Parameters:
        name - name of the property
        Returns:
        setter method or null if property is read-only
      • get

        public Object get​(Object bean,
                          String name)
        Get the value of given property from the given bean. It returns null if property doesn't exist.
        Parameters:
        bean - the bean
        name - name of the property
        Returns:
        property value
      • set

        public Object set​(Object bean,
                          String name,
                          Object value)
        Set the property of the given bean with the provided value.
        Parameters:
        bean - the bean
        name - name of the property
        value - value for the property
        Returns:
        old value of the property
      • toBean

        public static <T> T toBean​(Class<T> klass,
                                   Map<String,​Object> values)
        Create an object of the given class mapping the given value map to it's properties.
        Type Parameters:
        T - type of the bean
        Parameters:
        klass - class of the bean
        values - value map
        Returns:
        an instance of the given class
      • toMap

        public static Map<String,​Object> toMap​(Object bean)
        Create a map from the given bean instance with property names are keys and their respective values are map values.
        Parameters:
        bean - a bean instance
        Returns:
        a map