mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1131 Use SourceRHS source type for update methods factories
If a SourceRHS is present then the source type of the SourceRHS and the MappingContext parameters are considered for the factory method selection, i.e. the other source parameters are ignored
This commit is contained in:
parent
3ebd09eec9
commit
3004ea28c5
@ -30,7 +30,7 @@ import java.lang.annotation.Target;
|
||||
* return type that is assignable to the required object type is present, then the factory method is used instead.
|
||||
* <p>
|
||||
* Factory methods can be defined without parameters, with an {@code @}{@link TargetType} parameter, a {@code @}
|
||||
* {@link Context} parameter, or with a mapping methods source parameter. If any of those parameters are defined, then
|
||||
* {@link Context} parameter, or with the mapping source parameter. If any of those parameters are defined, then
|
||||
* the mapping method that is supposed to use the factory method needs to be declared with an assignable result type,
|
||||
* assignable context parameter, and/or assignable source types.
|
||||
* <p>
|
||||
|
@ -1579,7 +1579,7 @@ The mapping of enum to enum via the `@Mapping` annotation is *DEPRECATED*. It wi
|
||||
[[object-factories]]
|
||||
== Object factories
|
||||
|
||||
By default, the generated code for mapping one bean type into another will call the default constructor to instantiate the target type.
|
||||
By default, the generated code for mapping one bean type into another or updating a bean will call the default constructor to instantiate the target type.
|
||||
|
||||
Alternatively you can plug in custom object factories which will be invoked to obtain instances of the target type. One use case for this is JAXB which creates `ObjectFactory` classes for obtaining new instances of schema types.
|
||||
|
||||
@ -1613,7 +1613,7 @@ public class EntityFactory {
|
||||
@Mapper(uses= { DtoFactory.class, EntityFactory.class } )
|
||||
public interface CarMapper {
|
||||
|
||||
OrderMapper INSTANCE = Mappers.getMapper( CarMapper.class );
|
||||
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
|
||||
|
||||
CarDto carToCarDto(Car car);
|
||||
|
||||
@ -1659,6 +1659,74 @@ public class CarMapperImpl implements CarMapper {
|
||||
----
|
||||
====
|
||||
|
||||
.Custom object factories with update methods
|
||||
====
|
||||
[source, java, linenums]
|
||||
[subs="verbatim,attributes"]
|
||||
----
|
||||
@Mapper(uses = { DtoFactory.class, EntityFactory.class, CarMapper.class } )
|
||||
public interface OwnerMapper {
|
||||
|
||||
OwnerMapper INSTANCE = Mappers.getMapper( OwnerMapper.class );
|
||||
|
||||
void updateOwnerDto(Owner owner, @MappingTarget OwnerDto ownerDto);
|
||||
|
||||
void updateOwner(OwnerDto ownerDto, @MappingTarget Owner owner);
|
||||
}
|
||||
----
|
||||
[source, java, linenums]
|
||||
[subs="verbatim,attributes"]
|
||||
----
|
||||
//GENERATED CODE
|
||||
public class OwnerMapperImpl implements OwnerMapper {
|
||||
|
||||
private final DtoFactory dtoFactory = new DtoFactory();
|
||||
|
||||
private final EntityFactory entityFactory = new EntityFactory();
|
||||
|
||||
private final OwnerMapper ownerMapper = Mappers.getMapper( OwnerMapper.class );
|
||||
|
||||
@Override
|
||||
public void updateOwnerDto(Owner owner, @MappingTarget OwnerDto ownerDto) {
|
||||
if ( owner == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( owner.getCar() != null ) {
|
||||
if ( ownerDto.getCar() == null ) {
|
||||
ownerDto.setCar( dtoFactory.createCarDto() );
|
||||
}
|
||||
// update car within ownerDto
|
||||
}
|
||||
else {
|
||||
ownerDto.setCar( null );
|
||||
}
|
||||
|
||||
// updating other properties
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOwner(OwnerDto ownerDto, @MappingTarget Owner owner) {
|
||||
if ( ownerDto == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ownerDto.getCar() != null ) {
|
||||
if ( owner.getCar() == null ) {
|
||||
owner.setCar( entityFactory.createEntity( Car.class ) );
|
||||
}
|
||||
// update car within owner
|
||||
}
|
||||
else {
|
||||
owner.setCar( null );
|
||||
}
|
||||
|
||||
// updating other properties
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
In addition, annotating a factory method with `@ObjectFactory` lets you gain access to the mapping sources.
|
||||
Source objects can be added as parameters in the same way as for mapping method. The `@ObjectFactory`
|
||||
annotation is necessary to let MapStruct know that the given method is only a factory method.
|
||||
|
@ -20,7 +20,7 @@ package org.mapstruct.ap.internal.conversion;
|
||||
|
||||
import java.util.List;
|
||||
import org.mapstruct.ap.internal.model.TypeConversion;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.HelperMethod;
|
||||
|
||||
|
@ -27,10 +27,10 @@ import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.HelperMethod;
|
||||
import org.mapstruct.ap.internal.model.TypeConversion;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -20,7 +20,7 @@ package org.mapstruct.ap.internal.conversion;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.HelperMethod;
|
||||
|
||||
|
@ -23,7 +23,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.TypeConversion;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.HelperMethod;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
@ -18,8 +18,9 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ParameterBinding;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
import org.mapstruct.ap.internal.model.source.MappingMethodUtils;
|
||||
|
@ -19,7 +19,8 @@
|
||||
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethodHistory;
|
||||
|
@ -18,14 +18,16 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps;
|
||||
import org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps;
|
||||
import org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMaps;
|
||||
import org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMapsWithNullCheck;
|
||||
import org.mapstruct.ap.internal.model.assignment.UpdateWrapper;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
|
||||
import org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism;
|
||||
import org.mapstruct.ap.internal.util.Message;
|
||||
@ -65,7 +67,8 @@ public class CollectionAssignmentBuilder {
|
||||
private Type targetType;
|
||||
private String targetPropertyName;
|
||||
private PropertyMapping.TargetWriteAccessorType targetAccessorType;
|
||||
private Assignment rhs;
|
||||
private Assignment assignment;
|
||||
private SourceRHS sourceRHS;
|
||||
|
||||
public CollectionAssignmentBuilder mappingBuilderContext(MappingBuilderContext ctx) {
|
||||
this.ctx = ctx;
|
||||
@ -97,13 +100,28 @@ public class CollectionAssignmentBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CollectionAssignmentBuilder rightHandSide(Assignment rhs) {
|
||||
this.rhs = rhs;
|
||||
/**
|
||||
* @param assignment the assignment that needs to be invoked
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public CollectionAssignmentBuilder assignment(Assignment assignment) {
|
||||
this.assignment = assignment;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sourceRHS the source right hand side for getting the property for mapping
|
||||
*
|
||||
* @return this builder for chaining
|
||||
*/
|
||||
public CollectionAssignmentBuilder rightHandSide(SourceRHS sourceRHS) {
|
||||
this.sourceRHS = sourceRHS;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Assignment build() {
|
||||
Assignment result = rhs;
|
||||
Assignment result = assignment;
|
||||
|
||||
CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy();
|
||||
boolean targetImmutable = cms == CollectionMappingStrategyPrism.TARGET_IMMUTABLE;
|
||||
@ -121,7 +139,8 @@ public class CollectionAssignmentBuilder {
|
||||
targetPropertyName
|
||||
);
|
||||
}
|
||||
Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null );
|
||||
Assignment factoryMethod = ctx.getMappingResolver()
|
||||
.getFactoryMethod( method, targetType, SelectionParameters.forSourceRHS( sourceRHS ) );
|
||||
result = new UpdateWrapper(
|
||||
result,
|
||||
method.getThrownTypes(),
|
||||
|
@ -22,7 +22,7 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
|
@ -25,10 +25,11 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
|
||||
|
@ -23,9 +23,9 @@ import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.SetterWrapper;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
|
@ -24,12 +24,13 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.LocalVarWrapper;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
import org.mapstruct.ap.internal.prism.NullValueMappingStrategyPrism;
|
||||
|
@ -28,8 +28,9 @@ import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.util.Elements;
|
||||
import javax.lang.model.util.Types;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
|
@ -24,7 +24,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.ParameterBinding;
|
||||
|
@ -18,7 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.DIRECT;
|
||||
import static org.mapstruct.ap.internal.model.common.Assignment.AssignmentType.DIRECT;
|
||||
import static org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism.ALWAYS;
|
||||
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
import static org.mapstruct.ap.internal.util.Collections.last;
|
||||
@ -34,14 +34,15 @@ import javax.lang.model.type.DeclaredType;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.AdderWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.EnumConstantWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps;
|
||||
import org.mapstruct.ap.internal.model.assignment.SetterWrapper;
|
||||
import org.mapstruct.ap.internal.model.assignment.UpdateWrapper;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethod;
|
||||
import org.mapstruct.ap.internal.model.source.ForgedMethodHistory;
|
||||
@ -393,7 +394,8 @@ public class PropertyMapping extends ModelElement {
|
||||
targetPropertyName
|
||||
);
|
||||
}
|
||||
Assignment factory = ctx.getMappingResolver().getFactoryMethod( method, targetType, null );
|
||||
Assignment factory = ctx.getMappingResolver()
|
||||
.getFactoryMethod( method, targetType, SelectionParameters.forSourceRHS( rightHandSide ) );
|
||||
return new UpdateWrapper( rhs, method.getThrownTypes(), factory, isFieldAssignment(), targetType,
|
||||
!rhs.isSourceReferenceParameter() );
|
||||
}
|
||||
@ -426,7 +428,8 @@ public class PropertyMapping extends ModelElement {
|
||||
.targetType( targetType )
|
||||
.targetPropertyName( targetPropertyName )
|
||||
.targetAccessorType( targetAccessorType )
|
||||
.rightHandSide( rhs )
|
||||
.rightHandSide( rightHandSide )
|
||||
.assignment( rhs )
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -26,8 +26,8 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
|
@ -22,7 +22,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
|
@ -23,6 +23,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.util.Nouns;
|
||||
|
||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
|
@ -20,6 +20,8 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism;
|
||||
|
@ -22,6 +22,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
@ -53,7 +54,7 @@ public class Java8FunctionWrapper extends AssignmentWrapper {
|
||||
/**
|
||||
*
|
||||
* @return {@code true} if the wrapped assignment is
|
||||
* {@link org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType#DIRECT}, {@code false} otherwise
|
||||
* {@link Assignment.AssignmentType#DIRECT}, {@code false} otherwise
|
||||
*/
|
||||
public boolean isDirectAssignment() {
|
||||
return getAssignment().getType() == AssignmentType.DIRECT;
|
||||
|
@ -23,6 +23,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.prism.NullValueCheckStrategyPrism;
|
||||
|
||||
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -23,10 +23,11 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
|
||||
import static org.mapstruct.ap.internal.model.assignment.Assignment.AssignmentType.DIRECT;
|
||||
import static org.mapstruct.ap.internal.model.common.Assignment.AssignmentType.DIRECT;
|
||||
|
||||
/**
|
||||
* This wrapper handles the situation where an assignment is done via the setter and a null check is needed.
|
||||
|
@ -23,6 +23,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
@ -51,19 +52,19 @@ public class UpdateWrapper extends AssignmentWrapper {
|
||||
}
|
||||
|
||||
private static Type determineImplType(Assignment factoryMethod, Type targetType) {
|
||||
if ( factoryMethod != null ) {
|
||||
//If we have factory method then we won't use the targetType
|
||||
return null;
|
||||
}
|
||||
if ( targetType.getImplementationType() != null ) {
|
||||
// it's probably a collection or something
|
||||
return targetType.getImplementationType();
|
||||
}
|
||||
|
||||
if ( factoryMethod == null ) {
|
||||
// no factory method means we create a new instance ourself and thus need to import the type
|
||||
return targetType;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Type> getThrownTypes() {
|
||||
List<Type> parentThrownTypes = super.getThrownTypes();
|
||||
@ -82,6 +83,9 @@ public class UpdateWrapper extends AssignmentWrapper {
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> imported = new HashSet<Type>();
|
||||
imported.addAll( super.getImportTypes() );
|
||||
if ( factoryMethod != null ) {
|
||||
imported.addAll( factoryMethod.getImportTypes() );
|
||||
}
|
||||
if ( targetImplementationType != null ) {
|
||||
imported.add( targetImplementationType );
|
||||
imported.addAll( targetImplementationType.getTypeParameters() );
|
||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.internal.model.assignment;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
|
@ -16,13 +16,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.assignment;
|
||||
package org.mapstruct.ap.internal.model.common;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
|
||||
/**
|
||||
* Assignment represents all kind of manners a source can be assigned to a target.
|
||||
*
|
@ -35,14 +35,16 @@ public class ParameterBinding {
|
||||
private final boolean targetType;
|
||||
private final boolean mappingTarget;
|
||||
private final boolean mappingContext;
|
||||
private final SourceRHS sourceRHS;
|
||||
|
||||
private ParameterBinding(Type parameterType, String variableName, boolean mappingTarget, boolean targetType,
|
||||
boolean mappingContext) {
|
||||
boolean mappingContext, SourceRHS sourceRHS) {
|
||||
this.type = parameterType;
|
||||
this.variableName = variableName;
|
||||
this.targetType = targetType;
|
||||
this.mappingTarget = mappingTarget;
|
||||
this.mappingContext = mappingContext;
|
||||
this.sourceRHS = sourceRHS;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,11 +82,22 @@ public class ParameterBinding {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the sourceRHS that this parameter is bound to
|
||||
*/
|
||||
public SourceRHS getSourceRHS() {
|
||||
return sourceRHS;
|
||||
}
|
||||
|
||||
public Set<Type> getImportTypes() {
|
||||
if ( targetType ) {
|
||||
return type.getImportTypes();
|
||||
}
|
||||
|
||||
if ( sourceRHS != null ) {
|
||||
return sourceRHS.getImportTypes();
|
||||
}
|
||||
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@ -98,7 +111,9 @@ public class ParameterBinding {
|
||||
parameter.getName(),
|
||||
parameter.isMappingTarget(),
|
||||
parameter.isTargetType(),
|
||||
parameter.isMappingContext() );
|
||||
parameter.isMappingContext(),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
public static List<ParameterBinding> fromParameters(List<Parameter> parameters) {
|
||||
@ -114,7 +129,7 @@ public class ParameterBinding {
|
||||
* @return a parameter binding representing a target type parameter
|
||||
*/
|
||||
public static ParameterBinding forTargetTypeBinding(Type classTypeOf) {
|
||||
return new ParameterBinding( classTypeOf, null, false, true, false );
|
||||
return new ParameterBinding( classTypeOf, null, false, true, false, null );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,7 +137,7 @@ public class ParameterBinding {
|
||||
* @return a parameter binding representing a mapping target parameter
|
||||
*/
|
||||
public static ParameterBinding forMappingTargetBinding(Type resultType) {
|
||||
return new ParameterBinding( resultType, null, true, false, false );
|
||||
return new ParameterBinding( resultType, null, true, false, false, null );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,6 +145,10 @@ public class ParameterBinding {
|
||||
* @return a parameter binding representing a mapping source type
|
||||
*/
|
||||
public static ParameterBinding forSourceTypeBinding(Type sourceType) {
|
||||
return new ParameterBinding( sourceType, null, false, false, false );
|
||||
return new ParameterBinding( sourceType, null, false, false, false, null );
|
||||
}
|
||||
|
||||
public static ParameterBinding fromSourceRHS(SourceRHS sourceRHS) {
|
||||
return new ParameterBinding( sourceRHS.getSourceType(), null, false, false, false, sourceRHS );
|
||||
}
|
||||
}
|
||||
|
@ -16,15 +16,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model;
|
||||
package org.mapstruct.ap.internal.model.common;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ModelElement;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.util.Strings;
|
||||
|
||||
/**
|
@ -18,10 +18,13 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.source;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.Types;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
|
||||
/**
|
||||
* Holding parameters common to the selection process, common to IterableMapping, BeanMapping, PropertyMapping and
|
||||
* MapMapping
|
||||
@ -34,13 +37,20 @@ public class SelectionParameters {
|
||||
private final List<String> qualifyingNames;
|
||||
private final TypeMirror resultType;
|
||||
private final Types typeUtils;
|
||||
private final SourceRHS sourceRHS;
|
||||
|
||||
public SelectionParameters(List<TypeMirror> qualifiers, List<String> qualifyingNames, TypeMirror resultType,
|
||||
Types typeUtils) {
|
||||
this( qualifiers, qualifyingNames, resultType, typeUtils, null );
|
||||
}
|
||||
|
||||
private SelectionParameters(List<TypeMirror> qualifiers, List<String> qualifyingNames, TypeMirror resultType,
|
||||
Types typeUtils, SourceRHS sourceRHS) {
|
||||
this.qualifiers = qualifiers;
|
||||
this.qualifyingNames = qualifyingNames;
|
||||
this.resultType = resultType;
|
||||
this.typeUtils = typeUtils;
|
||||
this.sourceRHS = sourceRHS;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,6 +78,13 @@ public class SelectionParameters {
|
||||
return resultType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return sourceRHS used for further selection of an appropriate factory method
|
||||
*/
|
||||
public SourceRHS getSourceRHS() {
|
||||
return sourceRHS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 3;
|
||||
@ -97,6 +114,10 @@ public class SelectionParameters {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !equals( this.sourceRHS, other.sourceRHS ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return equals( this.resultType, other.resultType );
|
||||
}
|
||||
|
||||
@ -133,4 +154,14 @@ public class SelectionParameters {
|
||||
return mirror2 != null && typeUtils.isSameType( mirror1, mirror2 );
|
||||
}
|
||||
}
|
||||
|
||||
public static SelectionParameters forSourceRHS(SourceRHS sourceRHS) {
|
||||
return new SelectionParameters(
|
||||
Collections.<TypeMirror>emptyList(),
|
||||
Collections.<String>emptyList(),
|
||||
null,
|
||||
null,
|
||||
sourceRHS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import java.util.List;
|
||||
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
|
||||
/**
|
||||
@ -36,6 +37,7 @@ public class SelectionCriteria {
|
||||
private final List<String> qualifiedByNames = new ArrayList<String>();
|
||||
private final String targetPropertyName;
|
||||
private final TypeMirror qualifyingResultType;
|
||||
private final SourceRHS sourceRHS;
|
||||
private boolean preferUpdateMapping;
|
||||
private final boolean objectFactoryRequired;
|
||||
private final boolean lifecycleCallbackRequired;
|
||||
@ -47,9 +49,11 @@ public class SelectionCriteria {
|
||||
qualifiers.addAll( selectionParameters.getQualifiers() );
|
||||
qualifiedByNames.addAll( selectionParameters.getQualifyingNames() );
|
||||
qualifyingResultType = selectionParameters.getResultType();
|
||||
sourceRHS = selectionParameters.getSourceRHS();
|
||||
}
|
||||
else {
|
||||
this.qualifyingResultType = null;
|
||||
sourceRHS = null;
|
||||
}
|
||||
this.targetPropertyName = targetPropertyName;
|
||||
this.preferUpdateMapping = preferUpdateMapping;
|
||||
@ -91,6 +95,10 @@ public class SelectionCriteria {
|
||||
return preferUpdateMapping;
|
||||
}
|
||||
|
||||
public SourceRHS getSourceRHS() {
|
||||
return sourceRHS;
|
||||
}
|
||||
|
||||
public void setPreferUpdateMapping(boolean preferUpdateMapping) {
|
||||
this.preferUpdateMapping = preferUpdateMapping;
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.ParameterBinding;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
@ -59,7 +60,11 @@ public class TypeSelector implements MethodSelector {
|
||||
List<ParameterBinding> availableBindings;
|
||||
if ( sourceTypes.isEmpty() ) {
|
||||
// if no source types are given, we have a factory or lifecycle method
|
||||
availableBindings = getAvailableParameterBindingsFromMethod( mappingMethod, targetType );
|
||||
availableBindings = getAvailableParameterBindingsFromMethod(
|
||||
mappingMethod,
|
||||
targetType,
|
||||
criteria.getSourceRHS()
|
||||
);
|
||||
}
|
||||
else {
|
||||
availableBindings = getAvailableParameterBindingsFromSourceTypes( sourceTypes, targetType, mappingMethod );
|
||||
@ -81,11 +86,18 @@ public class TypeSelector implements MethodSelector {
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ParameterBinding> getAvailableParameterBindingsFromMethod(Method method, Type targetType) {
|
||||
List<ParameterBinding> availableParams = new ArrayList<ParameterBinding>( method.getParameters().size() + 2 );
|
||||
private List<ParameterBinding> getAvailableParameterBindingsFromMethod(Method method, Type targetType,
|
||||
SourceRHS sourceRHS) {
|
||||
List<ParameterBinding> availableParams = new ArrayList<ParameterBinding>( method.getParameters().size() + 3 );
|
||||
|
||||
availableParams.addAll( ParameterBinding.fromParameters( method.getParameters() ) );
|
||||
addMappingTargetAndTargetTypeBindings( availableParams, targetType );
|
||||
if ( sourceRHS != null ) {
|
||||
availableParams.addAll( ParameterBinding.fromParameters( method.getContextParameters() ) );
|
||||
availableParams.add( ParameterBinding.fromSourceRHS( sourceRHS ) );
|
||||
}
|
||||
else {
|
||||
availableParams.addAll( ParameterBinding.fromParameters( method.getParameters() ) );
|
||||
}
|
||||
|
||||
return availableParams;
|
||||
}
|
||||
|
@ -42,14 +42,14 @@ import org.mapstruct.ap.internal.model.HelperMethod;
|
||||
import org.mapstruct.ap.internal.model.MapperReference;
|
||||
import org.mapstruct.ap.internal.model.MappingBuilderContext.MappingResolver;
|
||||
import org.mapstruct.ap.internal.model.MethodReference;
|
||||
import org.mapstruct.ap.internal.model.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.VirtualMappingMethod;
|
||||
import org.mapstruct.ap.internal.model.assignment.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||
import org.mapstruct.ap.internal.model.common.DefaultConversionContext;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.common.SourceRHS;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.model.common.FormattingParameters;
|
||||
import org.mapstruct.ap.internal.model.source.Method;
|
||||
import org.mapstruct.ap.internal.model.source.SelectionParameters;
|
||||
import org.mapstruct.ap.internal.model.source.builtin.BuiltInMappingMethods;
|
||||
|
@ -53,8 +53,10 @@
|
||||
${ext.targetBeanName}<#if ext.targetReadAccessorName??>.${ext.targetReadAccessorName}</#if><#t>
|
||||
<#elseif param.mappingContext>
|
||||
${param.variableName}<#t>
|
||||
<#elseif param.sourceRHS??>
|
||||
<@_assignment assignmentToUse=param.sourceRHS/><#t>
|
||||
<#elseif assignment??>
|
||||
<@_assignment/><#t>
|
||||
<@_assignment assignmentToUse=assignment/><#t>
|
||||
<#else>
|
||||
${param.variableName}<#t>
|
||||
</#if>
|
||||
@ -67,8 +69,8 @@
|
||||
macro: assignment
|
||||
purpose: note: takes its targetyType from the singleSourceParameterType
|
||||
-->
|
||||
<#macro _assignment>
|
||||
<@includeModel object=assignment
|
||||
<#macro _assignment assignmentToUse>
|
||||
<@includeModel object=assignmentToUse
|
||||
targetBeanName=ext.targetBeanName
|
||||
existingInstanceMapping=ext.existingInstanceMapping
|
||||
targetReadAccessorName=ext.targetReadAccessorName
|
||||
|
@ -1,4 +1,4 @@
|
||||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SourceRHS" -->
|
||||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.SourceRHS" -->
|
||||
<#--
|
||||
|
||||
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1131;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import org.mapstruct.ObjectFactory;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper
|
||||
public abstract class Issue1131Mapper {
|
||||
public static final Issue1131Mapper INSTANCE = Mappers.getMapper( Issue1131Mapper.class );
|
||||
|
||||
public static final List<String> CALLED_METHODS = new ArrayList<String>();
|
||||
|
||||
public abstract void merge(Source source, @MappingTarget Target target);
|
||||
|
||||
public abstract void mergeNested(List<Source.Nested> source, @MappingTarget List<Target.Nested> target);
|
||||
|
||||
@ObjectFactory
|
||||
protected Target.Nested create(Source.Nested source) {
|
||||
CALLED_METHODS.add( "create(Source.Nested)" );
|
||||
return new Target.Nested( "from object factory" );
|
||||
}
|
||||
|
||||
@ObjectFactory
|
||||
protected Target.Nested createWithSource(Source source) {
|
||||
throw new IllegalArgumentException( "Should not use create with source" );
|
||||
}
|
||||
|
||||
@ObjectFactory
|
||||
protected List<Target.Nested> createWithSourceList(List<Source.Nested> source) {
|
||||
CALLED_METHODS.add( "create(List<Source.Nested>)" );
|
||||
List<Target.Nested> result = new ArrayList<Target.Nested>();
|
||||
result.add( new Target.Nested( "from createWithSourceList" ) );
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1131;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.Context;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
import org.mapstruct.ObjectFactory;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper
|
||||
public abstract class Issue1131MapperWithContext {
|
||||
public static final Issue1131MapperWithContext INSTANCE = Mappers.getMapper( Issue1131MapperWithContext.class );
|
||||
|
||||
public static class MappingContext {
|
||||
private final List<String> calledMethods = new ArrayList<String>();
|
||||
|
||||
public Target.Nested create(Source.Nested source) {
|
||||
calledMethods.add( "create(Source.Nested)" );
|
||||
return new Target.Nested( "from within @Context" );
|
||||
}
|
||||
|
||||
public List<Target.Nested> create(List<Source.Nested> source) {
|
||||
calledMethods.add( "create(List<Source.Nested>)" );
|
||||
if ( source == null ) {
|
||||
return new ArrayList<Target.Nested>();
|
||||
}
|
||||
else {
|
||||
return new ArrayList<Target.Nested>( source.size() );
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getCalledMethods() {
|
||||
return calledMethods;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void merge(Source source, @MappingTarget Target target, @Context MappingContext context);
|
||||
|
||||
public abstract void merge(List<Source.Nested> source, @MappingTarget List<Target.Nested> target,
|
||||
@Context MappingContext context);
|
||||
|
||||
@ObjectFactory
|
||||
protected Target.Nested create(Source.Nested source, @Context MappingContext context) {
|
||||
return context.create( source );
|
||||
}
|
||||
|
||||
@ObjectFactory
|
||||
protected Target.Nested createWithSource(Source source, @Context MappingContext context) {
|
||||
throw new IllegalArgumentException( "Should not use create with source" );
|
||||
}
|
||||
|
||||
@ObjectFactory
|
||||
protected List<Target.Nested> createWithSourceList(List<Source.Nested> source, @Context MappingContext context) {
|
||||
return context.create( source );
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1131;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@IssueKey("1131")
|
||||
@WithClasses({
|
||||
Issue1131Mapper.class,
|
||||
Issue1131MapperWithContext.class,
|
||||
Source.class,
|
||||
Target.class
|
||||
})
|
||||
public class Issue1131Test {
|
||||
|
||||
@Test
|
||||
public void shouldUseCreateWithSourceNested() {
|
||||
|
||||
Source source = new Source();
|
||||
source.setNested( new Source.Nested() );
|
||||
source.getNested().setProperty( "something" );
|
||||
source.setMoreNested( new ArrayList<Source.Nested>() );
|
||||
|
||||
Target target = new Target();
|
||||
|
||||
Issue1131Mapper.INSTANCE.merge( source, target );
|
||||
|
||||
assertThat( target.getNested() ).isNotNull();
|
||||
assertThat( target.getNested().getProperty() ).isEqualTo( "something" );
|
||||
assertThat( target.getNested().getInternal() ).isEqualTo( "from object factory" );
|
||||
assertThat( Issue1131Mapper.CALLED_METHODS ).containsExactly(
|
||||
"create(Source.Nested)",
|
||||
"create(List<Source.Nested>)"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUseContextObjectFactory() {
|
||||
|
||||
Source source = new Source();
|
||||
source.setNested( new Source.Nested() );
|
||||
source.getNested().setProperty( "something" );
|
||||
source.setMoreNested( new ArrayList<Source.Nested>() );
|
||||
|
||||
Target target = new Target();
|
||||
|
||||
Issue1131MapperWithContext.MappingContext context = new Issue1131MapperWithContext.MappingContext();
|
||||
Issue1131MapperWithContext.INSTANCE.merge( source, target, context );
|
||||
|
||||
assertThat( target.getNested() ).isNotNull();
|
||||
assertThat( target.getNested().getProperty() ).isEqualTo( "something" );
|
||||
assertThat( target.getNested().getInternal() ).isEqualTo( "from within @Context" );
|
||||
assertThat( context.getCalledMethods() ).containsExactly(
|
||||
"create(Source.Nested)",
|
||||
"create(List<Source.Nested>)"
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1131;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class Source {
|
||||
|
||||
public static class Nested {
|
||||
private String property;
|
||||
|
||||
public String getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(String property) {
|
||||
this.property = property;
|
||||
}
|
||||
}
|
||||
|
||||
private Nested nested;
|
||||
private List<Nested> moreNested;
|
||||
|
||||
public Nested getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
public void setNested(Nested nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
|
||||
public List<Nested> getMoreNested() {
|
||||
return moreNested;
|
||||
}
|
||||
|
||||
public void setMoreNested(List<Nested> moreNested) {
|
||||
this.moreNested = moreNested;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1131;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class Target {
|
||||
|
||||
public static class Nested {
|
||||
private final String internal;
|
||||
private String property;
|
||||
|
||||
public Nested(String internal) {
|
||||
this.internal = internal;
|
||||
}
|
||||
|
||||
public String getInternal() {
|
||||
return internal;
|
||||
}
|
||||
|
||||
public String getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void setProperty(String property) {
|
||||
this.property = property;
|
||||
}
|
||||
}
|
||||
|
||||
private Nested nested;
|
||||
private List<Nested> moreNested;
|
||||
|
||||
public Nested getNested() {
|
||||
return nested;
|
||||
}
|
||||
|
||||
public void setNested(Nested nested) {
|
||||
this.nested = nested;
|
||||
}
|
||||
|
||||
public List<Nested> getMoreNested() {
|
||||
return moreNested;
|
||||
}
|
||||
|
||||
public void setMoreNested(List<Nested> moreNested) {
|
||||
this.moreNested = moreNested;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user