diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index 68c5601ac..0688178ce 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -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; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java index 72b1b0f10..20318f0fb 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -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; } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl index b1131aa3c..080c1cf5f 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl @@ -22,17 +22,17 @@ <#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) <@throws/> { if ( ${sourceParameter.name} == null ) { <#if !mapNullToDefault> - return<#if returnType.name != "void"> null; + return<#if returnType.name != "void"> null; <#else> - return Collections.<${resultType.typeParameters[0].name}>emptyList(); + return <@returnObjectCreation/>; } <#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/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod/><#else>new <#if resultType.implementationType??><@includeModel object=resultType.implementationType/><#else><@includeModel object=resultType/>(); + <@localVarDefinition/> = <@returnObjectCreation/>; for ( <@includeModel object=sourceParameter.type.typeParameters[0]/> ${loopVariableName} : ${sourceParameter.name} ) { @@ -51,4 +51,27 @@ <#if exceptionType_has_next>, + +<#macro localVarDefinition> + <@compress single_line=true> + <#if resultType.fullyQualifiedName == "java.lang.Iterable"> + <@includeModel object=resultType.implementationType/> + <#else> + <@includeModel object=resultType/> + ${resultName} + + +<#macro returnObjectCreation> + <@compress single_line=true> + <#if factoryMethod??> + <@includeModel object=factoryMethod/> + <#else> + new + <#if resultType.implementationType??> + <@includeModel object=resultType.implementationType/> + <#else> + <@includeModel object=resultType/> + () + + \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl index 3cc52a273..4aa59ff5a 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl @@ -22,16 +22,16 @@ <#lt>${accessibility.keyword} <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) <@throws/> { if ( ${sourceParameter.name} == null ) { <#if !mapNullToDefault> - return<#if returnType.name != "void"> null; + return<#if returnType.name != "void"> null; <#else> - return Collections.<${resultType.typeParameters[0].name}, ${resultType.typeParameters[1].name}>emptyMap(); + return <@returnObjectCreation/>; } <#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 />(); + <@includeModel object=resultType /> ${resultName} = <@returnObjectCreation/>; <#-- Once #148 has been addressed, the simple name of Map.Entry can be used --> @@ -61,4 +61,18 @@ <#if exceptionType_has_next>, + +<#macro returnObjectCreation> + <@compress single_line=true> + <#if factoryMethod??> + <@includeModel object=factoryMethod/> + <#else> + new + <#if resultType.implementationType??> + <@includeModel object=resultType.implementationType /> + <#else> + <@includeModel object=resultType /> + () + + \ No newline at end of file