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 MappingBuilderBasepFad - (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: