mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#361 returning mutable objects iso immutable objects when RETURN_DEFAULT
This commit is contained in:
parent
5c323458fc
commit
403995af9a
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
@ -156,10 +155,6 @@ public class IterableMappingMethod extends MappingMethod {
|
||||
types.addAll( elementAssignment.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( mapNullToDefault ) {
|
||||
types.add( typeFactory.getType( Collections.class ) );
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
@ -201,10 +200,6 @@ public class MapMappingMethod extends MappingMethod {
|
||||
types.addAll( valueAssignment.getImportTypes() );
|
||||
}
|
||||
|
||||
if ( mapNullToDefault ) {
|
||||
types.add( typeFactory.getType( Collections.class ) );
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
|
@ -22,17 +22,17 @@
|
||||
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) <@throws/> {
|
||||
if ( ${sourceParameter.name} == null ) {
|
||||
<#if !mapNullToDefault>
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
<#else>
|
||||
return Collections.<${resultType.typeParameters[0].name}>emptyList();
|
||||
return <@returnObjectCreation/>;
|
||||
</#if>
|
||||
}
|
||||
|
||||
<#if existingInstanceMapping>
|
||||
${resultName}.clear();
|
||||
${resultName}.clear();
|
||||
<#else>
|
||||
<#-- Use the interface type on the left side, except it is java.lang.Iterable; use the implementation type - if present - on the right side -->
|
||||
<#if resultType.fullyQualifiedName == "java.lang.Iterable"><@includeModel object=resultType.implementationType/><#else><@includeModel object=resultType/></#if> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod/><#else>new <#if resultType.implementationType??><@includeModel object=resultType.implementationType/><#else><@includeModel object=resultType/></#if>()</#if>;
|
||||
<@localVarDefinition/> = <@returnObjectCreation/>;
|
||||
</#if>
|
||||
|
||||
for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) {
|
||||
@ -52,3 +52,26 @@
|
||||
</#list>
|
||||
</@compress>
|
||||
</#macro>
|
||||
<#macro localVarDefinition>
|
||||
<@compress single_line=true>
|
||||
<#if resultType.fullyQualifiedName == "java.lang.Iterable">
|
||||
<@includeModel object=resultType.implementationType/>
|
||||
<#else>
|
||||
<@includeModel object=resultType/>
|
||||
</#if> ${resultName}
|
||||
</@compress>
|
||||
</#macro>
|
||||
<#macro returnObjectCreation>
|
||||
<@compress single_line=true>
|
||||
<#if factoryMethod??>
|
||||
<@includeModel object=factoryMethod/>
|
||||
<#else>
|
||||
new
|
||||
<#if resultType.implementationType??>
|
||||
<@includeModel object=resultType.implementationType/>
|
||||
<#else>
|
||||
<@includeModel object=resultType/>
|
||||
</#if>()
|
||||
</#if>
|
||||
</@compress>
|
||||
</#macro>
|
@ -22,16 +22,16 @@
|
||||
<#lt>${accessibility.keyword} <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>) <@throws/> {
|
||||
if ( ${sourceParameter.name} == null ) {
|
||||
<#if !mapNullToDefault>
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
return<#if returnType.name != "void"> null</#if>;
|
||||
<#else>
|
||||
return Collections.<${resultType.typeParameters[0].name}, ${resultType.typeParameters[1].name}>emptyMap();
|
||||
return <@returnObjectCreation/>;
|
||||
</#if>
|
||||
}
|
||||
|
||||
<#if existingInstanceMapping>
|
||||
${resultName}.clear();
|
||||
${resultName}.clear();
|
||||
<#else>
|
||||
<@includeModel object=resultType /> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod/><#else>new <#if resultType.implementationType??><@includeModel object=resultType.implementationType /><#else><@includeModel object=resultType /></#if>()</#if>;
|
||||
<@includeModel object=resultType /> ${resultName} = <@returnObjectCreation/>;
|
||||
</#if>
|
||||
|
||||
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
||||
@ -62,3 +62,17 @@
|
||||
</#list>
|
||||
</@compress>
|
||||
</#macro>
|
||||
<#macro returnObjectCreation>
|
||||
<@compress single_line=true>
|
||||
<#if factoryMethod??>
|
||||
<@includeModel object=factoryMethod/>
|
||||
<#else>
|
||||
new
|
||||
<#if resultType.implementationType??>
|
||||
<@includeModel object=resultType.implementationType />
|
||||
<#else>
|
||||
<@includeModel object=resultType />
|
||||
</#if>()
|
||||
</#if>
|
||||
</@compress>
|
||||
</#macro>
|
Loading…
x
Reference in New Issue
Block a user