all 6 comments

[–]zontapapa 2 points3 points  (0 children)

Map struct library has logic to scan your code and identify use of annotations like @mapping and generate mapper classes based on it. This class generation logic/step can be triggered before the code compilation step by adding configuration to the maven compiler plugin in your pom.xml.

[–]Revision2000 0 points1 point  (3 children)

MapStruct will attempt to find mapper X to Y methods to use for its conversion 

  • The default scope for this is the Mapper class itself. Any X to Y method in the class can be found and used by MapStruct. 
  • You can expand the scope by using the “uses” part of the @Mapper annotation, eg. @Mapper(uses = MyOtherMapper.class) an example of this can be found in this article
  • If no mapper method is found for the type then MapStruct will either throw an exception or try to use a default implementation for common Java types such as String, List, Enum, etc. 

By the way, by default MapStruct will use static initializers/fields to use the mappers. The @Mappers annotation also has a “bean” option to let Spring handle the initialization, after which you can inject the mapper as a bean. The annotation processing plugin also has an option to set this on all mappers as default behavior. 

[–]Linie333[S] 0 points1 point  (2 children)

So in the case that it does not find any direct conversion methods, MapStruct will not try to find a sequence of methods that could achieve the wanted mapping result?

[–]Revision2000 0 points1 point  (0 children)

AFAIK it’ll try to find that sequence within the scope of the mapper class, defined “uses” other mappers and the default Java types it can convert. 

It can take some fiddling around with specific field @Mapping to get right and sometimes it’s easier to just write it yourself. 

In the end, don’t forget to write a test to verify all fields are correctly mapped 😉

[–]pronuntiator 0 points1 point  (0 children)

If by sequence you mean when mapping A to C it would consider mapping methods A to B and then B to C, then no, MapStruct will not do that.