Class NestedTargetPropertyMappingHolder.Builder
- java.lang.Object
-
- org.mapstruct.ap.internal.model.NestedTargetPropertyMappingHolder.Builder
-
- Enclosing class:
- NestedTargetPropertyMappingHolder
public static class NestedTargetPropertyMappingHolder.Builder extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description private booleanerrorOccurredprivate java.util.Set<java.lang.String>existingVariableNamesprivate java.util.Set<java.lang.String>handledTargetsprivate MappingBuilderContextmappingContextprivate MappingReferencesmappingReferencesprivate Methodmethodprivate java.util.List<PropertyMapping>propertyMappingsprivate java.util.Map<java.lang.String,Accessor>targetPropertiesWriteAccessorsprivate TypetargetType
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NestedTargetPropertyMappingHolderbuild()private PropertyMappingcreatePropertyMappingForNestedTarget(MappingReferences mappingReferences, java.lang.String targetPropertyName, SourceReference sourceReference, boolean forceUpdateMethod)NestedTargetPropertyMappingHolder.BuilderexistingVariableNames(java.util.Set<java.lang.String> existingVariableNames)private java.util.Set<MappingReference>extractSingleTargetReferencesToUseAndPopulateSourceParameterMappings(java.util.Set<MappingReference> singleTargetReferences, java.util.Set<MappingReference> sourceParameterMappings, boolean hasNoMappings, Parameter sourceParameter)Extracts all relevant single target references and populates thesourceParameterMappingsif needed.private NestedTargetPropertyMappingHolder.GroupedSourceReferencesgroupByPoppedSourceReferences(java.util.Map.Entry<Parameter,java.util.Set<MappingReference>> entryByParam, java.util.Set<MappingReference> singleTargetReferences)Creates a nested grouping by popping the source mappings.private NestedTargetPropertyMappingHolder.GroupedBySourceParametersgroupBySourceParameter(java.util.Set<MappingReference> mappings, java.util.Set<MappingReference> singleTargetReferences)Splits the List of Mappings into possibly more Mappings based on each source method parameter type.private NestedTargetPropertyMappingHolder.GroupedTargetReferencesgroupByTargetReferences()The target references are popped.private voidhandleSourceParameterMappings(java.util.Set<MappingReference> sourceParameterMappings, java.lang.String targetProperty, Parameter sourceParameter, boolean forceUpdateMethod)Handle thePropertyMappingcreation for source parameter mappings.NestedTargetPropertyMappingHolder.BuildermappingContext(MappingBuilderContext mappingContext)NestedTargetPropertyMappingHolder.BuildermappingReferences(MappingReferences mappingReferences)NestedTargetPropertyMappingHolder.Buildermethod(Method method)private <K> voidpopulateWithSingleTargetReferences(java.util.Map<K,java.util.Set<MappingReference>> map, java.util.Set<MappingReference> singleTargetReferences, java.util.function.Function<SourceReference,K> keyExtractor)If a single target mapping has a validSourceReferenceand theSourceReferencehas more then 0PropertyEntryand if themapdoes not contain an entry with the extracted key then an entry with the extracted key and an empty list is added.NestedTargetPropertyMappingHolder.BuildertargetPropertiesWriteAccessors(java.util.Map<java.lang.String,Accessor> targetPropertiesWriteAccessors)NestedTargetPropertyMappingHolder.BuildertargetPropertyType(Type targetType)
-
-
-
Field Detail
-
method
private Method method
-
mappingReferences
private MappingReferences mappingReferences
-
mappingContext
private MappingBuilderContext mappingContext
-
existingVariableNames
private java.util.Set<java.lang.String> existingVariableNames
-
propertyMappings
private java.util.List<PropertyMapping> propertyMappings
-
handledTargets
private java.util.Set<java.lang.String> handledTargets
-
targetPropertiesWriteAccessors
private java.util.Map<java.lang.String,Accessor> targetPropertiesWriteAccessors
-
targetType
private Type targetType
-
errorOccurred
private boolean errorOccurred
-
-
Method Detail
-
mappingReferences
public NestedTargetPropertyMappingHolder.Builder mappingReferences(MappingReferences mappingReferences)
-
method
public NestedTargetPropertyMappingHolder.Builder method(Method method)
-
mappingContext
public NestedTargetPropertyMappingHolder.Builder mappingContext(MappingBuilderContext mappingContext)
-
existingVariableNames
public NestedTargetPropertyMappingHolder.Builder existingVariableNames(java.util.Set<java.lang.String> existingVariableNames)
-
targetPropertiesWriteAccessors
public NestedTargetPropertyMappingHolder.Builder targetPropertiesWriteAccessors(java.util.Map<java.lang.String,Accessor> targetPropertiesWriteAccessors)
-
targetPropertyType
public NestedTargetPropertyMappingHolder.Builder targetPropertyType(Type targetType)
-
build
public NestedTargetPropertyMappingHolder build()
-
handleSourceParameterMappings
private void handleSourceParameterMappings(java.util.Set<MappingReference> sourceParameterMappings, java.lang.String targetProperty, Parameter sourceParameter, boolean forceUpdateMethod)
Handle thePropertyMappingcreation for source parameter mappings.- Parameters:
sourceParameterMappings- the source parameter mappings for which property mapping should be donetargetProperty- the target property that is being mappedsourceParameter- the source parameter that is usedforceUpdateMethod- whether we need to force an update method
-
groupByTargetReferences
private NestedTargetPropertyMappingHolder.GroupedTargetReferences groupByTargetReferences()
The target references are popped. TheList<MappingOptions>are keyed on the unique first entries of the target references.Group all target references by their first property and for each such mapping use a new one where the first property will be removed from it. If a
Mappingcannot be popped, i.e. it contains a non nested target property just keep it as is (this is usually needed to control how an intermediary level can be mapped).We start with the following mappings:
@Mapping(target = "fish.kind", source = "fish.type"), @Mapping(target = "fish.name", ignore = true), @Mapping(target = "ornament", ignore = true ), @Mapping(target = "material.materialType", source = "material"), @Mapping(target = "document", source = "report"), @Mapping(target = "document.organisation.name", source = "report.organisationName")
We will get this:// All target references are popped and grouped by their first property GroupedTargetReferences.poppedTargetReferences { fish: @Mapping(target = "kind", source = "fish.type"), @Mapping(target = "name", ignore = true); material: @Mapping(target = "materialType", source = "material"); document: @Mapping(target = "organization.name", source = "report.organizationName"); } //This references are not nested and we they stay as they were GroupedTargetReferences.singleTargetReferences { document: @Mapping(target = "document", source = "report"); ornament: @Mapping(target = "ornament", ignore = true ); }- Returns:
- See above
-
groupBySourceParameter
private NestedTargetPropertyMappingHolder.GroupedBySourceParameters groupBySourceParameter(java.util.Set<MappingReference> mappings, java.util.Set<MappingReference> singleTargetReferences)
Splits the List of Mappings into possibly more Mappings based on each source method parameter type. Note: this method is used for forging nested update methods. For that purpose, the same method with all joined mappings should be generated. See also: NestedTargetPropertiesTest#shouldMapNestedComposedTarget Mappings:@Mapping(target = "organization.name", source = "report.organizationName");
singleTargetReferences:@Mapping(target = "document", source = "report");
We assume that all properties belong to the same source parameter (fish). We are getting this in return:GroupedBySourceParameters.groupedBySourceParameter { fish: @Mapping(target = "organization.name", source = "report.organizationName"); } GroupedBySourceParameters.notProcessedAppliesToAll {} //emptyNotice how thesingleTargetReferencesare missing. They are used for situations when there are mappings without source. Such as: Mappings:@Mapping(target = "organization.name", expression="java(\"Dunno\")");
singleTargetReferences:@Mapping(target = "document", source = "report");
The mappings have no source reference so we cannot extract the source parameter from them. When mappings have no source properties then we apply those to all of them. In this case we have a single target reference that can provide a source parameter. So we will get:GroupedBySourceParameters.groupedBySourceParameter { fish: @Mapping(target = "organization.name", expression="java(\"Dunno\")"); } GroupedBySourceParameters.notProcessedAppliesToAll {} //emptySee also how the
singleTargetReferencesare not part of the mappings. They are used only to make sure that their source parameter is taken into consideration in the next step.The
notProcessedAppliesToAllcontains all Mappings that should have been applied to all but have not because there were no other mappings that we could have used to pass them along. TheseMapping(s) are used later on for normal mapping.If we only had:
@Mapping(target = "document", source = "report"); @Mapping(target = "ornament", ignore = true );
Then we only would have had:GroupedBySourceParameters.notProcessedAppliesToAll { @Mapping(target = "document", source = "report"); @Mapping(target = "ornament", ignore = true ); }These mappings will be part of theGroupedBySourceParameters.notProcessedAppliesToAlland are used to be passed to the normal defined mapping.- Parameters:
mappings- that mappings that need to be used for the groupingsingleTargetReferences- a List containing all non-nested mappings for the same grouped target property as themappings- Returns:
- the split mapping options.
-
groupByPoppedSourceReferences
private NestedTargetPropertyMappingHolder.GroupedSourceReferences groupByPoppedSourceReferences(java.util.Map.Entry<Parameter,java.util.Set<MappingReference>> entryByParam, java.util.Set<MappingReference> singleTargetReferences)
Creates a nested grouping by popping the source mappings. See the description of the class to see what is generated. Mappings:@Mapping(target = "organization.name", source = "report.organizationName");
singleTargetReferences:@Mapping(target = "document", source = "report");
And we get:GroupedSourceReferences.groupedBySourceReferences { report: @Mapping(target = "organization.name", source = "organizationName"); }- Parameters:
entryByParam- the entry of aParameterand it's associatedMappingOptions(s) that need to be used for grouping on popped source referencessingleTargetReferences- the single target references that match the source mappings- Returns:
- the Grouped Source References
-
extractSingleTargetReferencesToUseAndPopulateSourceParameterMappings
private java.util.Set<MappingReference> extractSingleTargetReferencesToUseAndPopulateSourceParameterMappings(java.util.Set<MappingReference> singleTargetReferences, java.util.Set<MappingReference> sourceParameterMappings, boolean hasNoMappings, Parameter sourceParameter)
Extracts all relevant single target references and populates thesourceParameterMappingsif needed. A relevant single target reference mapping is a mapping that has a valid source reference and is for thesourceParameter. If there are no mappings i.e.hasNoMappings = trueand the source reference in the mapping has no property entries then add that to thesourceParameterMappings(mappings like this have found themselves here because there is a mapping method with multiple parameters and that are using the same sub-path in the target properties).- Parameters:
singleTargetReferences- All the single target references for a target propertysourceParameterMappings- a List that needs to be populated with valid mappings whenhasNoMappings = trueand there are no property entries in the source referencehasNoMappings- parameter indicating whether there were any extracted mappings for this target propertysourceParameter- the source parameter for which the grouping is being done- Returns:
- a list with valid single target references
-
createPropertyMappingForNestedTarget
private PropertyMapping createPropertyMappingForNestedTarget(MappingReferences mappingReferences, java.lang.String targetPropertyName, SourceReference sourceReference, boolean forceUpdateMethod)
-
populateWithSingleTargetReferences
private <K> void populateWithSingleTargetReferences(java.util.Map<K,java.util.Set<MappingReference>> map, java.util.Set<MappingReference> singleTargetReferences, java.util.function.Function<SourceReference,K> keyExtractor)
If a single target mapping has a validSourceReferenceand theSourceReferencehas more then 0PropertyEntryand if themapdoes not contain an entry with the extracted key then an entry with the extracted key and an empty list is added.- Parameters:
map- that needs to be populatedsingleTargetReferences- to usekeyExtractor- to be used to extract a key
-
-