Package org.mapstruct
Annotation Type AfterMapping
-
@Target(METHOD) @Retention(CLASS) public @interface AfterMapping
Marks a method to be invoked at the end of a generated mapping method, right before the lastreturn
statement of the mapping method. The method can be implemented in an abstract mapper class, be declared in a type (class or interface) referenced inMapper.uses()
, or in a type used as@
Context
parameter in order to be used in a mapping method.The method invocation is only generated if the return type of the method (if non-
void
) is assignable to the return type of the mapping method and all parameters can be assigned by the available source, target or context parameters of the mapping method:- A parameter annotated with
@
MappingTarget
is populated with the target instance of the mapping. - A parameter annotated with
@
TargetType
is populated with the target type of the mapping. - Parameters annotated with
@
Context
are populated with the context parameters of the mapping method. - Any other parameter is populated with a source parameter of the mapping.
For non-
void
methods, the return value of the method invocation is returned as the result of the mapping method if it is notnull
.All after-mapping methods that can be applied to a mapping method will be used.
@
Qualifier
/@
Named
can be used to filter the methods to use.The order of the method invocation is determined by their location of definition:
- Methods declared on
@
Context
parameters, ordered by the parameter order. - Methods implemented in the mapper itself.
- Methods from types referenced in
Mapper.uses()
, in the order of the type declaration in the annotation. - Methods declared in one type are used after methods declared in their super-type
Example:
@AfterMapping public void calledWithoutArgs() { // ... } @AfterMapping public void calledWithSourceAndTargetType(SourceEntity anySource, @TargetType Class<?> targetType) { // ... } @AfterMapping public void calledWithSourceAndTarget(Object anySource, @MappingTarget TargetDto target) { // ... } public abstract TargetDto toTargetDto(SourceEntity source); // generates: public TargetDto toTargetDto(SourceEntity source) { if ( source == null ) { return null; } TargetDto targetDto = new TargetDto(); // actual mapping code calledWithoutArgs(); calledWithSourceAndTargetType( source, TargetDto.class ); calledWithSourceAndTarget( source, targetDto ); return targetDto; }
- Author:
- Andreas Gudian
- See Also:
BeforeMapping
,Context
- A parameter annotated with