#1707 fix for not defining local variable in stream-iterable mapping (#1708)

This commit is contained in:
Sjaak Derksen 2019-02-10 10:31:51 +01:00 committed by GitHub
parent eca7433103
commit 3c079e412a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 193 additions and 5 deletions

View File

@ -48,11 +48,11 @@
</#if>
}
<#-- A variable needs to be defined if there are before mappings and this is not exisitingInstanceMapping -->
<#assign needVarDefine = beforeMappingReferencesWithMappingTarget?has_content && !existingInstanceMapping />
<#-- A variable needs to be defined if there are before or after mappings and this is not exisitingInstanceMapping -->
<#assign needVarDefine = (beforeMappingReferencesWithMappingTarget?has_content || afterMappingReferences?has_content) && !existingInstanceMapping />
<#if resultType.arrayType>
<#if !existingInstanceMapping && needVarDefine>
<#if needVarDefine>
<#assign needVarDefine = false />
<#-- We create a null array which later will be directly assigned from the stream-->
${resultElementType}[] ${resultName} = null;
@ -67,7 +67,7 @@
</#if>
<#else>
<#-- Streams are immutable so we can't update them -->
<#if !existingInstanceMapping && needVarDefine>
<#if needVarDefine>
<#assign needVarDefine = false />
<@iterableLocalVarDef/> ${resultName} = Stream.empty();
</#if>
@ -180,5 +180,5 @@
</@compress>
</#macro>
<#macro returnLocalVarDefOrUpdate>
<#if canReturnImmediatelly><#if returnType.name != "void">return </#if><#elseif !needVarDefine><@iterableLocalVarDef/> ${resultName} = <#else>${resultName} = </#if><#nested />
<#if canReturnImmediatelly><#if returnType.name != "void">return </#if><#elseif needVarDefine><@iterableLocalVarDef/> ${resultName} = <#else>${resultName} = </#if><#nested />
</#macro>

View File

@ -0,0 +1,89 @@
/*
* 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._1707;
import java.util.Set;
import java.util.stream.Stream;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
@Mapper
public abstract class Converter {
public abstract Set<Target> convert(Stream<Source> source);
public abstract Target[] convertArray(Stream<Source> source);
@Mapping( target = "custom", ignore = true )
public abstract Target convert(Source source);
@AfterMapping
public void addCustomValue(@MappingTarget Set<Target> targetList) {
targetList.forEach( t -> t.custom = true );
}
@AfterMapping
public void addCustomValue(@MappingTarget Target[] targetArray) {
for ( Target target : targetArray ) {
target.custom = true;
}
}
public static final class Source {
private String text;
private int number;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public static class Target {
private String text;
private int number;
private boolean custom;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public boolean isCustom() {
return custom;
}
public void setCustom(boolean custom) {
this.custom = custom;
}
}
}

View File

@ -0,0 +1,32 @@
/*
* 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._1707;
import org.junit.Rule;
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 org.mapstruct.ap.testutil.runner.GeneratedSource;
@RunWith(AnnotationProcessorTestRunner.class)
@IssueKey("1707")
@WithClasses({
Converter.class
})
public class Issue1707Test {
@Rule
public final GeneratedSource generatedSource = new GeneratedSource().addComparisonToFixtureFor(
Converter.class
);
@Test
public void codeShouldBeGeneratedCorrectly() {
}
}

View File

@ -0,0 +1,67 @@
/*
* 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._1707;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-02-10T09:58:11+0100",
comments = "version: , compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"
)
public class ConverterImpl extends Converter {
@Override
public Set<Target> convert(Stream<Source> source) {
if ( source == null ) {
return null;
}
Set<Target> set = new HashSet<Target>();
set.addAll( source.map( source1 -> convert( source1 ) )
.collect( Collectors.toCollection( HashSet<Target>::new ) )
);
addCustomValue( set );
return set;
}
@Override
public org.mapstruct.ap.test.bugs._1707.Converter.Target[] convertArray(Stream<Source> source) {
if ( source == null ) {
return null;
}
org.mapstruct.ap.test.bugs._1707.Converter.Target[] targetTmp = null;
targetTmp = source.map( source1 -> convert( source1 ) )
.toArray( Target[]::new );
addCustomValue( targetTmp );
return targetTmp;
}
@Override
public Target convert(Source source) {
if ( source == null ) {
return null;
}
Target target = new Target();
target.setText( source.getText() );
target.setNumber( source.getNumber() );
return target;
}
}