From 6f51cf4a8a2a04eb29722a4402efb52b072c5761 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Fri, 10 Feb 2017 22:21:07 +0100 Subject: [PATCH] #1057 Towards controlling name based mapping from root @Mapping --- .../model/AbstractMappingMethodBuilder.java | 3 +- .../ap/internal/model/PropertyMapping.java | 41 ++++------- .../internal/model/source/ForgedMethod.java | 72 ++----------------- .../ap/internal/model/source/Mapping.java | 2 +- 4 files changed, 22 insertions(+), 96 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java b/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java index cf1735b7b4..5be3fb186e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java @@ -68,7 +68,8 @@ Assignment forgeMapping(SourceRHS sourceRHS, Type sourceType, Type targetType) { targetType, shouldUsePropertyNamesInHistory(), sourceRHS.getSourceErrorMessagePart() - ) + ), + null ); return createForgedBeanAssignment( sourceRHS, forgedMethod ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index 9eb1290acd..e036e1d906 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -222,6 +222,7 @@ public PropertyMappingBuilder forgeMethodWithMappingOptions(MappingOptions mappi * Force the created mapping to use update methods when forging a method. * * @param forceUpdateMethod whether the mapping should force update method for forged mappings + * @return the builder for chaining */ public PropertyMappingBuilder forceUpdateMethod(boolean forceUpdateMethod) { this.forceUpdateMethod = forceUpdateMethod; @@ -585,7 +586,8 @@ private ForgedMethod prepareForgedMethod(Type sourceType, Type targetType, Sourc element, method.getContextParameters(), method.getContextProvidedMethods(), - getForgedMethodHistory( source, suffix ) + getForgedMethodHistory( source, suffix ), + null ); } @@ -627,32 +629,17 @@ private Assignment forgeMapping(SourceRHS sourceRHS) { else { returnType = targetType; } - ForgedMethod forgedMethod; - if ( forgeMethodWithMappingOptions != null ) { - forgedMethod = new ForgedMethod( - name, - sourceType, - returnType, - method.getMapperConfiguration(), - method.getExecutable(), - parameters, - method.getContextProvidedMethods(), - forgeMethodWithMappingOptions - ); - } - else { - forgedMethod = new ForgedMethod( - name, - sourceType, - returnType, - method.getMapperConfiguration(), - method.getExecutable(), - parameters, - method.getContextProvidedMethods(), - getForgedMethodHistory( sourceRHS ) - ); - } - + ForgedMethod forgedMethod = new ForgedMethod( + name, + sourceType, + returnType, + method.getMapperConfiguration(), + method.getExecutable(), + parameters, + method.getContextProvidedMethods(), + getForgedMethodHistory( sourceRHS ), + forgeMethodWithMappingOptions + ); return createForgedBeanAssignment( sourceRHS, forgedMethod ); } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java index 2a890b802a..88517f6c10 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java @@ -49,7 +49,6 @@ public class ForgedMethod implements Method { private final List contextParameters; private final Parameter mappingTargetParameter; private final MappingOptions mappingOptions; - private final boolean autoMapping; private final ParameterProvidedMethods contextProvidedMethods; /** @@ -76,66 +75,7 @@ public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfigu additionalParameters, parameterProvidedMethods, null, - false, - MappingOptions.empty() ); - } - - /** - * Creates a new forged method with the given name with history - * - * @param name the (unique name) for this method - * @param sourceType the source type - * @param returnType the return type. - * @param mapperConfiguration the mapper configuration - * @param positionHintElement element used to for reference to the position in the source file. - * @param additionalParameters additional parameters to add to the forged method - * @param parameterProvidedMethods additional factory/lifecycle methods to consider that are provided by context - * parameters - * @param history a parent forged method if this is a forged method within a forged method - */ - public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfiguration mapperConfiguration, - ExecutableElement positionHintElement, List additionalParameters, - ParameterProvidedMethods parameterProvidedMethods, ForgedMethodHistory history) { - this( - name, - sourceType, - returnType, - mapperConfiguration, - positionHintElement, - additionalParameters, - parameterProvidedMethods, - history, - true, - MappingOptions.empty() ); - } - - /** - * Creates a new forged method with the given name with mapping options - * - * @param name the (unique name) for this method - * @param sourceType the source type - * @param returnType the return type. - * @param mapperConfiguration the mapper configuration - * @param positionHintElement element used to for reference to the position in the source file. - * @param additionalParameters additional parameters to add to the forged method - * @param parameterProvidedMethods additional factory/lifecycle methods to consider that are provided by context - * parameters - * @param mappingOptions with mapping options - */ - public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfiguration mapperConfiguration, - ExecutableElement positionHintElement, List additionalParameters, - ParameterProvidedMethods parameterProvidedMethods, MappingOptions mappingOptions) { - this( - name, - sourceType, - returnType, - mapperConfiguration, - positionHintElement, - additionalParameters, - parameterProvidedMethods, - null, - false, - mappingOptions ); + null ); } /** @@ -152,10 +92,10 @@ public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfigu * @param history a parent forged method if this is a forged method within a forged method * @param mappingOptions the mapping options for this method */ - private ForgedMethod(String name, Type sourceType, Type returnType, MapperConfiguration mapperConfiguration, + public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfiguration mapperConfiguration, ExecutableElement positionHintElement, List additionalParameters, ParameterProvidedMethods parameterProvidedMethods, ForgedMethodHistory history, - boolean autoMapping, MappingOptions mappingOptions) { + MappingOptions mappingOptions) { String sourceParamName = Strings.decapitalize( sourceType.getName() ); String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName ); @@ -174,8 +114,7 @@ private ForgedMethod(String name, Type sourceType, Type returnType, MapperConfig this.mapperConfiguration = mapperConfiguration; this.positionHintElement = positionHintElement; this.history = history; - this.autoMapping = autoMapping; - this.mappingOptions = mappingOptions; + this.mappingOptions = mappingOptions == null ? MappingOptions.empty() : mappingOptions; this.mappingOptions.initWithParameter( sourceParameter ); } @@ -191,7 +130,6 @@ public ForgedMethod(String name, ForgedMethod forgedMethod) { this.mapperConfiguration = forgedMethod.mapperConfiguration; this.positionHintElement = forgedMethod.positionHintElement; this.history = forgedMethod.history; - this.autoMapping = forgedMethod.autoMapping; this.sourceParameters = Parameter.getSourceParameters( parameters ); this.contextParameters = Parameter.getContextParameters( parameters ); @@ -287,7 +225,7 @@ public ForgedMethodHistory getHistory() { } public boolean isAutoMapping() { - return autoMapping; + return mappingOptions.getValueMappings().isEmpty(); } public void addThrownTypes(List thrownTypesToAdd) { diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java index e3517ea015..9eac9f92da 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/Mapping.java @@ -249,7 +249,7 @@ public void init(SourceMethod method, FormattingMessager messager, TypeFactory t /** * Initializes the mapping with a new source parameter. * - * @param sourceParameter + * @param sourceParameter sets the source parameter that acts as a basis for this mapping */ public void init( Parameter sourceParameter ) { if ( sourceReference != null ) {