#2005 Parameter type should only be checked if we are mapping from a single argument source

This commit is contained in:
Filip Hrisafov 2021-11-07 09:14:13 +01:00
parent 72e6b1feb5
commit 29008e12bf
3 changed files with 119 additions and 26 deletions

View File

@ -512,6 +512,7 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return false;
}
if ( sourceParameters.size() == 1 ) {
Type parameterType = sourceParameters.get( 0 ).getType();
if ( isStreamTypeOrIterableFromJavaStdLib( parameterType ) && !resultType.isIterableOrStreamType() ) {
@ -519,11 +520,6 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return false;
}
if ( containsTargetTypeParameter ) {
messager.printMessage( method, Message.RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER );
return false;
}
if ( !parameterType.isIterableOrStreamType() && isStreamTypeOrIterableFromJavaStdLib( resultType ) ) {
messager.printMessage( method, Message.RETRIEVAL_NON_ITERABLE_TO_ITERABLE );
return false;
@ -534,6 +530,24 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return false;
}
for ( Type typeParameter : parameterType.getTypeParameters() ) {
if ( typeParameter.hasSuperBound() ) {
messager.printMessage( method, Message.RETRIEVAL_WILDCARD_SUPER_BOUND_SOURCE );
return false;
}
if ( typeParameter.isTypeVar() ) {
messager.printMessage( method, Message.RETRIEVAL_TYPE_VAR_SOURCE );
return false;
}
}
}
if ( containsTargetTypeParameter ) {
messager.printMessage( method, Message.RETRIEVAL_MAPPING_HAS_TARGET_TYPE_PARAMETER );
return false;
}
if ( resultType.isPrimitive() ) {
messager.printMessage( method, Message.RETRIEVAL_PRIMITIVE_RETURN );
return false;
@ -550,18 +564,6 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
}
}
for ( Type typeParameter : parameterType.getTypeParameters() ) {
if ( typeParameter.hasSuperBound() ) {
messager.printMessage( method, Message.RETRIEVAL_WILDCARD_SUPER_BOUND_SOURCE );
return false;
}
if ( typeParameter.isTypeVar() ) {
messager.printMessage( method, Message.RETRIEVAL_TYPE_VAR_SOURCE );
return false;
}
}
return true;
}

View File

@ -0,0 +1,46 @@
/*
* 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.multisource;
import java.util.Collection;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface MultiSourceMapper {
MultiSourceMapper INSTANCE = Mappers.getMapper( MultiSourceMapper.class );
Target mapFromPrimitiveAndCollection(int value, Collection<String> elements);
Target mapFromCollectionAndPrimitive(Collection<String> elements, int value);
class Target {
private int value;
private Collection<String> elements;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Collection<String> getElements() {
return elements;
}
public void setElements(Collection<String> elements) {
this.elements = elements;
}
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.multisource;
import java.util.Collections;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
public class MultiSourceMapperTest {
@IssueKey("2005")
@ProcessorTest
@WithClasses({
MultiSourceMapper.class
})
void shouldBeAbleToMapFromPrimitiveAndCollectionAsMultiSource() {
MultiSourceMapper.Target target = MultiSourceMapper.INSTANCE.mapFromPrimitiveAndCollection(
10,
Collections.singleton( "test" )
);
assertThat( target ).isNotNull();
assertThat( target.getValue() ).isEqualTo( 10 );
assertThat( target.getElements() ).containsExactly( "test" );
target = MultiSourceMapper.INSTANCE.mapFromCollectionAndPrimitive(
Collections.singleton( "otherTest" ),
20
);
assertThat( target ).isNotNull();
assertThat( target.getValue() ).isEqualTo( 20 );
assertThat( target.getElements() ).containsExactly( "otherTest" );
}
}