mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2195 @Beanmapping#resultType should be used to construct return type also if it's a builder (#2196)
This commit is contained in:
parent
8b22654abd
commit
427f5023ef
@ -87,6 +87,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
|
|
||||||
private MappingBuilderContext ctx;
|
private MappingBuilderContext ctx;
|
||||||
private Method method;
|
private Method method;
|
||||||
|
private Type userDefinedReturnType;
|
||||||
|
|
||||||
/* returnType to construct can have a builder */
|
/* returnType to construct can have a builder */
|
||||||
private BuilderType returnTypeBuilder;
|
private BuilderType returnTypeBuilder;
|
||||||
@ -108,6 +109,11 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder userDefinedReturnType(Type userDefinedReturnType) {
|
||||||
|
this.userDefinedReturnType = userDefinedReturnType;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder returnTypeBuilder( BuilderType returnTypeBuilder ) {
|
public Builder returnTypeBuilder( BuilderType returnTypeBuilder ) {
|
||||||
this.returnTypeBuilder = returnTypeBuilder;
|
this.returnTypeBuilder = returnTypeBuilder;
|
||||||
return this;
|
return this;
|
||||||
@ -148,20 +154,22 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
// determine which return type to construct
|
// determine which return type to construct
|
||||||
boolean cannotConstructReturnType = false;
|
boolean cannotConstructReturnType = false;
|
||||||
if ( !method.getReturnType().isVoid() ) {
|
if ( !method.getReturnType().isVoid() ) {
|
||||||
Type returnTypeImpl = getReturnTypeToConstructFromSelectionParameters( selectionParameters );
|
Type returnTypeImpl = null;
|
||||||
if ( returnTypeImpl != null ) {
|
if ( isBuilderRequired() ) {
|
||||||
|
// the userDefinedReturn type can also require a builder. That buildertype is already set
|
||||||
|
returnTypeImpl = returnTypeBuilder.getBuilder();
|
||||||
initializeFactoryMethod( returnTypeImpl, selectionParameters );
|
initializeFactoryMethod( returnTypeImpl, selectionParameters );
|
||||||
if ( factoryMethod != null || canResultTypeFromBeanMappingBeConstructed( returnTypeImpl ) ) {
|
if ( factoryMethod != null || canReturnTypeBeConstructed( returnTypeImpl ) ) {
|
||||||
returnTypeToConstruct = returnTypeImpl;
|
returnTypeToConstruct = returnTypeImpl;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cannotConstructReturnType = true;
|
cannotConstructReturnType = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( isBuilderRequired() ) {
|
else if ( userDefinedReturnType != null ) {
|
||||||
returnTypeImpl = returnTypeBuilder.getBuilder();
|
returnTypeImpl = userDefinedReturnType;
|
||||||
initializeFactoryMethod( returnTypeImpl, selectionParameters );
|
initializeFactoryMethod( returnTypeImpl, selectionParameters );
|
||||||
if ( factoryMethod != null || canReturnTypeBeConstructed( returnTypeImpl ) ) {
|
if ( factoryMethod != null || canResultTypeFromBeanMappingBeConstructed( returnTypeImpl ) ) {
|
||||||
returnTypeToConstruct = returnTypeImpl;
|
returnTypeToConstruct = returnTypeImpl;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -482,16 +490,6 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Type getReturnTypeToConstructFromSelectionParameters(SelectionParameters selectionParams) {
|
|
||||||
// resultType only applies to method that actually has @BeanMapping annotation, never to forged methods
|
|
||||||
if ( !( method instanceof ForgedMethod )
|
|
||||||
&& selectionParams != null
|
|
||||||
&& selectionParams.getResultType() != null ) {
|
|
||||||
return ctx.getTypeFactory().getType( selectionParams.getResultType() );
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean canResultTypeFromBeanMappingBeConstructed(Type resultType) {
|
private boolean canResultTypeFromBeanMappingBeConstructed(Type resultType) {
|
||||||
|
|
||||||
boolean error = true;
|
boolean error = true;
|
||||||
|
@ -367,11 +367,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
else {
|
else {
|
||||||
this.messager.note( 1, Message.BEANMAPPING_CREATE_NOTE, method );
|
this.messager.note( 1, Message.BEANMAPPING_CREATE_NOTE, method );
|
||||||
BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
|
BuilderGem builder = method.getOptions().getBeanMapping().getBuilder();
|
||||||
|
Type userDefinedReturnType = getUserDesiredReturnType( method );
|
||||||
|
Type builderBaseType = userDefinedReturnType != null ? userDefinedReturnType : method.getReturnType();
|
||||||
BeanMappingMethod.Builder beanMappingBuilder = new BeanMappingMethod.Builder();
|
BeanMappingMethod.Builder beanMappingBuilder = new BeanMappingMethod.Builder();
|
||||||
BeanMappingMethod beanMappingMethod = beanMappingBuilder
|
BeanMappingMethod beanMappingMethod = beanMappingBuilder
|
||||||
.mappingContext( mappingContext )
|
.mappingContext( mappingContext )
|
||||||
.sourceMethod( method )
|
.sourceMethod( method )
|
||||||
.returnTypeBuilder( typeFactory.builderTypeFor( method.getReturnType(), builder ) )
|
.userDefinedReturnType( userDefinedReturnType )
|
||||||
|
.returnTypeBuilder( typeFactory.builderTypeFor( builderBaseType, builder ) )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// We can consider that the bean mapping method can always be constructed. If there is a problem
|
// We can consider that the bean mapping method can always be constructed. If there is a problem
|
||||||
@ -392,6 +395,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
return mappingMethods;
|
return mappingMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Type getUserDesiredReturnType(SourceMethod method) {
|
||||||
|
SelectionParameters selectionParameters = method.getOptions().getBeanMapping().getSelectionParameters();
|
||||||
|
if ( selectionParameters != null && selectionParameters.getResultType() != null ) {
|
||||||
|
return typeFactory.getType( selectionParameters.getResultType() );
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private <M extends ContainerMappingMethod> M createWithElementMappingMethod(SourceMethod method,
|
private <M extends ContainerMappingMethod> M createWithElementMappingMethod(SourceMethod method,
|
||||||
MappingMethodOptions mappingMethodOptions, ContainerMappingMethodBuilder<?, M> builder) {
|
MappingMethodOptions mappingMethodOptions, ContainerMappingMethodBuilder<?, M> builder) {
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* 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._2195;
|
||||||
|
|
||||||
|
import org.mapstruct.BeanMapping;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.Source;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.Target;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.TargetBase;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface Issue2195Mapper {
|
||||||
|
|
||||||
|
Issue2195Mapper INSTANCE = Mappers.getMapper( Issue2195Mapper.class );
|
||||||
|
|
||||||
|
@BeanMapping( resultType = Target.class )
|
||||||
|
TargetBase map(Source source);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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._2195;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.Source;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.Target;
|
||||||
|
import org.mapstruct.ap.test.bugs._2195.dto.TargetBase;
|
||||||
|
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("2195")
|
||||||
|
@WithClasses( { Source.class, Target.class, TargetBase.class } )
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
public class Issue2195Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( Issue2195Mapper.class )
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setName( "JohnDoe" );
|
||||||
|
|
||||||
|
TargetBase target = Issue2195Mapper.INSTANCE.map( source );
|
||||||
|
|
||||||
|
assertThat( target ).isInstanceOf( Target.class );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* 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._2195.dto;
|
||||||
|
|
||||||
|
public class Source {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -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._2195.dto;
|
||||||
|
|
||||||
|
public class Target extends TargetBase {
|
||||||
|
|
||||||
|
protected Target(Builder builder) {
|
||||||
|
super( builder );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder extends TargetBase.Builder {
|
||||||
|
|
||||||
|
protected Builder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Target build() {
|
||||||
|
return new Target( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder name(String name) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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._2195.dto;
|
||||||
|
|
||||||
|
public class TargetBase {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
protected TargetBase(Builder builder) {
|
||||||
|
this.name = builder.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
|
||||||
|
protected Builder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public TargetBase build() {
|
||||||
|
return new TargetBase( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder name(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user