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 Object
get(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.Method
getGetter(String name)
Get the getter method of the given property.Property
getNameField()
Get the property of the name field.Property[]
getProperties()
Get all the properties.Property
getProperty(Object bean, String name)
Get theProperty
orJsonProperty
of the given name if it exists.Property
getProperty(Method method)
GetProperty
by it's getter, setter or compute method.Property
getProperty(String name)
Get theProperty
of the given name.Property[]
getSequenceFields()
Get all theSequence
fields.Method
getSetter(String name)
Get the setter method of the given property.static Mapper
of(Class<?> klass)
Create aMapper
for the given Java Bean class by introspecting all it's properties.Object
set(Object bean, String name, Object value)
Set the property of the given bean with the provided value.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.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 aMapper
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 theMapper
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 theProperty
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 theProperty
orJsonProperty
of 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)
GetProperty
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 namename
is considered name field.- Returns:
- a property
-
getSequenceFields
public Property[] getSequenceFields()
Get all theSequence
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 returnsnull
if 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
-
-