An Annotation that can be used to copy properties from one object to another object with intermediate conversions.
The problem field mapper solves is eliminating multiple lines of code for converting one object type to another object type with intermediate conversion.
Use cases can be like, When there is need to convert entity to dto, Where some field type should be converted to another type.
Let's say we have an entity User
and a dto UserDto
like below,
User.java
:
public class User{
private Integer userId;
private String userName;
private Date createdDate;
private String active;
//Getter and Setters
}
UserDto.java
:
public class UserDto{
private Integer userId;
private String userName;
private String createdDate;
private boolean isActive;
//Getter and Setters
}
Now, the need is to copy properties from User
object to UserDto
object.
- For same data type we will use getter and setters to get value form
User
and set it toUserDto
object. - When we need to set
createdDate
inUserDto
, we will use conversion method to convert date object fromUser
object to MM-DD-YY formated string. method to convert will be inDateUtilty.class
for reusing purpose. - For
isActive
field inUserDto
we need to convert Y/N to true/false. we can create conversion method in any class, let's sayCommonUtility
and use that to convert to true/false and set the value toUserDto
.
let's look at conversion code below,
private UserDto userToUserDto(User user){
UserDto userDto= new UserDto();
userDto.setUserId(user.getUserId());
userDto.setUserName(user.getUserName());
userDto.setCreatedDate( DateUtilty.dateToMmddyy(user.getCreatedDate()));
userDto.setIsActive(CommonUtility.booleanToYN(user.getActive()));
return userDto;
}
Yeah..! That's easy..! Here we have only four fields and code looks okay. but what if we have many fields and some fields needs to be converted while copying to different object type?
Code will be long line of setters and getters and conversion methods
1)Add @FieldMapper
annotation in required fields.
public class User{
@FieldMapper
private Integer userId;
@FieldMapper
private String userName;
@FieldMapper(method="mmddyyToDate", clazz=DateUtilty.class)
private Date createdDate;
@FieldMapper(name="active" method="booleanToYn", clazz=CommonUtility.class)
private String active;
//Getter and Setters
}
public class UserDto{
@FieldMapper
private Integer userId;
@FieldMapper
private String userName;
@FieldMapper(method="dateToMmddyy", clazz=DateUtilty.class)
private String createdDate;
@FieldMapper(name="active" method="ynToBoolean", clazz=CommonUtility.class)
private boolean isActive;
//Getter and Setters
}
Let's modify our userToUserDto
method:
private UserDto userToUserDto(User user){
return FieldMapperUtility.copy(user,UserDto.class);
}
Just annotate the fields that needs to be copied.
we have three usefull attributes in @FieldMapper
clazz
- class form which the method should be accessedmethod
- method name which is to be invoked from class mentioned in clazzname
- name is to map different fields having different field name. if field from which value is copied and field from which value is to be copied have same name then this attribute can be skipped. In above example we have used name attribute to mapactive
field inUser.class
andisActive
field inUserDto.class
- Method mentioned in
method
attribute of@FieldMapper
should be static method - If Java Security Manager restricts permission to access field using java reflection API. This annotation cannot be used. since
FieldMapperUtility.class
uses Reflection API to access private members to get and set values
Check test package for above example.