I have various sets of Java objects, some of them pojos generated from JAXB tools, some of them domain classes etc.. In a large application, I need to get data from one set of objects and put into another set of objects which have different capabilities to use the data.
There are various methods of doing this: object mapping frameworks being an obvious choice. However, most of these frameworks with a solid code base and community, use reflection.(dozer for example)
I’ve been using a combination of adapters which adopt pojos and more complex java classes to visitor pattern, so that a visitor walking on the adaptor walks over one set of objects, and in the process creates another set of objects (the objects usually have parent/child/tree type of references) Especially when pass by reference is used rather than creating new Strings etc, this should be the fastest method available.
Can you think of any other method? Some sort of serialization to a byte array in memory, then deserialization maybe? Could it beat visitor based copies in terms of performance? Am I being unfair to reflection based approaches like Dozer? This is a key operation in the application, so any improvements are likely to improve overall performance significantly.
The visitor pattern should be pretty near optimal for performance. Anything involving serialization is going to be worse because of the overhead of any kind of generalized mapping.
I’m not specifically familiar with dozer– but reflection doesn’t have to be such a huge hit if it is used essentially to automate the code writing you are doing by hand; that is, if it generates a class (or equivalent logic tree) a single time to define the copying operation and then runs this repeatedly. The reflection cost is amortized over a large number of operations and becomes negligible.