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;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
@ -156,10 +155,6 @@ public class IterableMappingMethod extends MappingMethod {
|
|||||||
types.addAll( elementAssignment.getImportTypes() );
|
types.addAll( elementAssignment.getImportTypes() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( mapNullToDefault ) {
|
|
||||||
types.add( typeFactory.getType( Collections.class ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
@ -201,10 +200,6 @@ public class MapMappingMethod extends MappingMethod {
|
|||||||
types.addAll( valueAssignment.getImportTypes() );
|
types.addAll( valueAssignment.getImportTypes() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( mapNullToDefault ) {
|
|
||||||
types.add( typeFactory.getType( Collections.class ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
return types;
|
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/> {
|
<#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 ( ${sourceParameter.name} == null ) {
|
||||||
<#if !mapNullToDefault>
|
<#if !mapNullToDefault>
|
||||||
return<#if returnType.name != "void"> null</#if>;
|
return<#if returnType.name != "void"> null</#if>;
|
||||||
<#else>
|
<#else>
|
||||||
return Collections.<${resultType.typeParameters[0].name}>emptyList();
|
return <@returnObjectCreation/>;
|
||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
|
|
||||||
<#if existingInstanceMapping>
|
<#if existingInstanceMapping>
|
||||||
${resultName}.clear();
|
${resultName}.clear();
|
||||||
<#else>
|
<#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 -->
|
<#-- 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>
|
</#if>
|
||||||
|
|
||||||
for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) {
|
for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) {
|
||||||
@ -51,4 +51,27 @@
|
|||||||
<#if exceptionType_has_next>, </#if>
|
<#if exceptionType_has_next>, </#if>
|
||||||
</#list>
|
</#list>
|
||||||
</@compress>
|
</@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>
|
</#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/> {
|
<#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 ( ${sourceParameter.name} == null ) {
|
||||||
<#if !mapNullToDefault>
|
<#if !mapNullToDefault>
|
||||||
return<#if returnType.name != "void"> null</#if>;
|
return<#if returnType.name != "void"> null</#if>;
|
||||||
<#else>
|
<#else>
|
||||||
return Collections.<${resultType.typeParameters[0].name}, ${resultType.typeParameters[1].name}>emptyMap();
|
return <@returnObjectCreation/>;
|
||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
|
|
||||||
<#if existingInstanceMapping>
|
<#if existingInstanceMapping>
|
||||||
${resultName}.clear();
|
${resultName}.clear();
|
||||||
<#else>
|
<#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>
|
</#if>
|
||||||
|
|
||||||
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
||||||
@ -61,4 +61,18 @@
|
|||||||
<#if exceptionType_has_next>, </#if>
|
<#if exceptionType_has_next>, </#if>
|
||||||
</#list>
|
</#list>
|
||||||
</@compress>
|
</@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>
|
</#macro>
|
Loading…
x
Reference in New Issue
Block a user