0

I have a base class that I want to convert. All the types I want to convert to are identical to each other, except for the package name. They're created in specs I have no control over. Is there a way of creating a generic converter, so I'm not repeating identical get and set calls over and over? Mine are obviously more complex than the ones below, but the point I'm highlighting is that the field names differ between the original and final types.

Original:

com.mycompany.base.ConvertMe;

class ConvertMe {
  private String name;
  private String colour;
  private String fruit;

  //getters and setters...
}

Needs to map to:

com.mycompany.convertedA;

class Result {
  private String firstName;
  private String favouriteColour;
  private String favouriteFruit;

  //getters and setters...
}

And also map to:

com.mycompany.convertedB;

class Result {
  private String firstName;
  private String favouriteColour;
  private String favouriteFruit;

  //getters and setters...
}
edwardmlyte
  • 15,937
  • 23
  • 58
  • 83
  • Possible duplicate of http://stackoverflow.com/questions/7567409/create-a-common-converter-for-objects-from-different-packages ? – Rob Feb 11 '15 at 15:00
  • possible duplicate of [any tool for java object to object mapping?](http://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping) – Sufiyan Ghori Feb 11 '15 at 15:01

1 Answers1

2

Dozer is what you are looking for. It is very flexible and convenient for those cases, as supports mappings via annotations, xml mappings.

Orika could be another option. Though Dozer is one of the powerful and simplest.

For more detailed comparison, check this post

Community
  • 1
  • 1
vtor
  • 8,989
  • 7
  • 51
  • 67