tags:

views:

2349

answers:

4

Friends, I am trying to convert DO to DTO using java and looking for automated tool before start writing my own. I just wanted to know if there any free tool available for the same.

+9  A: 

You could try Dozer.

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.

skaffman
seems promising let me try that out. Thanks skaffman
RN
+2  A: 

Use Apache commons beanutils:

static void copyProperties(Object dest, Object orig) -Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

http://commons.apache.org/beanutils/api/index.html

Pablojim
yeh,but properties that are in the source bean, but do not in the destination bean are simply ignored. Thanks anyways Pablojim :) I think I have to go with Dozer since we have some property names different for DO and DTO :(
RN
Fair enough - I didn't know this... Pascal's list was pretty impressive!
Pablojim
+24  A: 

There are some libraries around there:

  • Transmorph: Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).

  • EZMorph: EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans

  • Commons-BeanUtils: ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.

  • Commons-Lang: ArrayUtils -> Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).

  • Commons-Convert: Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions.

  • Morph: Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations.

  • Lorentz: Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type. (seems dead)

  • Spring framework: Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.

  • Dozer: Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.

  • OTOM: With OTOM, you can copy any data from any object to any other object. The possibilities are endless. Welcome to "Autumn".

  • Smooks: The Smooks JavaBean Cartridge allows you to create and populate Java objects from your message data (i.e. bind data to) (suggested by superfilin in comments).

Transmorph (pretty recent), EZMorph, Dozer, OTOM are all serious candidates. Dozer seems to be the most active project though (and maybe the most advanced).

Pascal Thivent
oh man Pascal, I really appreciate this. thank you.
RN
+1 for a great big list
KLE
none of the list supports annotation :(
RN
I would also add Smooks to that list. http://www.smooks.org/mediawiki/index.php?title=V1.2:Smooks_v1.2_User_Guide#Java_Binding
Superfilin
@superfilin Thanks! I wasn't aware of this one and will definitely have a look at it.
Pascal Thivent
Commons-Convert seems not just dormant but completely gone now ...
Jim Ferrans
A: 

You can also use Bean2Bean: http://sourceforge.net/projects/bean2bean

Dario
I see that this is yours (thus this post is to the point just plain spam). To avoid potential further sanctions and to save your face, I think it's smart to elaborate more why Bean2Bean should be chosen above the aforementioned existing products :) Make the people eager instead of just spamming.
BalusC