Content-Length: 14601 | pFad | http://github.com/mapstruct/mapstruct/pull/3616.diff

thub.com diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index cf5179f9cb..68ad765e74 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -235,7 +235,7 @@ else if ( !method.isUpdateMethod() ) { this.unprocessedTargetProperties = new LinkedHashMap<>( accessors ); - if ( !method.isUpdateMethod() && !hasFactoryMethod ) { + if ( !method.isUpdateMethod() && !hasFactoryMethod && !isCorrectlyDefinedTargetThisForEnumTarget() ) { ConstructorAccessor constructorAccessor = getConstructorAccessor( resultTypeToMap ); if ( constructorAccessor != null ) { @@ -310,6 +310,8 @@ else if ( !method.isUpdateMethod() ) { // map parameters without a mapping applyParameterNameBasedMapping(); + // map this enum mapping + applyTargetThisMappingForEnumTarget(); } // Process the unprocessed defined targets @@ -514,7 +516,8 @@ private SubclassMapping createSubclassMapping(SubclassMappingOptions subclassMap private boolean isAbstractReturnTypeAllowed() { return !method.getOptions().getSubclassMappings().isEmpty() && ( method.getOptions().getBeanMapping().getSubclassExhaustiveStrategy().isAbstractReturnTypeAllowed() - || isCorrectlySealed() ); + || isCorrectlySealed() ) + || isCorrectlyDefinedTargetThisForEnumTarget(); } private boolean isCorrectlySealed() { @@ -756,7 +759,9 @@ else if ( !returnType.hasAccessibleConstructor() ) { private boolean isReturnTypeAbstractOrCanBeConstructed(Type returnType) { boolean error = true; - if ( !returnType.isAbstract() && !returnType.hasAccessibleConstructor() ) { + if ( !returnType.isAbstract() + && !returnType.hasAccessibleConstructor() + && !isCorrectlyDefinedTargetThisForEnumTarget() ) { ctx .getMessager() .printMessage( @@ -1532,6 +1537,38 @@ private void applyTargetThisMapping() { } } + /** + * When a single target this mapping is present, and the current target type is an enum. + */ + private void applyTargetThisMappingForEnumTarget() { + if ( !isCorrectlyDefinedTargetThisForEnumTarget() ) { + return; + } + + for ( MappingReference targetThis : this.targetThisReferences ) { + PropertyMapping propertyMapping = new PropertyMappingBuilder().mappingContext( ctx ) + .sourceMethod( method ) + .sourcePropertyName( "test" ) + .sourceReference( targetThis.getSourceReference() ) + .targetType( this.method.getReturnType() ) + .forgedNamedBased( false ) + .existingVariableNames( existingVariableNames ) + .forgeMethodWithMappingReferences( MappingReferences.empty() ) + .options( method.getOptions().getBeanMapping() ) + .build(); + + if ( propertyMapping != null ) { + propertyMappings.add( propertyMapping ); + } + } + } + + private boolean isCorrectlyDefinedTargetThisForEnumTarget() { + return this.method.getOptions().getMappings().size() == 1 + && this.method.getOptions().getMappings().iterator().next().getTargetName().equals( "." ) + && this.method.getReturnType().isEnumType(); + } + /** * Iterates over all target properties and all source parameters. *

@@ -2115,6 +2152,10 @@ private boolean doesNotNeedPresenceCheckForSourceParameter(PropertyMapping mappi return mapping.getAssignment().isSourceReferenceParameter(); } + public boolean shouldDirectlyReturnOnlyMappingResult() { + return this.returnTypeToConstruct.isEnumType() && this.propertyMappings.size() == 1; + } + @Override public int hashCode() { //Needed for Checkstyle, otherwise it fails due to EqualsHashCode rule diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index 2f30957962..9ed3a19827 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -5,12 +5,22 @@ */ package org.mapstruct.ap.internal.model; +import static org.mapstruct.ap.internal.gem.NullValueCheckStrategyGem.ALWAYS; +import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.IGNORE; +import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.SET_TO_DEFAULT; +import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.SET_TO_NULL; +import static org.mapstruct.ap.internal.model.ForgedMethod.forElementMapping; +import static org.mapstruct.ap.internal.model.ForgedMethod.forParameterMapping; +import static org.mapstruct.ap.internal.model.ForgedMethod.forPropertyMapping; +import static org.mapstruct.ap.internal.model.common.Assignment.AssignmentType.DIRECT; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Set; + import javax.lang.model.element.AnnotationMirror; import org.mapstruct.ap.internal.gem.BuilderGem; @@ -20,6 +30,7 @@ import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper; import org.mapstruct.ap.internal.model.assignment.EnumConstantWrapper; import org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps; +import org.mapstruct.ap.internal.model.assignment.ResultWrapper; import org.mapstruct.ap.internal.model.assignment.SetterWrapper; import org.mapstruct.ap.internal.model.assignment.StreamAdderWrapper; import org.mapstruct.ap.internal.model.assignment.UpdateWrapper; @@ -51,15 +62,6 @@ import org.mapstruct.ap.internal.util.accessor.AccessorType; import org.mapstruct.ap.internal.util.accessor.ReadAccessor; -import static org.mapstruct.ap.internal.gem.NullValueCheckStrategyGem.ALWAYS; -import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.IGNORE; -import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.SET_TO_DEFAULT; -import static org.mapstruct.ap.internal.gem.NullValuePropertyMappingStrategyGem.SET_TO_NULL; -import static org.mapstruct.ap.internal.model.ForgedMethod.forElementMapping; -import static org.mapstruct.ap.internal.model.ForgedMethod.forParameterMapping; -import static org.mapstruct.ap.internal.model.ForgedMethod.forPropertyMapping; -import static org.mapstruct.ap.internal.model.common.Assignment.AssignmentType.DIRECT; - /** * Represents the mapping between a source and target property, e.g. from {@code String Source#foo} to * {@code int Target#bar}. Name and type of source and target property can differ. If they have different types, the @@ -160,6 +162,11 @@ public static class PropertyMappingBuilder extends MappingBuilderBase ${resultName} = <@includeModel object=factoryMethod targetType=returnTypeToConstruct/>; - <#else > + <#elseif shouldDirectlyReturnOnlyMappingResult()> + <@includeModel object=returnTypeToConstruct/> ${resultName}; + <#else> <@includeModel object=returnTypeToConstruct/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod targetType=returnTypeToConstruct/><#else>new <@includeModel object=returnTypeToConstruct/>(); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ResultWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ResultWrapper.ftl new file mode 100644 index 0000000000..3589058d1d --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ResultWrapper.ftl @@ -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 + +--> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.ReturnWrapper" --> +${ext.targetBeanName} = <@_assignment/>; +<#macro _assignment> + <@includeModel object=assignment + targetBeanName=ext.targetBeanName + existingInstanceMapping=ext.existingInstanceMapping + targetReadAccessorName=ext.targetReadAccessorName + targetWriteAccessorName=ext.targetWriteAccessorName + targetPropertyName=ext.targetPropertyName + targetType=ext.targetType/> + diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Mapper.java new file mode 100644 index 0000000000..ee7373a95c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Mapper.java @@ -0,0 +1,48 @@ +/* + * 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._2421; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingConstants; +import org.mapstruct.ValueMapping; + +@Mapper +abstract class Issue2421Mapper { + + @Mapping( target = ".", source = "value" ) + @ValueMapping( target = "C", source = MappingConstants.ANY_REMAINING ) + public abstract EnumExample dtoTestToEnum(InnerDtoTest innerDtoTest); + +} + +enum EnumExample { + A, B, C +} + +class InnerDtoTest { + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} + +class TargetTest { + private EnumExample enumExampleValue; + + public EnumExample getEnumExampleValue() { + return enumExampleValue; + } + + public void setEnumExampleValue(EnumExample enumExampleValue) { + this.enumExampleValue = enumExampleValue; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Test.java new file mode 100644 index 0000000000..dc7a64afe3 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2421/Issue2421Test.java @@ -0,0 +1,17 @@ +/* + * 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._2421; + +import org.mapstruct.ap.testutil.ProcessorTest; +import org.mapstruct.ap.testutil.WithClasses; + +public class Issue2421Test { + + @ProcessorTest + @WithClasses( Issue2421Mapper.class ) + void shouldCompile() { + } +}









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/mapstruct/mapstruct/pull/3616.diff

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy