-2

We have a large number of classes that is generated by JAXB. There are a couple that have identical fields and I want to see if there is a clean way to map class A to class B.

Example:

public static class ParentA  implements ... {

   public static class A implements ... {
      protected String name;
   }
}
public static class ParentB  implements ... {

   public static class B implements ... {
      protected String name;
   }
}

I want to take an incoming A and map it to B but there are many other classes that I want to map A to. How can I handle this? I see that there is a copyTo method, but it only copies A to another A.

  • I also would like to know if there is a way to wrap these generated classes with a builder pattern. – ooomid Jul 30 at 16:01
  • Is there a superclass of both A and B? If not, why do they have so much in common without being related? Its hard to say what you want to model and how this could be solved without knowing more about your domain. – Polygnome Jul 31 at 14:01
0

What you'd probably get some value from would be an Object mapping tool/framework. The point of such tools is to remove some boilerplate code, so that you don't have to do so much work to transform object A into object B, in your scenerio. However, these are tool recommendations and not such a great fit for this site. You should check out http://modelmapper.org/ and google around for some similar packages.

  • Worked great, thanks!! – ooomid Aug 1 at 1:14
0

If all that A and B have in common is the field name, and all you want is an extended version of

B b = new B();
b.setName(a.getName());

You could consider a bean mapping tool like Dozer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.