Prev Next

Java / MapStruct Java Interview questions part 2

Explain the execution flow of a generated MapStruct mapper method?

Once compiled, calling a MapStruct mapper method behaves exactly like calling any ordinary Java method - there is no framework machinery involved at runtime.

sequenceDiagram participant Caller participant MapperImpl participant NestedMapper Caller->>MapperImpl: toDto(entity) MapperImpl->>MapperImpl: new TargetDto() MapperImpl->>MapperImpl: target.setName(entity.getName()) MapperImpl->>NestedMapper: toAddressDto(entity.getAddress()) NestedMapper-->>MapperImpl: AddressDto MapperImpl->>MapperImpl: target.setAddress(addressDto) MapperImpl-->>Caller: TargetDto

Each field assignment is a direct method call baked in at compile time; nested object properties are delegated to whichever mapping method was resolved for that type during code generation. There is no lookup, reflection, or dynamic dispatch beyond normal Java virtual method calls.

This is exactly why stepping through a mapper call in a debugger feels the same as stepping through hand-written code: the stack frames belong to the generated class itself, with named local variables and straightforward branches for any null checks, rather than opaque proxy or reflection frames.

At runtime, a generated mapper method's field assignments are:
Nested object conversion inside a generated method is handled by:

More Related questions...

What is MapStruct? What are the main features of MapStruct? What is the purpose of the @Mapper annotation? What are the types of mapping methods MapStruct can generate? How do you add MapStruct to a Maven project? How do you add MapStruct to a Gradle project? Define a MapStruct mapper interface? Describe how MapStruct generates the mapper implementation class? List the annotations commonly used with MapStruct? What is the purpose of the @Mapping annotation? How do you map fields with different names using MapStruct? How do you ignore a target field during mapping? What is the componentModel attribute in @Mapper? How do you use MapStruct with Spring Boot? What are default methods in a MapStruct mapper used for? How do you map a List of objects with MapStruct? What is the purpose of the uses attribute in @Mapper? How do you retrieve a mapper instance without Spring? What is the difference between MapStruct and ModelMapper? What is the difference between MapStruct and Dozer? Why is MapStruct faster than reflection-based mapping libraries? How does MapStruct handle nested object mapping? How does MapStruct map a Map to another Map type? How do you perform custom type conversion in MapStruct? Explain the internal working of MapStruct's annotation processing? Explain the execution flow of a generated MapStruct mapper method? How do you use @BeforeMapping and @AfterMapping? When should you use an expression inside @Mapping? How does MapStruct handle null values during mapping? What is the difference between NullValueMappingStrategy options? How do you inject dependencies into a MapStruct mapper? When would you choose an abstract class over an interface for a mapper? What happens when MapStruct can't find a matching source property? How do you use @InheritInverseConfiguration for bidirectional mapping? How do you use @InheritConfiguration to reuse mapping rules? Why doesn't MapStruct use reflection at runtime? How do you troubleshoot an "Unmapped target property" warning? What is the difference between @Mapper(uses =...) and a default method? How can you optimize MapStruct-generated mapping performance? Explain the lifecycle of a MapStruct mapper from source code to runtime call? How does MapStruct integrate with Lombok, and what issues can occur? What is the difference between returning a new object and using @MappingTarget? When should you use the @MappingTarget parameter? How do you map an enum to a different enum with mismatched constants? What is the difference between qualifiedByName and qualifiedBy? How does MapStruct handle circular references between objects? Why should you use MapStruct instead of hand-written DTO mapping code? How do you unit test a MapStruct-generated mapper? What is the difference between MapStruct's compile-time approach and CGLIB proxy-based mapping? How do you handle multiple mapping methods for the same source and target types?
Show more question and Answers...


Comments & Discussions