4

I have four classes:

public class A { }
public class B extends A { }
public class C extends B { }
public class D extends B { }

From what I read in the dozer documentation regarding inheritance, it must be pretty straight forward, I need to map super classes to each other, and as well for sub classes. Here is how it looks in the xml:

<mapping>
<class-a>foo.A</class-a>
<class-b>foo.bar.A</class-b>
</mapping>

<mapping>
<class-a>foo.B</class-a>
<class-b>foo.bar.B</class-b>
</mapping>

<mapping>
<class-a>foo.C</class-a>
<class-b>foo.bar.C</class-b>
</mapping>

<mapping>
<class-a>foo.D</class-a>
<class-b>foo.bar.D</class-b>
</mapping>

All these classes are identical, by that I mean D and D are having the same attributes, C and C and so on.

Problem: In the object I am passing to dozer, I have an attribute of type B, which could be initialized by C or D (polymorphism). When dozer gives back the new mapped object, it will always return the attribute type of B to me, and not C or D. How to fix this?

Max ZooZoo
  • 313
  • 5
  • 13

2 Answers2

1

Please, take a look at the Inheritance Mapping section. You can use such a mapping for a field class with subclasses.

<mapping>
    <class-a>foo.Source</class-a>
    <class-b>foo.Dest</class-b>
    <field>
      <a>attr</a>
      <b>attr</b>
      <a-hint>foo.B, foo.C, foo.D</a-hint>
      <b-hint>foo.bar.B, foo.bar.C, foo.bar.D</b-hint>
   </field>
</mapping>

EDIT: I implement some type of automatically polymorphism feature request. I think, it will be included in the next Dozer release.

Dmitry Spikhalskiy
  • 5,379
  • 1
  • 26
  • 40
0

- I think you need an explicit cast.

Eg:

B b = new C();

go(b)

//////////Called Method////////////////

public void go(B bx){

    C c = (C) bx;

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75