@FieldMapper

@FieldMapper

A simple java solution for field mapping

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.

What we typically do?

  1. For same data type we will use getter and setters to get value form User and set it to UserDto object.
  2. When we need to set createdDate in UserDto, we will use conversion method to convert date object from Userobject to MM-DD-YY formated string. method to convert will be in DateUtilty.class for reusing purpose.
  3. For isActive field in UserDto we need to convert Y/N to true/false. we can create conversion method in any class, let’s say CommonUtility and use that to convert to true/false and set the value to UserDto.

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

Let’s use @FieldMapper:

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);
}

How to configure?

Just annotate the fields that needs to be copied. we have three usefull attributes in @FieldMapper

  • clazz– class form which the method should be accessed
  • method– method name which is to be invoked from class mentioned in clazz
  • name – name is to map two fields in two objects having different field name. if object from field and object to field have same name then this attribute can be skipped. In above example we have used name attribute to map active field in User.class and isActive field in UserDto.class

What are the constraints??

  1. Method mentioned in method attribute of @FieldMapper should be static method
  2. If Java Security Manager restricts permission to access field using java reflection API. This annotation cannot be used. Since FieldMapperUtility.class uses java reflection API to access private members to get and set values

So Thats it ???

No..! More logic will be implemented. Samples and docs will be updated…!

Click here to redirect to GitHub repository

Join the Conversation

4 Comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: