mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2005 Parameter type should only be checked if we are mapping from a single argument source
This commit is contained in:
parent
72e6b1feb5
commit
29008e12bf
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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" );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user