Package com.axelor.db.mapper
Class Mapper
- java.lang.Object
-
- com.axelor.db.mapper.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectget(Object bean, String name)Get the value of given property from the given bean.Class<?>getBeanClass()Get the bean class this mapper operates on.Set<String>getComputeDependencies(Property property)Find the fields directly accessed by the compute method of the given computed property.MethodgetGetter(String name)Get the getter method of the given property.PropertygetNameField()Get the property of the name field.Property[]getProperties()Get all the properties.PropertygetProperty(Object bean, String name)Get thePropertyorJsonPropertyof the given name if it exists.PropertygetProperty(Method method)GetPropertyby it's getter, setter or compute method.PropertygetProperty(String name)Get thePropertyof the given name.Property[]getSequenceFields()Get all theSequencefields.MethodgetSetter(String name)Get the setter method of the given property.static Mapperof(Class<?> klass)Create aMapperfor the given Java Bean class by introspecting all it's properties.Objectset(Object bean, String name, Object value)Set the property of the given bean with the provided value.static <T> TtoBean(Class<T> klass, Map<String,Object> values)Create an object of the given class mapping the given value map to it's properties.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.
-
-
-
Method Detail
-
of
public static Mapper of(Class<?> klass)
Create aMapperfor the given Java Bean class by introspecting all it's properties.If the
Mapperclass has been previously created for the given class, then theMapperclass is retrieved from the cache.- Parameters:
klass- the bean class- Returns:
- an instance of
Mapperfor the given class.
-
getProperties
public Property[] getProperties()
Get all the properties.- Returns:
- an array of
Property
-
getProperty
public Property getProperty(String name)
Get thePropertyof 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 thePropertyorJsonPropertyof the given name if it exists.- Parameters:
bean- the beanname- name of the property- Returns:
- a Property or null if property doesn't exist.
-
getProperty
public Property getProperty(Method method)
GetPropertyby 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
NameColumnor a field with namenameis considered name field.- Returns:
- a property
-
getSequenceFields
public Property[] getSequenceFields()
Get all theSequencefields.- 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 returnsnullif property doesn't exist.- Parameters:
bean- the beanname- 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 beanname- name of the propertyvalue- 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 beanvalues- value map- Returns:
- an instance of the given class
-
-