mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
This commit is contained in:
parent
7dcbef349d
commit
e0eb0f6bb8
@ -1068,10 +1068,23 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
// its a plain-old property mapping
|
// its a plain-old property mapping
|
||||||
else {
|
else {
|
||||||
|
|
||||||
// determine source parameter
|
|
||||||
SourceReference sourceRef = mappingRef.getSourceReference();
|
SourceReference sourceRef = mappingRef.getSourceReference();
|
||||||
|
// sourceRef is not defined, check if a source property has the same name
|
||||||
if ( sourceRef == null && method.getSourceParameters().size() == 1 ) {
|
if ( sourceRef == null && method.getSourceParameters().size() == 1 ) {
|
||||||
sourceRef = getSourceRef( method.getSourceParameters().get( 0 ), targetPropertyName );
|
sourceRef = getSourceRefByTargetName( method.getSourceParameters().get( 0 ), targetPropertyName );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( sourceRef == null ) {
|
||||||
|
// still no match. Try if one of the parameters has the same name
|
||||||
|
sourceRef = method.getSourceParameters()
|
||||||
|
.stream()
|
||||||
|
.filter( p -> targetPropertyName.equals( p.getName() ) )
|
||||||
|
.findAny()
|
||||||
|
.map( p -> new SourceReference.BuilderFromProperty()
|
||||||
|
.sourceParameter( p )
|
||||||
|
.name( targetPropertyName )
|
||||||
|
.build() )
|
||||||
|
.orElse( null );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( sourceRef != null ) {
|
if ( sourceRef != null ) {
|
||||||
@ -1153,7 +1166,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
List<SourceReference> sourceReferences = new ArrayList<>();
|
List<SourceReference> sourceReferences = new ArrayList<>();
|
||||||
for ( String targetPropertyName : unprocessedTargetProperties.keySet() ) {
|
for ( String targetPropertyName : unprocessedTargetProperties.keySet() ) {
|
||||||
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
||||||
SourceReference sourceRef = getSourceRef( sourceParameter, targetPropertyName );
|
SourceReference sourceRef = getSourceRefByTargetName( sourceParameter, targetPropertyName );
|
||||||
if ( sourceRef != null ) {
|
if ( sourceRef != null ) {
|
||||||
sourceReferences.add( sourceRef );
|
sourceReferences.add( sourceRef );
|
||||||
}
|
}
|
||||||
@ -1251,7 +1264,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SourceReference getSourceRef(Parameter sourceParameter, String targetPropertyName) {
|
private SourceReference getSourceRefByTargetName(Parameter sourceParameter, String targetPropertyName) {
|
||||||
|
|
||||||
SourceReference sourceRef = null;
|
SourceReference sourceRef = null;
|
||||||
|
|
||||||
@ -1261,11 +1274,11 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
|
|
||||||
Accessor sourceReadAccessor =
|
Accessor sourceReadAccessor =
|
||||||
sourceParameter.getType().getPropertyReadAccessors().get( targetPropertyName );
|
sourceParameter.getType().getPropertyReadAccessors().get( targetPropertyName );
|
||||||
|
|
||||||
Accessor sourcePresenceChecker =
|
|
||||||
sourceParameter.getType().getPropertyPresenceCheckers().get( targetPropertyName );
|
|
||||||
|
|
||||||
if ( sourceReadAccessor != null ) {
|
if ( sourceReadAccessor != null ) {
|
||||||
|
// property mapping
|
||||||
|
Accessor sourcePresenceChecker =
|
||||||
|
sourceParameter.getType().getPropertyPresenceCheckers().get( targetPropertyName );
|
||||||
|
|
||||||
DeclaredType declaredSourceType = (DeclaredType) sourceParameter.getType().getTypeMirror();
|
DeclaredType declaredSourceType = (DeclaredType) sourceParameter.getType().getTypeMirror();
|
||||||
Type returnType = ctx.getTypeFactory().getReturnType( declaredSourceType, sourceReadAccessor );
|
Type returnType = ctx.getTypeFactory().getReturnType( declaredSourceType, sourceReadAccessor );
|
||||||
sourceRef = new SourceReference.BuilderFromProperty().sourceParameter( sourceParameter )
|
sourceRef = new SourceReference.BuilderFromProperty().sourceParameter( sourceParameter )
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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._2164;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface Issue2164Mapper {
|
||||||
|
|
||||||
|
Issue2164Mapper INSTANCE = Mappers.getMapper( Issue2164Mapper.class );
|
||||||
|
|
||||||
|
@Mapping(target = "value", qualifiedByName = "truncate2")
|
||||||
|
Target map(BigDecimal value);
|
||||||
|
|
||||||
|
@Named( "truncate2" )
|
||||||
|
default String truncate2(String in) {
|
||||||
|
return in.substring( 0, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
class Target {
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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._2164;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@IssueKey("2164")
|
||||||
|
@WithClasses(Issue2164Mapper.class)
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
public class Issue2164Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSelectProperMethod() {
|
||||||
|
|
||||||
|
Issue2164Mapper.Target target = Issue2164Mapper.INSTANCE.map( new BigDecimal( "1234" ) );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getValue() ).isEqualTo( "12" );
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user