From 4d9894ba25ba4e17c76211409f951f4cce956b3e Mon Sep 17 00:00:00 2001 From: Obolrom <65775868+Obolrom@users.noreply.github.com> Date: Mon, 2 Sep 2024 11:26:48 +0300 Subject: [PATCH] #3113 Use LinkedHashSet, LinkedHashSet new factory methods for java >= 19 --- .../model/common/ImplementationType.java | 25 +- .../ap/internal/model/common/TypeFactory.java | 26 +- .../DefaultModelElementProcessorContext.java | 3 +- .../processor/DefaultVersionInformation.java | 7 + .../internal/version/VersionInformation.java | 2 + .../ap/internal/model/IterableCreation.ftl | 12 +- .../testutil/runner/CompilationRequest.java | 4 + .../ap/testutil/runner/GeneratedSource.java | 21 +- .../test/bugs/_1453/Issue1453MapperImpl.java | 139 ++++++++++ .../bugs/_3591/ContainerBeanMapperImpl.java | 85 ++++++ .../DomainDtoWithNcvsAlwaysMapperImpl.java | 214 +++++++++++++++ .../DomainDtoWithNvmsDefaultMapperImpl.java | 245 +++++++++++++++++ .../_913/DomainDtoWithNvmsNullMapperImpl.java | 248 +++++++++++++++++ .../DomainDtoWithPresenceCheckMapperImpl.java | 214 +++++++++++++++ .../SourceTargetMapperImpl.java | 259 ++++++++++++++++++ .../updatemethods/CompanyMapper1Impl.java | 120 ++++++++ 16 files changed, 1605 insertions(+), 19 deletions(-) create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_1453/Issue1453MapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_3591/ContainerBeanMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/collection/defaultimplementation/SourceTargetMapperImpl.java create mode 100644 processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/updatemethods/CompanyMapper1Impl.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/ImplementationType.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/ImplementationType.java index 45b73c492..af8c7ecfb 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/ImplementationType.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/ImplementationType.java @@ -16,23 +16,34 @@ public class ImplementationType { private final Type type; private final boolean initialCapacityConstructor; private final boolean loadFactorAdjustment; + private final String factoryMethodName; - private ImplementationType(Type type, boolean initialCapacityConstructor, boolean loadFactorAdjustment) { + private ImplementationType( + Type type, + boolean initialCapacityConstructor, + boolean loadFactorAdjustment, + String factoryMethodName + ) { this.type = type; this.initialCapacityConstructor = initialCapacityConstructor; this.loadFactorAdjustment = loadFactorAdjustment; + this.factoryMethodName = factoryMethodName; } public static ImplementationType withDefaultConstructor(Type type) { - return new ImplementationType( type, false, false ); + return new ImplementationType( type, false, false, null ); } public static ImplementationType withInitialCapacity(Type type) { - return new ImplementationType( type, true, false ); + return new ImplementationType( type, true, false, null ); } public static ImplementationType withLoadFactorAdjustment(Type type) { - return new ImplementationType( type, true, true ); + return new ImplementationType( type, true, true, null ); + } + + public static ImplementationType withFactoryMethod(Type type, String factoryMethodName) { + return new ImplementationType( type, true, false, factoryMethodName ); } /** @@ -44,7 +55,7 @@ public class ImplementationType { * @return a new implementation type with the given {@code type} */ public ImplementationType createNew(Type type) { - return new ImplementationType( type, initialCapacityConstructor, loadFactorAdjustment ); + return new ImplementationType( type, initialCapacityConstructor, loadFactorAdjustment, factoryMethodName ); } /** @@ -71,4 +82,8 @@ public class ImplementationType { public boolean isLoadFactorAdjustment() { return loadFactorAdjustment; } + + public String getFactoryMethodName() { + return factoryMethodName; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java index f28e0c687..d65070426 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java @@ -38,12 +38,11 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.type.WildcardType; -import org.mapstruct.ap.internal.util.ElementUtils; -import org.mapstruct.ap.internal.util.TypeUtils; import org.mapstruct.ap.internal.gem.BuilderGem; import org.mapstruct.ap.internal.util.AnnotationProcessingException; import org.mapstruct.ap.internal.util.Collections; +import org.mapstruct.ap.internal.util.ElementUtils; import org.mapstruct.ap.internal.util.Extractor; import org.mapstruct.ap.internal.util.FormattingMessager; import org.mapstruct.ap.internal.util.JavaStreamConstants; @@ -51,13 +50,16 @@ import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.NativeTypes; import org.mapstruct.ap.internal.util.RoundContext; import org.mapstruct.ap.internal.util.Strings; +import org.mapstruct.ap.internal.util.TypeUtils; import org.mapstruct.ap.internal.util.accessor.Accessor; +import org.mapstruct.ap.internal.version.VersionInformation; import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor; import org.mapstruct.ap.spi.BuilderInfo; import org.mapstruct.ap.spi.MoreThanOneBuilderCreationMethodException; import org.mapstruct.ap.spi.TypeHierarchyErroneousException; import static org.mapstruct.ap.internal.model.common.ImplementationType.withDefaultConstructor; +import static org.mapstruct.ap.internal.model.common.ImplementationType.withFactoryMethod; import static org.mapstruct.ap.internal.model.common.ImplementationType.withInitialCapacity; import static org.mapstruct.ap.internal.model.common.ImplementationType.withLoadFactorAdjustment; @@ -82,6 +84,8 @@ public class TypeFactory { sb.append( ')' ); return sb.toString(); }; + private static final String LINKED_HASH_SET_FACTORY_METHOD_NAME = "newLinkedHashSet"; + private static final String LINKED_HASH_MAP_FACTORY_METHOD_NAME = "newLinkedHashMap"; private final ElementUtils elementUtils; private final TypeUtils typeUtils; @@ -100,7 +104,8 @@ public class TypeFactory { private final boolean loggingVerbose; public TypeFactory(ElementUtils elementUtils, TypeUtils typeUtils, FormattingMessager messager, - RoundContext roundContext, Map notToBeImportedTypes, boolean loggingVerbose) { + RoundContext roundContext, Map notToBeImportedTypes, boolean loggingVerbose, + VersionInformation versionInformation) { this.elementUtils = elementUtils; this.typeUtils = typeUtils; this.messager = messager; @@ -118,11 +123,22 @@ public class TypeFactory { implementationTypes.put( Collection.class.getName(), withInitialCapacity( getType( ArrayList.class ) ) ); implementationTypes.put( List.class.getName(), withInitialCapacity( getType( ArrayList.class ) ) ); - implementationTypes.put( Set.class.getName(), withLoadFactorAdjustment( getType( LinkedHashSet.class ) ) ); + boolean sourceVersionAtLeast19 = versionInformation.isSourceVersionAtLeast19(); + implementationTypes.put( + Set.class.getName(), + sourceVersionAtLeast19 ? + withFactoryMethod( getType( LinkedHashSet.class ), LINKED_HASH_SET_FACTORY_METHOD_NAME ) : + withLoadFactorAdjustment( getType( LinkedHashSet.class ) ) + ); implementationTypes.put( SortedSet.class.getName(), withDefaultConstructor( getType( TreeSet.class ) ) ); implementationTypes.put( NavigableSet.class.getName(), withDefaultConstructor( getType( TreeSet.class ) ) ); - implementationTypes.put( Map.class.getName(), withLoadFactorAdjustment( getType( LinkedHashMap.class ) ) ); + implementationTypes.put( + Map.class.getName(), + sourceVersionAtLeast19 ? + withFactoryMethod( getType( LinkedHashMap.class ), LINKED_HASH_MAP_FACTORY_METHOD_NAME ) : + withLoadFactorAdjustment( getType( LinkedHashMap.class ) ) + ); implementationTypes.put( SortedMap.class.getName(), withDefaultConstructor( getType( TreeMap.class ) ) ); implementationTypes.put( NavigableMap.class.getName(), withDefaultConstructor( getType( TreeMap.class ) ) ); implementationTypes.put( diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java index 51ea5dd78..8ac1cc067 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java @@ -62,7 +62,8 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { messager, roundContext, notToBeImported, - options.isVerbose() + options.isVerbose(), + versionInformation ); this.options = options; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java index c4baf1bf5..055bfe609 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java @@ -41,6 +41,7 @@ public class DefaultVersionInformation implements VersionInformation { private final String runtimeVendor; private final String compiler; private final boolean sourceVersionAtLeast9; + private final boolean sourceVersionAtLeast19; private final boolean eclipseJDT; private final boolean javac; @@ -53,6 +54,7 @@ public class DefaultVersionInformation implements VersionInformation { this.javac = compiler.startsWith( COMPILER_NAME_JAVAC ); // If the difference between the source version and RELEASE_6 is more that 2 than we are at least on 9 this.sourceVersionAtLeast9 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 2; + this.sourceVersionAtLeast19 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 12; } @Override @@ -80,6 +82,11 @@ public class DefaultVersionInformation implements VersionInformation { return sourceVersionAtLeast9; } + @Override + public boolean isSourceVersionAtLeast19() { + return sourceVersionAtLeast19; + } + @Override public boolean isEclipseJDTCompiler() { return eclipseJDT; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java index 94e3520ad..5e1972fca 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java @@ -21,6 +21,8 @@ public interface VersionInformation { boolean isSourceVersionAtLeast9(); + boolean isSourceVersionAtLeast19(); + boolean isEclipseJDTCompiler(); boolean isJavacCompiler(); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableCreation.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableCreation.ftl index d49397a98..d083bd113 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableCreation.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableCreation.ftl @@ -11,13 +11,15 @@ <@includeModel object=factoryMethod targetType=resultType/> <#elseif enumSet> EnumSet.noneOf( <@includeModel object=enumSetElementType raw=true/>.class ) - <#else> - new - <#if resultType.implementationType??> - <@includeModel object=resultType.implementationType/><#if ext.useSizeIfPossible?? && ext.useSizeIfPossible && canUseSize>( <@sizeForCreation /> )<#else>() + <#elseif resultType.implementation??> + <#if resultType.implementation.factoryMethodName?? && ext.useSizeIfPossible?? && ext.useSizeIfPossible && canUseSize> + <@includeModel object=resultType.implementationType raw=true />.${resultType.implementation.factoryMethodName}( <@sizeForCreation /> ) <#else> - <@includeModel object=resultType/>() + new <@includeModel object=resultType.implementationType/><#if ext.useSizeIfPossible?? && ext.useSizeIfPossible && canUseSize>( <@sizeForCreation /> )<#else>() + <#else> + new <@includeModel object=resultType/>() + <#macro sizeForCreation> <@compress single_line=true> diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilationRequest.java b/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilationRequest.java index 60e9a3007..9f1b78caa 100644 --- a/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilationRequest.java +++ b/processor/src/test/java/org/mapstruct/ap/testutil/runner/CompilationRequest.java @@ -76,4 +76,8 @@ public class CompilationRequest { public Collection getTestDependencies() { return testDependencies; } + + public Compiler getCompiler() { + return compiler; + } } diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java b/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java index c0993ea09..18a53b497 100644 --- a/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java +++ b/processor/src/test/java/org/mapstruct/ap/testutil/runner/GeneratedSource.java @@ -44,12 +44,15 @@ public class GeneratedSource implements BeforeTestExecutionCallback, AfterTestEx */ private ThreadLocal sourceOutputDir = new ThreadLocal<>(); + private Compiler compiler; + private List> fixturesFor = new ArrayList<>(); @Override public void beforeTestExecution(ExtensionContext context) throws Exception { CompilationRequest compilationRequest = context.getStore( NAMESPACE ) .get( context.getUniqueId() + "-compilationRequest", CompilationRequest.class ); + this.compiler = compilationRequest.getCompiler(); setSourceOutputDir( context.getStore( NAMESPACE ) .get( compilationRequest, CompilationCache.class ) .getLastSourceOutputDir() ); @@ -118,13 +121,13 @@ public class GeneratedSource implements BeforeTestExecutionCallback, AfterTestEx private void handleFixtureComparison() throws UnsupportedEncodingException { for ( Class fixture : fixturesFor ) { - String expectedFixture = FIXTURES_ROOT + getMapperName( fixture ); - URL expectedFile = getClass().getClassLoader().getResource( expectedFixture ); + String fixtureName = getMapperName( fixture ); + URL expectedFile = getExpectedResource( fixtureName ); if ( expectedFile == null ) { fail( String.format( "No reference file could be found for Mapper %s. You should create a file %s", fixture.getName(), - expectedFixture + FIXTURES_ROOT + fixtureName ) ); } else { @@ -135,4 +138,16 @@ public class GeneratedSource implements BeforeTestExecutionCallback, AfterTestEx } } + + private URL getExpectedResource( String fixtureName ) { + ClassLoader classLoader = getClass().getClassLoader(); + for ( int version = Runtime.version().feature(); version >= 11 && compiler != Compiler.ECLIPSE; version-- ) { + URL resource = classLoader.getResource( FIXTURES_ROOT + "/" + version + "/" + fixtureName ); + if ( resource != null ) { + return resource; + } + } + + return classLoader.getResource( FIXTURES_ROOT + fixtureName ); + } } diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_1453/Issue1453MapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_1453/Issue1453MapperImpl.java new file mode 100644 index 000000000..78c3bea0d --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_1453/Issue1453MapperImpl.java @@ -0,0 +1,139 @@ +/* + * 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._1453; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-18T14:48:39+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class Issue1453MapperImpl implements Issue1453Mapper { + + @Override + public AuctionDto map(Auction auction) { + if ( auction == null ) { + return null; + } + + AuctionDto auctionDto = new AuctionDto(); + + auctionDto.setPayments( paymentListToPaymentDtoList( auction.getPayments() ) ); + auctionDto.setOtherPayments( paymentListToPaymentDtoList( auction.getOtherPayments() ) ); + auctionDto.setMapPayments( paymentPaymentMapToPaymentDtoPaymentDtoMap( auction.getMapPayments() ) ); + auctionDto.setMapSuperPayments( paymentPaymentMapToPaymentDtoPaymentDtoMap( auction.getMapSuperPayments() ) ); + + return auctionDto; + } + + @Override + public List mapExtend(List auctions) { + if ( auctions == null ) { + return null; + } + + List list = new ArrayList( auctions.size() ); + for ( Auction auction : auctions ) { + list.add( map( auction ) ); + } + + return list; + } + + @Override + public List mapSuper(List auctions) { + if ( auctions == null ) { + return null; + } + + List list = new ArrayList( auctions.size() ); + for ( Auction auction : auctions ) { + list.add( map( auction ) ); + } + + return list; + } + + @Override + public Map mapExtend(Map auctions) { + if ( auctions == null ) { + return null; + } + + Map map = LinkedHashMap.newLinkedHashMap( auctions.size() ); + + for ( java.util.Map.Entry entry : auctions.entrySet() ) { + AuctionDto key = map( entry.getKey() ); + AuctionDto value = map( entry.getValue() ); + map.put( key, value ); + } + + return map; + } + + @Override + public Map mapSuper(Map auctions) { + if ( auctions == null ) { + return null; + } + + Map map = LinkedHashMap.newLinkedHashMap( auctions.size() ); + + for ( java.util.Map.Entry entry : auctions.entrySet() ) { + AuctionDto key = map( entry.getKey() ); + AuctionDto value = map( entry.getValue() ); + map.put( key, value ); + } + + return map; + } + + protected PaymentDto paymentToPaymentDto(Payment payment) { + if ( payment == null ) { + return null; + } + + PaymentDto paymentDto = new PaymentDto(); + + paymentDto.setPrice( payment.getPrice() ); + + return paymentDto; + } + + protected List paymentListToPaymentDtoList(List list) { + if ( list == null ) { + return null; + } + + List list1 = new ArrayList( list.size() ); + for ( Payment payment : list ) { + list1.add( paymentToPaymentDto( payment ) ); + } + + return list1; + } + + protected Map paymentPaymentMapToPaymentDtoPaymentDtoMap(Map map) { + if ( map == null ) { + return null; + } + + Map map1 = LinkedHashMap.newLinkedHashMap( map.size() ); + + for ( java.util.Map.Entry entry : map.entrySet() ) { + PaymentDto key = paymentToPaymentDto( entry.getKey() ); + PaymentDto value = paymentToPaymentDto( entry.getValue() ); + map1.put( key, value ); + } + + return map1; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_3591/ContainerBeanMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_3591/ContainerBeanMapperImpl.java new file mode 100644 index 000000000..af3d293f7 --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_3591/ContainerBeanMapperImpl.java @@ -0,0 +1,85 @@ +/* + * 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.LinkedHashMap; +import java.util.Map; +import java.util.stream.Stream; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-05-25T14:23:23+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class ContainerBeanMapperImpl implements ContainerBeanMapper { + + @Override + public ContainerBeanDto mapWithMapMapping(ContainerBean containerBean, ContainerBeanDto containerBeanDto) { + if ( containerBean == null ) { + return containerBeanDto; + } + + if ( containerBeanDto.getBeanMap() != null ) { + Map map = stringContainerBeanMapToStringContainerBeanDtoMap( containerBean.getBeanMap() ); + if ( map != null ) { + containerBeanDto.getBeanMap().clear(); + containerBeanDto.getBeanMap().putAll( map ); + } + else { + containerBeanDto.setBeanMap( null ); + } + } + else { + Map map = stringContainerBeanMapToStringContainerBeanDtoMap( containerBean.getBeanMap() ); + if ( map != null ) { + containerBeanDto.setBeanMap( map ); + } + } + containerBeanDto.setBeanStream( containerBeanStreamToContainerBeanDtoStream( containerBean.getBeanStream() ) ); + containerBeanDto.setValue( containerBean.getValue() ); + + return containerBeanDto; + } + + protected Stream containerBeanStreamToContainerBeanDtoStream(Stream stream) { + if ( stream == null ) { + return null; + } + + return stream.map( containerBean -> containerBeanToContainerBeanDto( containerBean ) ); + } + + protected ContainerBeanDto containerBeanToContainerBeanDto(ContainerBean containerBean) { + if ( containerBean == null ) { + return null; + } + + ContainerBeanDto containerBeanDto = new ContainerBeanDto(); + + containerBeanDto.setBeanMap( stringContainerBeanMapToStringContainerBeanDtoMap( containerBean.getBeanMap() ) ); + containerBeanDto.setBeanStream( containerBeanStreamToContainerBeanDtoStream( containerBean.getBeanStream() ) ); + containerBeanDto.setValue( containerBean.getValue() ); + + return containerBeanDto; + } + + protected Map stringContainerBeanMapToStringContainerBeanDtoMap(Map map) { + if ( map == null ) { + return null; + } + + Map map1 = LinkedHashMap.newLinkedHashMap( map.size() ); + + for ( java.util.Map.Entry entry : map.entrySet() ) { + String key = entry.getKey(); + ContainerBeanDto value = containerBeanToContainerBeanDto( entry.getValue() ); + map1.put( key, value ); + } + + return map1; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java new file mode 100644 index 000000000..d5cfd2472 --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNcvsAlwaysMapperImpl.java @@ -0,0 +1,214 @@ +/* + * 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._913; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-19T11:20:01+0300", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Amazon.com Inc.)" +) +public class DomainDtoWithNcvsAlwaysMapperImpl implements DomainDtoWithNcvsAlwaysMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(DtoWithPresenceCheck source) { + if ( source == null ) { + return null; + } + + Domain domain = createNullDomain(); + + if ( source.hasStrings() ) { + List list = source.getStrings(); + domain.setStrings( new LinkedHashSet( list ) ); + } + if ( source.hasStrings() ) { + domain.setLongs( stringListToLongSet( source.getStrings() ) ); + } + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + domain.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + if ( source.hasStringsInitialized() ) { + domain.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + domain.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + + return domain; + } + + @Override + public void update(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + target.getStrings().clear(); + target.getStrings().addAll( source.getStrings() ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + target.getLongs().clear(); + target.getLongs().addAll( stringListToLongSet( source.getStrings() ) ); + } + } + else { + if ( source.hasStrings() ) { + target.setLongs( stringListToLongSet( source.getStrings() ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( source.getStringsInitialized() ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + else { + if ( source.hasStringsInitialized() ) { + target.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( source.getStringsWithDefault() ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + } + + @Override + public Domain updateWithReturn(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return target; + } + + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + target.getStrings().clear(); + target.getStrings().addAll( source.getStrings() ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + target.getLongs().clear(); + target.getLongs().addAll( stringListToLongSet( source.getStrings() ) ); + } + } + else { + if ( source.hasStrings() ) { + target.setLongs( stringListToLongSet( source.getStrings() ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( source.getStringsInitialized() ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + else { + if ( source.hasStringsInitialized() ) { + target.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( source.getStringsWithDefault() ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = LinkedHashSet.newLinkedHashSet( list.size() ); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java new file mode 100644 index 000000000..76de94378 --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsDefaultMapperImpl.java @@ -0,0 +1,245 @@ +/* + * 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._913; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-18T14:48:39+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class DomainDtoWithNvmsDefaultMapperImpl implements DomainDtoWithNvmsDefaultMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(Dto source) { + + Domain domain = new Domain(); + + if ( source != null ) { + List list = source.getStrings(); + if ( list != null ) { + domain.setStrings( new LinkedHashSet( list ) ); + } + domain.setLongs( stringListToLongSet( source.getStrings() ) ); + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + domain.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + domain.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + domain.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + } + + return domain; + } + + @Override + public void update(Dto source, Domain target) { + + if ( source != null ) { + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( new LinkedHashSet() ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( new LinkedHashSet() ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStringsInitialized() != null ) { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list1 ); + } + else { + target.setStringsInitialized( new LinkedHashSet() ); + } + } + else { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set1 ); + } + else { + target.setLongsInitialized( new LinkedHashSet() ); + } + } + else { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.setLongsInitialized( set1 ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list2 ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + } + } + + @Override + public Domain updateWithReturn(Dto source, Domain target) { + + if ( source != null ) { + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( new LinkedHashSet() ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( new LinkedHashSet() ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStringsInitialized() != null ) { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list1 ); + } + else { + target.setStringsInitialized( new LinkedHashSet() ); + } + } + else { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set1 ); + } + else { + target.setLongsInitialized( new LinkedHashSet() ); + } + } + else { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.setLongsInitialized( set1 ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list2 ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return new LinkedHashSet(); + } + + Set set = LinkedHashSet.newLinkedHashSet( list.size() ); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java new file mode 100644 index 000000000..c61c2c68e --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithNvmsNullMapperImpl.java @@ -0,0 +1,248 @@ +/* + * 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._913; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-18T14:48:39+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class DomainDtoWithNvmsNullMapperImpl implements DomainDtoWithNvmsNullMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(Dto source) { + if ( source == null ) { + return null; + } + + Domain domain = createNullDomain(); + + List list = source.getStrings(); + if ( list != null ) { + domain.setStrings( new LinkedHashSet( list ) ); + } + domain.setLongs( stringListToLongSet( source.getStrings() ) ); + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + domain.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + domain.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + domain.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + + return domain; + } + + @Override + public void update(Dto source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStringsInitialized() != null ) { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list1 ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set1 ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.setLongsInitialized( set1 ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list2 ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + } + + @Override + public Domain updateWithReturn(Dto source, Domain target) { + if ( source == null ) { + return target; + } + + if ( target.getStrings() != null ) { + List list = source.getStrings(); + if ( list != null ) { + target.getStrings().clear(); + target.getStrings().addAll( list ); + } + else { + target.setStrings( null ); + } + } + else { + List list = source.getStrings(); + if ( list != null ) { + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.getLongs().clear(); + target.getLongs().addAll( set ); + } + else { + target.setLongs( null ); + } + } + else { + Set set = stringListToLongSet( source.getStrings() ); + if ( set != null ) { + target.setLongs( set ); + } + } + if ( target.getStringsInitialized() != null ) { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( list1 ); + } + else { + target.setStringsInitialized( null ); + } + } + else { + List list1 = source.getStringsInitialized(); + if ( list1 != null ) { + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( set1 ); + } + else { + target.setLongsInitialized( null ); + } + } + else { + Set set1 = stringListToLongSet( source.getStringsInitialized() ); + if ( set1 != null ) { + target.setLongsInitialized( set1 ); + } + } + if ( target.getStringsWithDefault() != null ) { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( list2 ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + List list2 = source.getStringsWithDefault(); + if ( list2 != null ) { + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = LinkedHashSet.newLinkedHashSet( list.size() ); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java new file mode 100644 index 000000000..6d01a91ec --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/bugs/_913/DomainDtoWithPresenceCheckMapperImpl.java @@ -0,0 +1,214 @@ +/* + * 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._913; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-19T11:03:59+0300", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Amazon.com Inc.)" +) +public class DomainDtoWithPresenceCheckMapperImpl implements DomainDtoWithPresenceCheckMapper { + + private final Helper helper = new Helper(); + + @Override + public Domain create(DtoWithPresenceCheck source) { + if ( source == null ) { + return null; + } + + Domain domain = createNullDomain(); + + if ( source.hasStrings() ) { + List list = source.getStrings(); + domain.setStrings( new LinkedHashSet( list ) ); + } + if ( source.hasStrings() ) { + domain.setLongs( stringListToLongSet( source.getStrings() ) ); + } + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + domain.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + if ( source.hasStringsInitialized() ) { + domain.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + domain.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + domain.setStringsWithDefault( helper.toList( "3" ) ); + } + + return domain; + } + + @Override + public void update(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return; + } + + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + target.getStrings().clear(); + target.getStrings().addAll( source.getStrings() ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + target.getLongs().clear(); + target.getLongs().addAll( stringListToLongSet( source.getStrings() ) ); + } + } + else { + if ( source.hasStrings() ) { + target.setLongs( stringListToLongSet( source.getStrings() ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( source.getStringsInitialized() ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + else { + if ( source.hasStringsInitialized() ) { + target.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( source.getStringsWithDefault() ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + } + + @Override + public Domain updateWithReturn(DtoWithPresenceCheck source, Domain target) { + if ( source == null ) { + return target; + } + + if ( target.getStrings() != null ) { + if ( source.hasStrings() ) { + target.getStrings().clear(); + target.getStrings().addAll( source.getStrings() ); + } + } + else { + if ( source.hasStrings() ) { + List list = source.getStrings(); + target.setStrings( new LinkedHashSet( list ) ); + } + } + if ( target.getLongs() != null ) { + if ( source.hasStrings() ) { + target.getLongs().clear(); + target.getLongs().addAll( stringListToLongSet( source.getStrings() ) ); + } + } + else { + if ( source.hasStrings() ) { + target.setLongs( stringListToLongSet( source.getStrings() ) ); + } + } + if ( target.getStringsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getStringsInitialized().clear(); + target.getStringsInitialized().addAll( source.getStringsInitialized() ); + } + } + else { + if ( source.hasStringsInitialized() ) { + List list1 = source.getStringsInitialized(); + target.setStringsInitialized( new LinkedHashSet( list1 ) ); + } + } + if ( target.getLongsInitialized() != null ) { + if ( source.hasStringsInitialized() ) { + target.getLongsInitialized().clear(); + target.getLongsInitialized().addAll( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + else { + if ( source.hasStringsInitialized() ) { + target.setLongsInitialized( stringListToLongSet( source.getStringsInitialized() ) ); + } + } + if ( target.getStringsWithDefault() != null ) { + if ( source.hasStringsWithDefault() ) { + target.getStringsWithDefault().clear(); + target.getStringsWithDefault().addAll( source.getStringsWithDefault() ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + else { + if ( source.hasStringsWithDefault() ) { + List list2 = source.getStringsWithDefault(); + target.setStringsWithDefault( new ArrayList( list2 ) ); + } + else { + target.setStringsWithDefault( helper.toList( "3" ) ); + } + } + + return target; + } + + protected Set stringListToLongSet(List list) { + if ( list == null ) { + return null; + } + + Set set = LinkedHashSet.newLinkedHashSet( list.size() ); + for ( String string : list ) { + set.add( Long.parseLong( string ) ); + } + + return set; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/collection/defaultimplementation/SourceTargetMapperImpl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/collection/defaultimplementation/SourceTargetMapperImpl.java new file mode 100644 index 000000000..3f0bee535 --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/collection/defaultimplementation/SourceTargetMapperImpl.java @@ -0,0 +1,259 @@ +/* + * 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.collection.defaultimplementation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.NavigableSet; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-18T14:48:39+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class SourceTargetMapperImpl implements SourceTargetMapper { + + @Override + public Target sourceToTarget(Source source) { + if ( source == null ) { + return null; + } + + Target target = new Target(); + + if ( target.getFooListNoSetter() != null ) { + List list = sourceFoosToTargetFoos( source.getFooList() ); + if ( list != null ) { + target.getFooListNoSetter().addAll( list ); + } + } + + return target; + } + + @Override + public TargetFoo sourceFooToTargetFoo(SourceFoo sourceFoo) { + if ( sourceFoo == null ) { + return null; + } + + TargetFoo targetFoo = new TargetFoo(); + + targetFoo.setName( sourceFoo.getName() ); + + return targetFoo; + } + + @Override + public List sourceFoosToTargetFoos(List foos) { + if ( foos == null ) { + return null; + } + + List list = new ArrayList( foos.size() ); + for ( SourceFoo sourceFoo : foos ) { + list.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return list; + } + + @Override + public Set sourceFoosToTargetFoos(Set foos) { + if ( foos == null ) { + return null; + } + + Set set = LinkedHashSet.newLinkedHashSet( foos.size() ); + for ( SourceFoo sourceFoo : foos ) { + set.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return set; + } + + @Override + public Collection sourceFoosToTargetFoos(Collection foos) { + if ( foos == null ) { + return null; + } + + Collection collection = new ArrayList( foos.size() ); + for ( SourceFoo sourceFoo : foos ) { + collection.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return collection; + } + + @Override + public Iterable sourceFoosToTargetFoos(Iterable foos) { + if ( foos == null ) { + return null; + } + + ArrayList iterable = new ArrayList(); + for ( SourceFoo sourceFoo : foos ) { + iterable.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return iterable; + } + + @Override + public void sourceFoosToTargetFoosUsingTargetParameter(List targetFoos, Iterable sourceFoos) { + if ( sourceFoos == null ) { + return; + } + + targetFoos.clear(); + for ( SourceFoo sourceFoo : sourceFoos ) { + targetFoos.add( sourceFooToTargetFoo( sourceFoo ) ); + } + } + + @Override + public Iterable sourceFoosToTargetFoosUsingTargetParameterAndReturn(Iterable sourceFoos, List targetFoos) { + if ( sourceFoos == null ) { + return targetFoos; + } + + targetFoos.clear(); + for ( SourceFoo sourceFoo : sourceFoos ) { + targetFoos.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return targetFoos; + } + + @Override + public SortedSet sourceFoosToTargetFooSortedSet(Collection foos) { + if ( foos == null ) { + return null; + } + + SortedSet sortedSet = new TreeSet(); + for ( SourceFoo sourceFoo : foos ) { + sortedSet.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return sortedSet; + } + + @Override + public NavigableSet sourceFoosToTargetFooNavigableSet(Collection foos) { + if ( foos == null ) { + return null; + } + + NavigableSet navigableSet = new TreeSet(); + for ( SourceFoo sourceFoo : foos ) { + navigableSet.add( sourceFooToTargetFoo( sourceFoo ) ); + } + + return navigableSet; + } + + @Override + public Map sourceFooMapToTargetFooMap(Map foos) { + if ( foos == null ) { + return null; + } + + Map map = LinkedHashMap.newLinkedHashMap( foos.size() ); + + for ( java.util.Map.Entry entry : foos.entrySet() ) { + String key = String.valueOf( entry.getKey() ); + TargetFoo value = sourceFooToTargetFoo( entry.getValue() ); + map.put( key, value ); + } + + return map; + } + + @Override + public SortedMap sourceFooMapToTargetFooSortedMap(Map foos) { + if ( foos == null ) { + return null; + } + + SortedMap sortedMap = new TreeMap(); + + for ( java.util.Map.Entry entry : foos.entrySet() ) { + String key = String.valueOf( entry.getKey() ); + TargetFoo value = sourceFooToTargetFoo( entry.getValue() ); + sortedMap.put( key, value ); + } + + return sortedMap; + } + + @Override + public NavigableMap sourceFooMapToTargetFooNavigableMap(Map foos) { + if ( foos == null ) { + return null; + } + + NavigableMap navigableMap = new TreeMap(); + + for ( java.util.Map.Entry entry : foos.entrySet() ) { + String key = String.valueOf( entry.getKey() ); + TargetFoo value = sourceFooToTargetFoo( entry.getValue() ); + navigableMap.put( key, value ); + } + + return navigableMap; + } + + @Override + public ConcurrentMap sourceFooMapToTargetFooConcurrentMap(Map foos) { + if ( foos == null ) { + return null; + } + + ConcurrentMap concurrentMap = new ConcurrentHashMap( Math.max( (int) ( foos.size() / .75f ) + 1, 16 ) ); + + for ( java.util.Map.Entry entry : foos.entrySet() ) { + String key = String.valueOf( entry.getKey() ); + TargetFoo value = sourceFooToTargetFoo( entry.getValue() ); + concurrentMap.put( key, value ); + } + + return concurrentMap; + } + + @Override + public ConcurrentNavigableMap sourceFooMapToTargetFooConcurrentNavigableMap(Map foos) { + if ( foos == null ) { + return null; + } + + ConcurrentNavigableMap concurrentNavigableMap = new ConcurrentSkipListMap(); + + for ( java.util.Map.Entry entry : foos.entrySet() ) { + String key = String.valueOf( entry.getKey() ); + TargetFoo value = sourceFooToTargetFoo( entry.getValue() ); + concurrentNavigableMap.put( key, value ); + } + + return concurrentNavigableMap; + } +} diff --git a/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/updatemethods/CompanyMapper1Impl.java b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/updatemethods/CompanyMapper1Impl.java new file mode 100644 index 000000000..f34495cb2 --- /dev/null +++ b/processor/src/test/resources/fixtures/21/org/mapstruct/ap/test/updatemethods/CompanyMapper1Impl.java @@ -0,0 +1,120 @@ +/* + * 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.updatemethods; + +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.processing.Generated; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-06-18T14:48:39+0200", + comments = "version: , compiler: javac, environment: Java 21.0.2 (Eclipse Adoptium)" +) +public class CompanyMapper1Impl implements CompanyMapper1 { + + private final DepartmentEntityFactory departmentEntityFactory = new DepartmentEntityFactory(); + + @Override + public void toCompanyEntity(UnmappableCompanyDto dto, CompanyEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + if ( dto.getDepartment() != null ) { + if ( entity.getDepartment() == null ) { + entity.setDepartment( departmentEntityFactory.createDepartmentEntity() ); + } + unmappableDepartmentDtoToDepartmentEntity( dto.getDepartment(), entity.getDepartment() ); + } + else { + entity.setDepartment( null ); + } + } + + @Override + public void toInBetween(UnmappableDepartmentDto dto, DepartmentInBetween entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + } + + @Override + public void toDepartmentEntity(DepartmentInBetween dto, DepartmentEntity entity) { + if ( dto == null ) { + return; + } + + entity.setName( dto.getName() ); + } + + protected SecretaryEntity secretaryDtoToSecretaryEntity(SecretaryDto secretaryDto) { + if ( secretaryDto == null ) { + return null; + } + + SecretaryEntity secretaryEntity = new SecretaryEntity(); + + secretaryEntity.setName( secretaryDto.getName() ); + + return secretaryEntity; + } + + protected EmployeeEntity employeeDtoToEmployeeEntity(EmployeeDto employeeDto) { + if ( employeeDto == null ) { + return null; + } + + EmployeeEntity employeeEntity = new EmployeeEntity(); + + employeeEntity.setName( employeeDto.getName() ); + + return employeeEntity; + } + + protected Map secretaryDtoEmployeeDtoMapToSecretaryEntityEmployeeEntityMap(Map map) { + if ( map == null ) { + return null; + } + + Map map1 = LinkedHashMap.newLinkedHashMap( map.size() ); + + for ( java.util.Map.Entry entry : map.entrySet() ) { + SecretaryEntity key = secretaryDtoToSecretaryEntity( entry.getKey() ); + EmployeeEntity value = employeeDtoToEmployeeEntity( entry.getValue() ); + map1.put( key, value ); + } + + return map1; + } + + protected void unmappableDepartmentDtoToDepartmentEntity(UnmappableDepartmentDto unmappableDepartmentDto, DepartmentEntity mappingTarget) { + if ( unmappableDepartmentDto == null ) { + return; + } + + mappingTarget.setName( unmappableDepartmentDto.getName() ); + if ( mappingTarget.getSecretaryToEmployee() != null ) { + Map map = secretaryDtoEmployeeDtoMapToSecretaryEntityEmployeeEntityMap( unmappableDepartmentDto.getSecretaryToEmployee() ); + if ( map != null ) { + mappingTarget.getSecretaryToEmployee().clear(); + mappingTarget.getSecretaryToEmployee().putAll( map ); + } + else { + mappingTarget.setSecretaryToEmployee( null ); + } + } + else { + Map map = secretaryDtoEmployeeDtoMapToSecretaryEntityEmployeeEntityMap( unmappableDepartmentDto.getSecretaryToEmployee() ); + if ( map != null ) { + mappingTarget.setSecretaryToEmployee( map ); + } + } + } +}