Skip to content

Commit

Permalink
mapstruct#1057 Add forged named based parameter to distinguish betwee…
Browse files Browse the repository at this point in the history
…n our methods that MapStruct has to create and methods that we create for the internal framework
  • Loading branch information
filiphr committed Feb 19, 2017
1 parent ec1dbeb commit 8653712
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Assignment forgeMapping(SourceRHS sourceRHS, Type sourceType, Type targetType) {
shouldUsePropertyNamesInHistory(),
sourceRHS.getSourceErrorMessagePart()
),
null
null,
true
);

return createForgedBeanAssignment( sourceRHS, forgedMethod );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ private void reportErrorForUnmappedTargetPropertiesIfRequired() {
ReportingPolicyPrism unmappedTargetPolicy = getUnmappedTargetPolicy();

//we handle automapping forged methods differently than the usual source ones. in
if ( method instanceof ForgedMethod && ( (ForgedMethod) method ).isAutoMapping() ) {
if ( method instanceof ForgedMethod && ( (ForgedMethod) method ).isForgedNamedBased() ) {

ForgedMethod forgedMethod = (ForgedMethod) this.method;
if ( targetProperties.isEmpty() || !unprocessedTargetProperties.isEmpty() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public static class PropertyMappingBuilder extends MappingBuilderBase<PropertyMa
private SelectionParameters selectionParameters;
private MappingOptions forgeMethodWithMappingOptions;
private boolean forceUpdateMethod;
private boolean forgedNamedBased = true;

PropertyMappingBuilder() {
super( PropertyMappingBuilder.class );
Expand Down Expand Up @@ -229,6 +230,16 @@ public PropertyMappingBuilder forceUpdateMethod(boolean forceUpdateMethod) {
return this;
}

/**
* @param forgedNamedBased mapping is based on forging
*
* @return the builder for chaining
*/
public PropertyMappingBuilder forgedNamedBased(boolean forgedNamedBased) {
this.forgedNamedBased = forgedNamedBased;
return this;
}

public PropertyMapping build() {
// handle source
this.rightHandSide = getSourceRHS( sourceReference );
Expand Down Expand Up @@ -587,7 +598,8 @@ private ForgedMethod prepareForgedMethod(Type sourceType, Type targetType, Sourc
method.getContextParameters(),
method.getContextProvidedMethods(),
getForgedMethodHistory( source, suffix ),
null
null,
forgedNamedBased
);
}

Expand Down Expand Up @@ -638,7 +650,8 @@ private Assignment forgeMapping(SourceRHS sourceRHS) {
parameters,
method.getContextProvidedMethods(),
getForgedMethodHistory( sourceRHS ),
forgeMethodWithMappingOptions
forgeMethodWithMappingOptions,
forgedNamedBased
);
return createForgedBeanAssignment( sourceRHS, forgedMethod );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class ForgedMethod implements Method {
private final Parameter mappingTargetParameter;
private final MappingOptions mappingOptions;
private final ParameterProvidedMethods contextProvidedMethods;
private final boolean forgedNameBased;

/**
* Creates a new forged method with the given name.
Expand All @@ -75,7 +76,8 @@ public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfigu
additionalParameters,
parameterProvidedMethods,
null,
null );
null,
false );
}

/**
Expand All @@ -91,11 +93,12 @@ public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfigu
* parameters
* @param history a parent forged method if this is a forged method within a forged method
* @param mappingOptions the mapping options for this method
* @param forgedNameBased forges a name based (matched) mapping method
*/
public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfiguration mapperConfiguration,
ExecutableElement positionHintElement, List<Parameter> additionalParameters,
ParameterProvidedMethods parameterProvidedMethods, ForgedMethodHistory history,
MappingOptions mappingOptions) {
MappingOptions mappingOptions, boolean forgedNameBased) {
String sourceParamName = Strings.decapitalize( sourceType.getName() );
String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName );

Expand All @@ -116,6 +119,7 @@ public ForgedMethod(String name, Type sourceType, Type returnType, MapperConfigu
this.history = history;
this.mappingOptions = mappingOptions == null ? MappingOptions.empty() : mappingOptions;
this.mappingOptions.initWithParameter( sourceParameter );
this.forgedNameBased = forgedNameBased;
}

/**
Expand All @@ -138,6 +142,7 @@ public ForgedMethod(String name, ForgedMethod forgedMethod) {
this.contextProvidedMethods = forgedMethod.contextProvidedMethods;

this.name = name;
this.forgedNameBased = forgedMethod.forgedNameBased;
}

@Override
Expand Down Expand Up @@ -224,8 +229,8 @@ public ForgedMethodHistory getHistory() {
return history;
}

public boolean isAutoMapping() {
return mappingOptions.getValueMappings().isEmpty();
public boolean isForgedNamedBased() {
return forgedNameBased;
}

public void addThrownTypes(List<Type> thrownTypesToAdd) {
Expand Down

0 comments on commit 8653712

Please sign in to comment.