Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package org.mapstruct.ap.internal.model;

import java.util.function.Supplier;
import javax.lang.model.element.AnnotationMirror;

import org.mapstruct.ap.internal.model.common.Assignment;
Expand Down Expand Up @@ -74,32 +75,41 @@ private boolean isDisableSubMappingMethodsGeneration() {
*/
Assignment createForgedAssignment(SourceRHS sourceRHS, BuilderType builderType, ForgedMethod forgedMethod) {

if ( ctx.getForgedMethodsUnderCreation().containsKey( forgedMethod ) ) {
return createAssignment( sourceRHS, ctx.getForgedMethodsUnderCreation().get( forgedMethod ) );
}
else {
ctx.getForgedMethodsUnderCreation().put( forgedMethod, forgedMethod );
}

MappingMethod forgedMappingMethod;
Supplier<MappingMethod> forgedMappingMethodCreator;
if ( MappingMethodUtils.isEnumMapping( forgedMethod ) ) {
forgedMappingMethod = new ValueMappingMethod.Builder()
forgedMappingMethodCreator = () -> new ValueMappingMethod.Builder()
.method( forgedMethod )
.valueMappings( forgedMethod.getOptions().getValueMappings() )
.enumMapping( forgedMethod.getOptions().getEnumMappingOptions() )
.mappingContext( ctx )
.build();
}
else {
forgedMappingMethod = new BeanMappingMethod.Builder()
forgedMappingMethodCreator = () -> new BeanMappingMethod.Builder()
.forgedMethod( forgedMethod )
.returnTypeBuilder( builderType )
.mappingContext( ctx )
.build();
}

return getOrCreateForgedAssignment( sourceRHS, forgedMethod, forgedMappingMethodCreator );
}

Assignment getOrCreateForgedAssignment(SourceRHS sourceRHS, ForgedMethod forgedMethod,
Comment thread
Hypnagokali marked this conversation as resolved.
Supplier<MappingMethod> mappingMethodCreator) {

if ( ctx.getForgedMethodsUnderCreation().containsKey( forgedMethod ) ) {
return createAssignment( sourceRHS, ctx.getForgedMethodsUnderCreation().get( forgedMethod ) );
}
else {
ctx.getForgedMethodsUnderCreation().put( forgedMethod, forgedMethod );
}

MappingMethod forgedMappingMethod = mappingMethodCreator.get();

Assignment forgedAssignment = createForgedAssignment( sourceRHS, forgedMethod, forgedMappingMethod );
ctx.getForgedMethodsUnderCreation().remove( forgedMethod );

return forgedAssignment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public abstract class ContainerMappingMethod extends NormalTypeMappingMethod {
afterMappingReferences );
this.elementAssignment = parameterAssignment;
this.loopVariableName = loopVariableName;
this.selectionParameters = selectionParameters;
this.selectionParameters = selectionParameters != null ? selectionParameters : SelectionParameters.empty();

this.index1Name = Strings.getSafeVariableName( "i", existingVariables );
this.index2Name = Strings.getSafeVariableName( "j", existingVariables );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import javax.lang.model.element.AnnotationMirror;

import org.mapstruct.ap.internal.gem.BuilderGem;
Expand Down Expand Up @@ -746,15 +747,15 @@ private Assignment forgeWithElementMapping(Type sourceType, Type targetType, Sou
targetType = targetType.withoutBounds();
ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, "[]" );

ContainerMappingMethod iterableMappingMethod = builder
Supplier<MappingMethod> mappingMethodCreator = () -> builder
.mappingContext( ctx )
.method( methodRef )
.selectionParameters( selectionParameters )
.callingContextTargetPropertyName( targetPropertyName )
.positionHint( positionHint )
.build();

return createForgedAssignment( source, methodRef, iterableMappingMethod );
return getOrCreateForgedAssignment( source, methodRef, mappingMethodCreator );
}

private ForgedMethod prepareForgedMethod(Type sourceType, Type targetType, SourceRHS source, String suffix) {
Expand All @@ -772,12 +773,12 @@ private Assignment forgeMapMapping(Type sourceType, Type targetType, SourceRHS s
ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, "{}" );

MapMappingMethod.Builder builder = new MapMappingMethod.Builder();
MapMappingMethod mapMappingMethod = builder
Supplier<MappingMethod> mapMappingMethodCreator = () -> builder
.mappingContext( ctx )
.method( methodRef )
.build();

return createForgedAssignment( source, methodRef, mapMappingMethod );
return getOrCreateForgedAssignment( source, methodRef, mapMappingMethodCreator );
}

private Assignment forgeMapping(SourceRHS sourceRHS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static BeanMappingOptions forInheritance(BeanMappingOptions beanMapping,
public static BeanMappingOptions forForgedMethods(BeanMappingOptions beanMapping) {
BeanMappingOptions options = new BeanMappingOptions(
beanMapping.selectionParameters != null ?
SelectionParameters.withoutResultType( beanMapping.selectionParameters ) : null,
SelectionParameters.withoutResultType( beanMapping.selectionParameters ) : SelectionParameters.empty(),
Collections.emptyList(),
beanMapping.beanMapping,
beanMapping
Expand All @@ -67,7 +67,7 @@ public static BeanMappingOptions forForgedMethods(BeanMappingOptions beanMapping
}

public static BeanMappingOptions empty(DelegatingOptions delegatingOptions) {
return new BeanMappingOptions( null, Collections.emptyList(), null, delegatingOptions );
return new BeanMappingOptions( SelectionParameters.empty(), Collections.emptyList(), null, delegatingOptions );
}

public static BeanMappingOptions getInstanceOn(BeanMappingGem beanMapping, MapperOptions mapperOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.TypeUtils;

import org.mapstruct.ap.internal.model.common.FormattingParameters;
import org.mapstruct.ap.internal.gem.IterableMappingGem;
import org.mapstruct.ap.internal.gem.NullValueMappingStrategyGem;
import org.mapstruct.ap.internal.model.common.FormattingParameters;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.FormattingMessager;
import org.mapstruct.ap.internal.util.Message;
import org.mapstruct.ap.internal.util.TypeUtils;
import org.mapstruct.tools.gem.GemValue;

/**
Expand All @@ -34,7 +34,12 @@ public static IterableMappingOptions fromGem(IterableMappingGem iterableMapping,
FormattingMessager messager, TypeUtils typeUtils) {

if ( iterableMapping == null || !isConsistent( iterableMapping, method, messager ) ) {
IterableMappingOptions options = new IterableMappingOptions( null, null, null, mapperOptions );
IterableMappingOptions options = new IterableMappingOptions(
null,
SelectionParameters.empty(),
null,
mapperOptions
);
return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public static MapMappingOptions fromGem(MapMappingGem mapMapping, MapperOptions
if ( mapMapping == null || !isConsistent( mapMapping, method, messager ) ) {
MapMappingOptions options = new MapMappingOptions(
null,
SelectionParameters.empty(),
null,
null,
null,
SelectionParameters.empty(),
null,
mapperOptions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static MappingOptions forIgnore(String targetName) {
null,
true,
null,
null,
SelectionParameters.empty(),
Collections.emptySet(),
null,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
*/
public class SelectionParameters {

private static final SelectionParameters EMPTY = new SelectionParameters(
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
null,
null,
null
);

private final List<TypeMirror> qualifiers;
private final List<String> qualifyingNames;
private final List<TypeMirror> conditionQualifiers;
Expand Down Expand Up @@ -225,4 +235,9 @@ public static SelectionParameters forSourceRHS(SourceRHS sourceRHS) {
sourceRHS
);
}

public static SelectionParameters empty() {
return EMPTY;
}

}
36 changes: 36 additions & 0 deletions processor/src/test/java/org/mapstruct/ap/test/bugs/_3591/Bean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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._3591;

import java.util.List;

public class Bean {
private List<Bean> beans;
private String value;

public Bean() {
}

public Bean(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public List<Bean> getBeans() {
return beans;
}

public void setBeans(List<Bean> beans) {
this.beans = beans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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._3591;

import java.util.List;

public class BeanDto {

private List<BeanDto> beans;
private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public List<BeanDto> getBeans() {
return beans;
}

public void setBeans(List<BeanDto> beans) {
this.beans = beans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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._3591;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

@Mapper
public interface BeanMapper {

BeanMapper INSTANCE = Mappers.getMapper( BeanMapper.class );

@Mapping(source = "beans", target = "beans")
BeanDto map(Bean bean, @MappingTarget BeanDto beanDto);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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._3591;

import java.util.Map;
import java.util.stream.Stream;

public class ContainerBean {

private String value;
private Map<String, ContainerBean> beanMap;
private Stream<ContainerBean> beanStream;

public ContainerBean() {
}

public ContainerBean(String value) {
this.value = value;
}

public Map<String, ContainerBean> getBeanMap() {
return beanMap;
}

public void setBeanMap(Map<String, ContainerBean> beanMap) {
this.beanMap = beanMap;
}

public Stream<ContainerBean> getBeanStream() {
return beanStream;
}

public void setBeanStream(Stream<ContainerBean> beanStream) {
this.beanStream = beanStream;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -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._3591;

import java.util.Map;
import java.util.stream.Stream;

public class ContainerBeanDto {

private String value;
private Map<String, ContainerBeanDto> beanMap;
private Stream<ContainerBeanDto> beanStream;

public Map<String, ContainerBeanDto> getBeanMap() {
return beanMap;
}

public void setBeanMap(Map<String, ContainerBeanDto> beanMap) {
this.beanMap = beanMap;
}

public Stream<ContainerBeanDto> getBeanStream() {
return beanStream;
}

public void setBeanStream(Stream<ContainerBeanDto> beanStream) {
this.beanStream = beanStream;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Loading