Casting source object in MapStruct

962 views
Skip to first unread message

Hylton Peimer

unread,
Jun 25, 2016, 2:10:29 PM6/25/16
to mapstruct-users
I'm mapping object of class "A" to a dto "ADto".

"A" contains a reference to class "B" which has subclass "C".

Is there a way to tell MapStruct the reference to "B" is an instance of "C"? The reason being that I have another mapping function that does something with "C".

I'm currently doing the following - which works, but I believe there must be a better way (eg. something with annotations)

    @Mapping(expression = "java( someC( (C) obj.gB() ) )", target = "something")
   
ADto map(A a);

    D someC
(C c);


Andreas Gudian

unread,
Jun 25, 2016, 2:19:40 PM6/25/16
to Hylton Peimer, mapstruct-users
What I would do is quite similar to what you are doing, just without having to use the java-expression:

Create a manual method that does the instance-of check and, if applicable, delegate to someC(..). MapStruct will generate the call to that method and that's it.

We have an issue on GitHub describing a feature that might generate such downcasting methods, but it's not more than an idea currently.


--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages