
@Target(value=METHOD) @Retention(value=CLASS) public @interface BeforeMapping
Mapper.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:
@MappingTarget is populated with the target instance of the mapping.
@TargetType is populated with the target type of the mapping.@Context are populated with the context parameters of the mapping
method.
For non-void methods, the return value of the method invocation is returned as the result of the mapping
method if it is not null.
All before-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 their variant and their location of definition:
@MappingTarget parameter are called before any null-checks on source
parameters and constructing a new target bean.@MappingTarget parameter are called after constructing a new target bean.@Context parameters, ordered by the parameter order.Mapper.uses(), in the order of the type declaration in the annotation.
Example:
@BeforeMapping
public void calledWithoutArgs() {
// ...
}
@BeforeMapping
public void calledWithSourceAndTargetType(SourceEntity anySource, @TargetType Class<?> targetType) {
// ...
}
@BeforeMapping
public void calledWithSourceAndTarget(Object anySource, @MappingTarget TargetDto target) {
// ...
}
public abstract TargetDto toTargetDto(SourceEntity source);
// generates:
public TargetDto toTargetDto(SourceEntity source) {
calledWithoutArgs();
calledWithSourceAndTargetType( source, TargetDto.class );
if ( source == null ) {
return null;
}
TargetDto targetDto = new TargetDto();
calledWithSourceAndTarget( source, targetDto );
// actual mapping code
return targetDto;
}
AfterMapping,
ContextCopyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.