anij
anij

Reputation: 1372

MapStruct: Object.class to Custom.class mapping

I'm a newbie with MapStruct, and need some help with that.

I have a Source class, with an attribute

Object input;

Which, in runtime, returns a custom object named TicketDetails.

Now, in my target class there is a attribute named,

MyTicket myTicket;

which, I need to map with an attribute of TicketDetails object. For, better understanding, I'm writing the normal java code example below.

SourceClassModel sourceClassModel = new SourceClassModel();
TargetClassModel targetClassModel = new TargetClassModel();

//mapping
TicketDetails ticketDetails = (TicketDetails) sourceClassModel.getInput();
targetClassModel.setMyTicket(ticketDetails.getMyTicket);

Now, my question is, how to achieve this case using MapStruct?

Upvotes: 1

Views: 3254

Answers (1)

Gunnar
Gunnar

Reputation: 18970

Either on a used mapper (see @Mapper#uses()) or in a non-abstract method on the mapper itself - in case it is an abstract class and not an interface - define the mapping from Object to TicketDetails yourself:

TicketDetails asTicketDetails(Object details) {
    return (TicketDetails) details;
}

The generated method for the conversion of SourceClassModel to TargetClassModel will then invoke that manually written method for converting the myTicket property.

Upvotes: 2

Related Questions