mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2109 Generate correct array assignment for constructor parameter mapping
This commit is contained in:
parent
d87d75a7a8
commit
da37d40152
@ -30,31 +30,26 @@
|
|||||||
<#list sourceParametersExcludingPrimitives as sourceParam>
|
<#list sourceParametersExcludingPrimitives as sourceParam>
|
||||||
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||||
</#list>
|
</#list>
|
||||||
if ( ${sourceParam.name} != null ) {
|
if ( ${sourceParam.name} != null ) {
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||||
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
||||||
</#list>
|
</#list>
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
|
||||||
${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
|
||||||
</#list>
|
|
||||||
}
|
|
||||||
</#if>
|
</#if>
|
||||||
</#list>
|
</#list>
|
||||||
<#list sourcePrimitiveParameters as sourceParam>
|
<#list sourcePrimitiveParameters as sourceParam>
|
||||||
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||||
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
||||||
</#list>
|
</#list>
|
||||||
</#if>
|
</#if>
|
||||||
</#list>
|
</#list>
|
||||||
<#else>
|
<#else>
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
||||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||||
</#list>
|
</#list>
|
||||||
<#if mapNullToDefault>if ( ${sourceParameters[0].name} != null ) {</#if>
|
<#if mapNullToDefault>if ( ${sourceParameters[0].name} != null ) {</#if>
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
||||||
@ -62,11 +57,6 @@
|
|||||||
</#list>
|
</#list>
|
||||||
<#if mapNullToDefault>
|
<#if mapNullToDefault>
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
|
||||||
${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
|
||||||
</#list>
|
|
||||||
}
|
|
||||||
</#if>
|
</#if>
|
||||||
</#if>
|
</#if>
|
||||||
<#list constructorConstantMappings as constantMapping>
|
<#list constructorConstantMappings as constantMapping>
|
||||||
|
@ -18,10 +18,6 @@
|
|||||||
<@lib.handleLocalVarNullCheck needs_explicit_local_var=directAssignment>
|
<@lib.handleLocalVarNullCheck needs_explicit_local_var=directAssignment>
|
||||||
<#if ext.targetBeanName?has_content>${ext.targetBeanName}.</#if>${ext.targetWriteAccessorName}<@lib.handleWrite><#if directAssignment><@wrapLocalVarInCollectionInitializer/><#else><@lib.handleWithAssignmentOrNullCheckVar/></#if></@lib.handleWrite>;
|
<#if ext.targetBeanName?has_content>${ext.targetBeanName}.</#if>${ext.targetWriteAccessorName}<@lib.handleWrite><#if directAssignment><@wrapLocalVarInCollectionInitializer/><#else><@lib.handleWithAssignmentOrNullCheckVar/></#if></@lib.handleWrite>;
|
||||||
</@lib.handleLocalVarNullCheck>
|
</@lib.handleLocalVarNullCheck>
|
||||||
<#if !ext.defaultValueAssignment?? && !sourcePresenceCheckerReference?? && !ext.targetBeanName?has_content>else {<#-- the opposite (defaultValueAssignment) case is handeld inside lib.handleLocalVarNullCheck -->
|
|
||||||
${ext.targetWriteAccessorName}<@lib.handleWrite>null</@lib.handleWrite>;
|
|
||||||
}
|
|
||||||
</#if>
|
|
||||||
</#macro>
|
</#macro>
|
||||||
<#--
|
<#--
|
||||||
wraps the local variable in a collection initializer (new collection, or EnumSet.copyOf)
|
wraps the local variable in a collection initializer (new collection, or EnumSet.copyOf)
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2109;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface Issue2109Mapper {
|
||||||
|
|
||||||
|
Issue2109Mapper INSTANCE = Mappers.getMapper( Issue2109Mapper.class );
|
||||||
|
|
||||||
|
Target map(Source source);
|
||||||
|
|
||||||
|
@Mapping(target = "data", defaultExpression = "java(new byte[0])")
|
||||||
|
Target mapWithEmptyData(Source source);
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2109;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2109")
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
@WithClasses({
|
||||||
|
Issue2109Mapper.class,
|
||||||
|
Source.class,
|
||||||
|
Target.class,
|
||||||
|
})
|
||||||
|
public class Issue2109Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCorrectlyMapArrayInConstructorMapping() {
|
||||||
|
Target target = Issue2109Mapper.INSTANCE.map( new Source( 100L, new byte[] { 100, 120, 40, 40 } ) );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getId() ).isEqualTo( 100L );
|
||||||
|
assertThat( target.getData() ).containsExactly( 100, 120, 40, 40 );
|
||||||
|
|
||||||
|
target = Issue2109Mapper.INSTANCE.map( new Source( 50L, null ) );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getId() ).isEqualTo( 50L );
|
||||||
|
assertThat( target.getData() ).isNull();
|
||||||
|
|
||||||
|
target = Issue2109Mapper.INSTANCE.mapWithEmptyData( new Source( 100L, new byte[] { 100, 120, 40, 40 } ) );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getId() ).isEqualTo( 100L );
|
||||||
|
assertThat( target.getData() ).containsExactly( 100, 120, 40, 40 );
|
||||||
|
|
||||||
|
target = Issue2109Mapper.INSTANCE.mapWithEmptyData( new Source( 50L, null ) );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getId() ).isEqualTo( 50L );
|
||||||
|
assertThat( target.getData() ).isEmpty();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2109;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Source {
|
||||||
|
|
||||||
|
private final Long id;
|
||||||
|
private final byte[] data;
|
||||||
|
|
||||||
|
public Source(Long id, byte[] data) {
|
||||||
|
this.id = id;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._2109;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Target {
|
||||||
|
|
||||||
|
private final Long id;
|
||||||
|
private final byte[] data;
|
||||||
|
|
||||||
|
public Target(Long id, byte[] data) {
|
||||||
|
this.id = id;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
@ -26,36 +26,24 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String chartName;
|
String chartName = null;
|
||||||
if ( chart != null ) {
|
if ( chart != null ) {
|
||||||
chartName = chart.getName();
|
chartName = chart.getName();
|
||||||
}
|
}
|
||||||
else {
|
String songTitle = null;
|
||||||
chartName = null;
|
String artistName = null;
|
||||||
}
|
String recordedAt = null;
|
||||||
String songTitle;
|
String city = null;
|
||||||
String artistName;
|
|
||||||
String recordedAt;
|
|
||||||
String city;
|
|
||||||
if ( song != null ) {
|
if ( song != null ) {
|
||||||
songTitle = song.getTitle();
|
songTitle = song.getTitle();
|
||||||
artistName = songArtistName( song );
|
artistName = songArtistName( song );
|
||||||
recordedAt = songArtistLabelStudioName( song );
|
recordedAt = songArtistLabelStudioName( song );
|
||||||
city = songArtistLabelStudioCity( song );
|
city = songArtistLabelStudioCity( song );
|
||||||
}
|
}
|
||||||
else {
|
int position1 = 0;
|
||||||
songTitle = null;
|
|
||||||
artistName = null;
|
|
||||||
recordedAt = null;
|
|
||||||
city = null;
|
|
||||||
}
|
|
||||||
int position1;
|
|
||||||
if ( position != null ) {
|
if ( position != null ) {
|
||||||
position1 = position;
|
position1 = position;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
position1 = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChartEntry chartEntry = new ChartEntry( chartName, songTitle, artistName, recordedAt, city, position1 );
|
ChartEntry chartEntry = new ChartEntry( chartName, songTitle, artistName, recordedAt, city, position1 );
|
||||||
|
|
||||||
@ -68,10 +56,10 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String songTitle;
|
String songTitle = null;
|
||||||
String artistName;
|
String artistName = null;
|
||||||
String recordedAt;
|
String recordedAt = null;
|
||||||
String city;
|
String city = null;
|
||||||
|
|
||||||
songTitle = song.getTitle();
|
songTitle = song.getTitle();
|
||||||
artistName = songArtistName( song );
|
artistName = songArtistName( song );
|
||||||
@ -92,7 +80,7 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String chartName;
|
String chartName = null;
|
||||||
|
|
||||||
chartName = name.getName();
|
chartName = name.getName();
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Song song;
|
Song song = null;
|
||||||
String name;
|
String name = null;
|
||||||
|
|
||||||
song = chartEntryToSong( chartEntry );
|
song = chartEntryToSong( chartEntry );
|
||||||
name = chartEntry.getChartName();
|
name = chartEntry.getChartName();
|
||||||
@ -46,12 +46,12 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String chartName;
|
String chartName = null;
|
||||||
String songTitle;
|
String songTitle = null;
|
||||||
String artistName;
|
String artistName = null;
|
||||||
String recordedAt;
|
String recordedAt = null;
|
||||||
String city;
|
String city = null;
|
||||||
int position;
|
int position = 0;
|
||||||
|
|
||||||
chartName = chart.getName();
|
chartName = chart.getName();
|
||||||
songTitle = chartSongTitle( chart );
|
songTitle = chartSongTitle( chart );
|
||||||
@ -70,8 +70,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String name;
|
String name = null;
|
||||||
String city;
|
String city = null;
|
||||||
|
|
||||||
name = chartEntry.getRecordedAt();
|
name = chartEntry.getRecordedAt();
|
||||||
city = chartEntry.getCity();
|
city = chartEntry.getCity();
|
||||||
@ -86,7 +86,7 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Studio studio;
|
Studio studio = null;
|
||||||
|
|
||||||
studio = chartEntryToStudio( chartEntry );
|
studio = chartEntryToStudio( chartEntry );
|
||||||
|
|
||||||
@ -102,8 +102,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Label label;
|
Label label = null;
|
||||||
String name;
|
String name = null;
|
||||||
|
|
||||||
label = chartEntryToLabel( chartEntry );
|
label = chartEntryToLabel( chartEntry );
|
||||||
name = chartEntry.getArtistName();
|
name = chartEntry.getArtistName();
|
||||||
@ -118,9 +118,9 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Artist artist;
|
Artist artist = null;
|
||||||
String title;
|
String title = null;
|
||||||
List<Integer> positions;
|
List<Integer> positions = null;
|
||||||
|
|
||||||
artist = chartEntryToArtist( chartEntry );
|
artist = chartEntryToArtist( chartEntry );
|
||||||
title = chartEntry.getSongTitle();
|
title = chartEntry.getSongTitle();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user