@Retention(value=CLASS) @Repeatable(value=MappingControls.class) @Target(value=ANNOTATION_TYPE) public @interface MappingControl
There are several applications of MappingControl
conceivable. One application, "deep cloning" is
explained below in the example.
Another application is controlling so called "complex mappings", which are not always desirable and sometimes lead to unexpected behaviour and prolonged compilation time.
Example:Cloning of an object
When all methods are allowed, MapStruct would make a shallow copy. It would take the ShelveDTO
in
the FridgeDTO
and directly enter that as target on the target FridgeDTO
. By disabling all
other kinds of mappings apart from MappingControl.Use.MAPPING_METHOD
, see DeepClone
MapStruct is
forced to generate mapping methods all through the object graph `FridgeDTO` and hence create a deep clone.
public class FridgeDTO {
private ShelveDTO shelve;
public ShelveDTO getShelve() {
return shelve;
}
public void setShelve(ShelveDTO shelve) {
this.shelve = shelve;
}
}
public class ShelveDTO {
private CoolBeerDTO coolBeer;
public CoolBeerDTO getCoolBeer() {
return coolBeer;
}
public void setCoolBeer(CoolBeerDTO coolBeer) {
this.coolBeer = coolBeer;
}
}
public class CoolBeerDTO {
private String beerCount;
public String getBeerCount() {
return beerCount;
}
public void setBeerCount(String beerCount) {
this.beerCount = beerCount;
}
}
@Mapper(mappingControl = DeepClone.class)
public interface CloningMapper {
CloningMapper INSTANCE = Mappers.getMapper( CloningMapper.class );
FridgeDTO clone(FridgeDTO in);
}
Modifier and Type | Required Element and Description |
---|---|
MappingControl.Use |
value |
public abstract MappingControl.Use value
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.