mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#373 Flattening and simplifying generated forged method
This commit is contained in:
parent
21010bae7f
commit
c551a2d27d
@ -39,7 +39,6 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
*/
|
*/
|
||||||
public class NestedPropertyMappingMethod extends MappingMethod {
|
public class NestedPropertyMappingMethod extends MappingMethod {
|
||||||
|
|
||||||
private final List<String> existingVariableNames;
|
|
||||||
private final List<SafePropertyEntry> safePropertyEntries;
|
private final List<SafePropertyEntry> safePropertyEntries;
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
@ -68,15 +67,13 @@ public class NestedPropertyMappingMethod extends MappingMethod {
|
|||||||
safePropertyEntries.add( new SafePropertyEntry( propertyEntry, safeName ) );
|
safePropertyEntries.add( new SafePropertyEntry( propertyEntry, safeName ) );
|
||||||
existingVariableNames.add( safeName );
|
existingVariableNames.add( safeName );
|
||||||
}
|
}
|
||||||
return new NestedPropertyMappingMethod( method, safePropertyEntries, existingVariableNames );
|
return new NestedPropertyMappingMethod( method, safePropertyEntries );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private NestedPropertyMappingMethod( Method method, List<SafePropertyEntry> sourcePropertyEntries,
|
private NestedPropertyMappingMethod( Method method, List<SafePropertyEntry> sourcePropertyEntries ) {
|
||||||
List<String> existingVariableNames ) {
|
|
||||||
super( method );
|
super( method );
|
||||||
this.safePropertyEntries = sourcePropertyEntries;
|
this.safePropertyEntries = sourcePropertyEntries;
|
||||||
this.existingVariableNames = existingVariableNames;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Parameter getSourceParameter() {
|
public Parameter getSourceParameter() {
|
||||||
@ -101,10 +98,6 @@ public class NestedPropertyMappingMethod extends MappingMethod {
|
|||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTargetLocalVariable() {
|
|
||||||
return Strings.getSaveVariableName( getReturnType().getName(), existingVariableNames );
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -20,19 +20,16 @@
|
|||||||
-->
|
-->
|
||||||
<#lt>private <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
|
<#lt>private <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) {
|
||||||
|
|
||||||
<#compress>
|
if ( ${sourceParameter.name} == null ) {
|
||||||
<@includeModel object=returnType/> ${targetLocalVariable} = null;
|
return null;
|
||||||
if ( ${sourceParameter.name} != null ) {
|
|
||||||
<#list propertyEntries as entry>
|
|
||||||
<@includeModel object=entry.type/> ${entry.name} = <#if entry_index == 0>${sourceParameter.name}.${entry.accessor}<#else>${propertyEntries[entry_index-1].name}.${entry.accessor}</#if>;
|
|
||||||
if ( ${entry.name} != null ) {
|
|
||||||
<#if !entry_has_next>${targetLocalVariable} = ${entry.name};</#if>
|
|
||||||
</#list>
|
|
||||||
<#list propertyEntries as entry>
|
|
||||||
}
|
|
||||||
</#list>
|
|
||||||
}
|
}
|
||||||
return ${targetLocalVariable};
|
<#list propertyEntries as entry>
|
||||||
</#compress>
|
<@includeModel object=entry.type/> ${entry.name} = <#if entry_index == 0>${sourceParameter.name}.${entry.accessor}<#else>${propertyEntries[entry_index-1].name}.${entry.accessor}</#if>;
|
||||||
|
if ( ${entry.name} == null ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
<#if !entry_has_next>
|
||||||
|
return ${entry.name};
|
||||||
|
</#if>
|
||||||
|
</#list>
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user