From 10f855fa9ec3fd8290cbb2536b352e2f1997d3c4 Mon Sep 17 00:00:00 2001 From: Sjaak Derksen Date: Tue, 14 Aug 2018 23:53:41 +0200 Subject: [PATCH] #1532 using fields and constructor fragments optimizing DataTypeFactory usage --- .../model/AbstractMappingMethodBuilder.java | 2 +- .../internal/model/AnnotatedConstructor.java | 54 ++++++++++-- .../model/ContainerMappingMethod.java | 4 +- .../model/ContainerMappingMethodBuilder.java | 2 +- .../ap/internal/model/Decorator.java | 2 +- .../model/DefaultMapperReference.java | 2 +- .../mapstruct/ap/internal/model/Field.java | 10 +++ .../ap/internal/model/GeneratedType.java | 8 +- .../LifecycleCallbackMethodReference.java | 2 +- .../ap/internal/model/MapMappingMethod.java | 6 +- .../mapstruct/ap/internal/model/Mapper.java | 34 +++++--- .../internal/model/MappingBuilderContext.java | 8 +- .../ap/internal/model/MappingMethod.java | 6 +- .../model/NestedPropertyMappingMethod.java | 2 +- .../internal/model/NoArgumentConstructor.java | 42 +++++++++ .../ap/internal/model/PropertyMapping.java | 6 +- .../model/SupportingConstructorFragment.java | 85 +++++++++++++++++++ .../ap/internal/model/SupportingField.java | 76 +++++++++++++++++ ...thod.java => SupportingMappingMethod.java} | 66 +++++++++++++- .../ap/internal/model/common/SourceRHS.java | 2 +- .../internal/model/source/ForgedMethod.java | 2 +- .../AbstractToXmlGregorianCalendar.java | 57 +++++++++++++ .../builtin/BuiltInConstructorFragment.java | 12 +++ .../source/builtin/BuiltInFieldReference.java | 27 ++++++ .../model/source/builtin/BuiltInMethod.java | 14 ++- .../CalendarToXmlGregorianCalendar.java | 27 ++---- .../builtin/CalendarToZonedDateTime.java | 4 +- .../builtin/DateToXmlGregorianCalendar.java | 27 ++---- .../model/source/builtin/FinalField.java | 35 ++++++++ .../model/source/builtin/JaxbElemToValue.java | 5 +- .../JodaDateTimeToXmlGregorianCalendar.java | 32 ++----- ...daLocalDateTimeToXmlGregorianCalendar.java | 32 ++----- .../JodaLocalDateToXmlGregorianCalendar.java | 32 ++----- .../JodaLocalTimeToXmlGregorianCalendar.java | 32 ++----- .../LocalDateToXmlGregorianCalendar.java | 32 +++---- ...NewDatatypeFactoryConstructorFragment.java | 12 +++ .../builtin/StringToXmlGregorianCalendar.java | 27 ++---- .../XmlGregorianCalendarToCalendar.java | 5 +- .../builtin/XmlGregorianCalendarToDate.java | 5 +- .../XmlGregorianCalendarToJodaDateTime.java | 4 +- .../XmlGregorianCalendarToJodaLocalDate.java | 4 +- ...lGregorianCalendarToJodaLocalDateTime.java | 4 +- .../XmlGregorianCalendarToJodaLocalTime.java | 4 +- .../XmlGregorianCalendarToLocalDate.java | 5 +- .../builtin/XmlGregorianCalendarToString.java | 5 +- .../builtin/ZonedDateTimeToCalendar.java | 4 +- .../ZonedDateTimeToXmlGregorianCalendar.java | 21 ++--- ...nnotationBasedComponentModelProcessor.java | 43 +++++++--- .../processor/MapperCreationProcessor.java | 23 ++++- .../creation/MappingResolverImpl.java | 41 +++++---- .../mapstruct/ap/internal/util/Strings.java | 6 +- .../internal/model/AnnotatedConstructor.ftl | 12 ++- .../ap/internal/model/Annotation.ftl | 1 + .../model/AnnotationMapperReference.ftl | 1 + .../ap/internal/model/BeanMappingMethod.ftl | 1 + .../internal/model/DecoratorConstructor.ftl | 1 + .../internal/model/DefaultMapperReference.ftl | 1 + .../ap/internal/model/DelegatingMethod.ftl | 1 + .../ap/internal/model/EnumMappingMethod.ftl | 1 + .../org/mapstruct/ap/internal/model/Field.ftl | 1 + .../ap/internal/model/GeneratedType.ftl | 1 + .../ap/internal/model/IterableCreation.ftl | 1 + .../internal/model/IterableMappingMethod.ftl | 1 + .../LifecycleCallbackMethodReference.ftl | 1 + .../ap/internal/model/MapMappingMethod.ftl | 1 + .../ap/internal/model/MethodReference.ftl | 1 + .../model/NestedPropertyMappingMethod.ftl | 1 + .../internal/model/NoArgumentConstructor.ftl | 13 +++ .../ap/internal/model/PropertyMapping.ftl | 1 + .../ap/internal/model/ServicesEntry.ftl | 1 + .../ap/internal/model/StreamMappingMethod.ftl | 1 + .../ap/internal/model/TypeConversion.ftl | 1 + .../ap/internal/model/ValueMappingMethod.ftl | 1 + .../model/assignment/AdderWrapper.ftl | 1 + .../model/assignment/ArrayCopyWrapper.ftl | 1 + .../model/assignment/EnumConstantWrapper.ftl | 1 + ...anceSetterWrapperForCollectionsAndMaps.ftl | 1 + .../GetterWrapperForCollectionsAndMaps.ftl | 1 + .../model/assignment/Java8FunctionWrapper.ftl | 1 + .../model/assignment/LocalVarWrapper.ftl | 1 + .../model/assignment/SetterWrapper.ftl | 1 + .../SetterWrapperForCollectionsAndMaps.ftl | 1 + ...pperForCollectionsAndMapsWithNullCheck.ftl | 1 + .../model/assignment/UpdateWrapper.ftl | 1 + .../ap/internal/model/common/Parameter.ftl | 1 + .../ap/internal/model/common/SourceRHS.ftl | 1 + .../ap/internal/model/common/Type.ftl | 1 + .../CalendarToXmlGregorianCalendar.ftl | 12 +-- .../builtin/CalendarToZonedDateTime.ftl | 1 + .../builtin/DateToXmlGregorianCalendar.ftl | 12 +-- .../model/source/builtin/FinalField.ftl | 9 ++ .../model/source/builtin/JaxbElemToValue.ftl | 1 + .../JodaDateTimeToXmlGregorianCalendar.ftl | 25 +++--- ...odaLocalDateTimeToXmlGregorianCalendar.ftl | 24 +++--- .../JodaLocalDateToXmlGregorianCalendar.ftl | 16 ++-- .../JodaLocalTimeToXmlGregorianCalendar.ftl | 19 ++--- .../LocalDateToXmlGregorianCalendar.ftl | 19 ++--- .../NewDatatypeFactoryConstructorFragment.ftl | 14 +++ .../builtin/StringToXmlGregorianCalendar.ftl | 8 +- .../XmlGregorianCalendarToCalendar.ftl | 1 + .../builtin/XmlGregorianCalendarToDate.ftl | 1 + .../XmlGregorianCalendarToJodaDateTime.ftl | 1 + .../XmlGregorianCalendarToJodaLocalDate.ftl | 1 + ...mlGregorianCalendarToJodaLocalDateTime.ftl | 1 + .../XmlGregorianCalendarToJodaLocalTime.ftl | 1 + .../XmlGregorianCalendarToLocalDate.ftl | 1 + .../builtin/XmlGregorianCalendarToString.ftl | 1 + .../builtin/ZonedDateTimeToCalendar.ftl | 1 + .../ZonedDateTimeToXmlGregorianCalendar.ftl | 8 +- .../ap/internal/util/StringsTest.java | 26 +++--- .../ap/test/builtin/DatatypeFactoryTest.java | 69 +++++++++++++++ .../ap/test/builtin/bean/DatatypeFactory.java | 13 +++ ...mlGregorianCalendarFactorizedProperty.java | 26 ++++++ .../builtin/mapper/ToXmlGregCalMapper.java | 24 ++++++ .../shared/CustomerRecordDto.java | 33 +++++++ .../shared/CustomerRecordEntity.java | 33 +++++++ ...CustomerRecordSpringConstructorMapper.java | 23 +++++ .../SpringConstructorMapperTest.java | 49 +++++++++-- 118 files changed, 1117 insertions(+), 417 deletions(-) create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/NoArgumentConstructor.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/SupportingConstructorFragment.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/SupportingField.java rename processor/src/main/java/org/mapstruct/ap/internal/model/{VirtualMappingMethod.java => SupportingMappingMethod.java} (50%) create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/AbstractToXmlGregorianCalendar.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInConstructorFragment.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInFieldReference.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/FinalField.java create mode 100644 processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.java create mode 100644 processor/src/main/resources/org/mapstruct/ap/internal/model/NoArgumentConstructor.ftl create mode 100644 processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/FinalField.ftl create mode 100644 processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.ftl create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/DatatypeFactoryTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/DatatypeFactory.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/bean/XmlGregorianCalendarFactorizedProperty.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/ToXmlGregCalMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordEntity.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/CustomerRecordSpringConstructorMapper.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java b/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java index 88f6d6a12..04c64d606 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/AbstractMappingMethodBuilder.java @@ -37,7 +37,7 @@ public abstract class AbstractMappingMethodBuilder mapperReferences; private final List annotations; - private final boolean publicEmptyConstructor; + private final NoArgumentConstructor noArgumentConstructor; + private final Set fragments; - public AnnotatedConstructor(String name, List mapperReferences, - List annotations, boolean publicEmptyConstructor) { + public static AnnotatedConstructor forComponentModels(String name, + List mapperReferences, + List annotations, + Constructor constructor, + boolean includeNoArgConstructor) { + + NoArgumentConstructor noArgumentConstructor = null; + if ( constructor instanceof NoArgumentConstructor ) { + noArgumentConstructor = (NoArgumentConstructor) constructor; + } + NoArgumentConstructor noArgConstructorToInBecluded = null; + Set fragmentsToBeIncluded = Collections.emptySet(); + if ( includeNoArgConstructor ) { + if ( noArgumentConstructor != null ) { + noArgConstructorToInBecluded = noArgumentConstructor; + } + else { + noArgConstructorToInBecluded = new NoArgumentConstructor( name, fragmentsToBeIncluded ); + } + } + else if ( noArgumentConstructor != null ) { + fragmentsToBeIncluded = noArgumentConstructor.getFragments(); + } + return new AnnotatedConstructor( + name, + mapperReferences, + annotations, + noArgConstructorToInBecluded, + fragmentsToBeIncluded + ); + } + + private AnnotatedConstructor(String name, List mapperReferences, + List annotations, NoArgumentConstructor noArgumentConstructor, + Set fragments) { this.name = name; this.mapperReferences = mapperReferences; this.annotations = annotations; - this.publicEmptyConstructor = publicEmptyConstructor; + this.noArgumentConstructor = noArgumentConstructor; + this.fragments = fragments; } @Override @@ -60,7 +96,11 @@ public class AnnotatedConstructor extends ModelElement implements Constructor { return annotations; } - public boolean isPublicEmptyConstructor() { - return publicEmptyConstructor; + public NoArgumentConstructor getNoArgumentConstructor() { + return noArgumentConstructor; + } + + public Set getFragments() { + return fragments; } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java index c80eb593d..13759f5c1 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethod.java @@ -41,8 +41,8 @@ public abstract class ContainerMappingMethod extends NormalTypeMappingMethod { this.elementAssignment = parameterAssignment; this.loopVariableName = loopVariableName; this.selectionParameters = selectionParameters; - this.index1Name = Strings.getSaveVariableName( "i", existingVariables ); - this.index2Name = Strings.getSaveVariableName( "j", existingVariables ); + this.index1Name = Strings.getSafeVariableName( "i", existingVariables ); + this.index2Name = Strings.getSafeVariableName( "j", existingVariables ); } public Parameter getSourceParameter() { diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java index c4b96c034..5cd610c20 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/ContainerMappingMethodBuilder.java @@ -73,7 +73,7 @@ public abstract class ContainerMappingMethodBuilder methods, - List fields, Options options, VersionInformation versionInformation, + List fields, Options options, VersionInformation versionInformation, Accessibility accessibility, SortedSet extraImports, DecoratorConstructor decoratorConstructor) { super( diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/DefaultMapperReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/DefaultMapperReference.java index e1fc85eb4..924648368 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/DefaultMapperReference.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/DefaultMapperReference.java @@ -37,7 +37,7 @@ public class DefaultMapperReference extends MapperReference { importTypes.add( typeFactory.getType( "org.mapstruct.factory.Mappers" ) ); } - String variableName = Strings.getSaveVariableName( + String variableName = Strings.getSafeVariableName( type.getName(), otherMapperReferences ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/Field.java b/processor/src/main/java/org/mapstruct/ap/internal/model/Field.java index 1ce3753a2..a47c44f6d 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/Field.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/Field.java @@ -5,7 +5,9 @@ */ package org.mapstruct.ap.internal.model; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Set; import org.mapstruct.ap.internal.model.common.ModelElement; @@ -115,4 +117,12 @@ public class Field extends ModelElement { (other.variableName != null) : !this.variableName.equals( other.variableName ) ); } + public static List getFieldNames(Set fields) { + List names = new ArrayList( fields.size() ); + for ( Field field : fields ) { + names.add( field.getVariableName() ); + } + return names; + } + } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java b/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java index 352e65175..75703dcfa 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java @@ -44,7 +44,7 @@ public abstract class GeneratedType extends ModelElement { private final boolean suppressGeneratorVersionComment; private final VersionInformation versionInformation; private final Accessibility accessibility; - private List fields; + private List fields; private Constructor constructor; /** @@ -56,7 +56,7 @@ public abstract class GeneratedType extends ModelElement { // CHECKSTYLE:OFF protected GeneratedType(TypeFactory typeFactory, String packageName, String name, String superClassName, String interfacePackage, String interfaceName, List methods, - List fields, Options options, VersionInformation versionInformation, + List fields, Options options, VersionInformation versionInformation, Accessibility accessibility, SortedSet extraImportedTypes, Constructor constructor) { this.packageName = packageName; this.name = name; @@ -123,11 +123,11 @@ public abstract class GeneratedType extends ModelElement { return methods; } - public List getFields() { + public List getFields() { return fields; } - public void setFields(List fields) { + public void setFields(List fields) { this.fields = fields; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.java index cb3d892de..10fb2d9c7 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.java @@ -42,7 +42,7 @@ public class LifecycleCallbackMethodReference extends MethodReference { this.methodResultType = containingMethod.getResultType(); if ( hasReturnType() ) { - this.targetVariableName = Strings.getSaveVariableName( "target", existingVariableNames ); + this.targetVariableName = Strings.getSafeVariableName( "target", existingVariableNames ); existingVariableNames.add( this.targetVariableName ); } else { diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java index e46e1194d..e4075757d 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java @@ -266,21 +266,21 @@ public class MapMappingMethod extends NormalTypeMappingMethod { } public String getKeyVariableName() { - return Strings.getSaveVariableName( + return Strings.getSafeVariableName( "key", getParameterNames() ); } public String getValueVariableName() { - return Strings.getSaveVariableName( + return Strings.getSafeVariableName( "value", getParameterNames() ); } public String getEntryVariableName() { - return Strings.getSaveVariableName( + return Strings.getSafeVariableName( "entry", getParameterNames() ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java b/processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java index 89b3c36ef..e4b89b4c0 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/Mapper.java @@ -6,6 +6,7 @@ package org.mapstruct.ap.internal.model; import java.util.List; +import java.util.Set; import java.util.SortedSet; import javax.lang.model.element.Element; @@ -34,15 +35,14 @@ public class Mapper extends GeneratedType { private final boolean customPackage; private final boolean customImplName; - private final List referencedMappers; private Decorator decorator; @SuppressWarnings( "checkstyle:parameternumber" ) private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName, String interfacePackage, String interfaceName, boolean customPackage, boolean customImplName, List methods, Options options, VersionInformation versionInformation, - Accessibility accessibility, List referencedMappers, Decorator decorator, - SortedSet extraImportedTypes) { + Accessibility accessibility, List fields, Constructor constructor, + Decorator decorator, SortedSet extraImportedTypes ) { super( typeFactory, @@ -52,17 +52,16 @@ public class Mapper extends GeneratedType { interfacePackage, interfaceName, methods, - referencedMappers, + fields, options, versionInformation, accessibility, extraImportedTypes, - null + constructor ); this.customPackage = customPackage; this.customImplName = customImplName; - this.referencedMappers = referencedMappers; this.decorator = decorator; } @@ -71,7 +70,8 @@ public class Mapper extends GeneratedType { private TypeFactory typeFactory; private TypeElement element; private List mappingMethods; - private List mapperReferences; + private List fields; + private Set fragments; private SortedSet extraImportedTypes; private Elements elementUtils; @@ -93,8 +93,13 @@ public class Mapper extends GeneratedType { return this; } - public Builder mapperReferences(List mapperReferences) { - this.mapperReferences = mapperReferences; + public Builder fields(List fields) { + this.fields = fields; + return this; + } + + public Builder constructorFragments(Set fragments) { + this.fragments = fragments; return this; } @@ -146,7 +151,10 @@ public class Mapper extends GeneratedType { String elementPackage = elementUtils.getPackageOf( element ).getQualifiedName().toString(); String packageName = implPackage.replace( PACKAGE_NAME_PLACEHOLDER, elementPackage ); - + Constructor constructor = null; + if ( !fragments.isEmpty() ) { + constructor = new NoArgumentConstructor( implementationName, fragments ); + } return new Mapper( typeFactory, packageName, @@ -160,15 +168,13 @@ public class Mapper extends GeneratedType { options, versionInformation, Accessibility.fromModifiers( element.getModifiers() ), - mapperReferences, + fields, + constructor, decorator, extraImportedTypes ); } - } - public List getReferencedMappers() { - return referencedMappers; } public Decorator getDecorator() { diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java index 3efa16ada..48ee36be0 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingBuilderContext.java @@ -37,7 +37,7 @@ import org.mapstruct.ap.spi.MappingExclusionProvider; *
    *
  • Input for the building process, such as the source model (mapping methods found) and mapper references.
  • *
  • Required factory, utility, reporting methods for building the mappings.
  • - *
  • Means to harbor results produced by the builders, such as forged- and virtual mapping methods that should be + *
  • Means to harbor results produced by the builders, such as forged- and supported mapping methods that should be * generated in a later stage.
  • *
* @@ -94,7 +94,7 @@ public class MappingBuilderContext { SelectionParameters selectionParameters, SourceRHS sourceRHS, boolean preferUpdateMethods); - Set getUsedVirtualMappings(); + Set getUsedSupportedMappings(); } private final TypeFactory typeFactory; @@ -209,8 +209,8 @@ public class MappingBuilderContext { return existingMappingMethod; } - public Set getUsedVirtualMappings() { - return mappingResolver.getUsedVirtualMappings(); + public Set getUsedSupportedMappings() { + return mappingResolver.getUsedSupportedMappings(); } /** diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingMethod.java index 3a4a9c130..0a23001d0 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MappingMethod.java @@ -5,7 +5,7 @@ */ package org.mapstruct.ap.internal.model; -import static org.mapstruct.ap.internal.util.Strings.getSaveVariableName; +import static org.mapstruct.ap.internal.util.Strings.getSafeVariableName; import static org.mapstruct.ap.internal.util.Strings.join; import java.util.ArrayList; @@ -91,12 +91,12 @@ public abstract class MappingMethod extends ModelElement { return targetParameter.getName(); } else if ( getResultType().isArrayType() ) { - String name = getSaveVariableName( getResultType().getComponentType().getName() + "Tmp", existingVarNames ); + String name = getSafeVariableName( getResultType().getComponentType().getName() + "Tmp", existingVarNames ); existingVarNames.add( name ); return name; } else { - String name = getSaveVariableName( getResultType().getName(), existingVarNames ); + String name = getSafeVariableName( getResultType().getName(), existingVarNames ); existingVarNames.add( name ); return name; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java index fbe9076a6..f77218431 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.java @@ -58,7 +58,7 @@ public class NestedPropertyMappingMethod extends MappingMethod { final List thrownTypes = new ArrayList(); List safePropertyEntries = new ArrayList(); for ( PropertyEntry propertyEntry : propertyEntries ) { - String safeName = Strings.getSaveVariableName( propertyEntry.getName(), existingVariableNames ); + String safeName = Strings.getSafeVariableName( propertyEntry.getName(), existingVariableNames ); safePropertyEntries.add( new SafePropertyEntry( propertyEntry, safeName ) ); existingVariableNames.add( safeName ); thrownTypes.addAll( ctx.getTypeFactory().getThrownTypes( diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/NoArgumentConstructor.java b/processor/src/main/java/org/mapstruct/ap/internal/model/NoArgumentConstructor.java new file mode 100644 index 000000000..1101c1589 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/NoArgumentConstructor.java @@ -0,0 +1,42 @@ +/* + * 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.internal.model; + +import java.util.Collections; +import java.util.Set; + +import org.mapstruct.ap.internal.model.common.ModelElement; +import org.mapstruct.ap.internal.model.common.Type; + +/** + * Represents a constructor that is used for constructor injection. + * + * @author Sjaak Derksen + */ +public class NoArgumentConstructor extends ModelElement implements Constructor { + + private final String name; + private final Set fragments; + + public NoArgumentConstructor(String name, Set fragments) { + this.name = name; + this.fragments = fragments; + } + + @Override + public Set getImportTypes() { + return Collections.emptySet(); + } + + @Override + public String getName() { + return name; + } + + public Set getFragments() { + return fragments; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index fc996c052..7c632f1f8 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -511,7 +511,7 @@ public class PropertyMapping extends ModelElement { // forge a method from the parameter type to the last entry type. String forgedName = Strings.joinAndCamelize( sourceReference.getElementNames() ); - forgedName = Strings.getSaveVariableName( forgedName, ctx.getNamesOfMappingsToGenerate() ); + forgedName = Strings.getSafeVariableName( forgedName, ctx.getNamesOfMappingsToGenerate() ); ForgedMethod methodRef = new ForgedMethod( forgedName, sourceReference.getParameter().getType(), @@ -601,7 +601,7 @@ public class PropertyMapping extends ModelElement { private ForgedMethod prepareForgedMethod(Type sourceType, Type targetType, SourceRHS source, ExecutableElement element, String suffix) { String name = getName( sourceType, targetType ); - name = Strings.getSaveVariableName( name, ctx.getNamesOfMappingsToGenerate() ); + name = Strings.getSafeVariableName( name, ctx.getNamesOfMappingsToGenerate() ); // copy mapper configuration from the source method, its the same mapper MapperConfiguration config = method.getMapperConfiguration(); @@ -647,7 +647,7 @@ public class PropertyMapping extends ModelElement { } String name = getName( sourceType, targetType ); - name = Strings.getSaveVariableName( name, ctx.getNamesOfMappingsToGenerate() ); + name = Strings.getSafeVariableName( name, ctx.getNamesOfMappingsToGenerate() ); List parameters = new ArrayList( method.getContextParameters() ); Type returnType; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingConstructorFragment.java b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingConstructorFragment.java new file mode 100644 index 000000000..14a13ca73 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingConstructorFragment.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.internal.model; + +import java.util.Collections; +import java.util.Set; + +import org.mapstruct.ap.internal.model.common.ModelElement; +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.source.builtin.BuiltInConstructorFragment; + +/** + * A mapper instance field, initialized as null + * + * @author Sjaak Derksen + */ +public class SupportingConstructorFragment extends ModelElement { + + private final String templateName; + private final SupportingMappingMethod definingMethod; + + public SupportingConstructorFragment(SupportingMappingMethod definingMethod, + BuiltInConstructorFragment constructorFragment) { + this.templateName = getTemplateNameForClass( constructorFragment.getClass() ); + this.definingMethod = definingMethod; + } + + @Override + public String getTemplateName() { + return templateName; + } + + @Override + public Set getImportTypes() { + return Collections.emptySet(); + } + + public SupportingMappingMethod getDefiningMethod() { + return definingMethod; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ( ( templateName == null ) ? 0 : templateName.hashCode() ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( this == obj ) { + return true; + } + if ( obj == null ) { + return false; + } + if ( getClass() != obj.getClass() ) { + return false; + } + SupportingConstructorFragment other = (SupportingConstructorFragment) obj; + if ( templateName == null ) { + if ( other.templateName != null ) { + return false; + } + } + else if ( !templateName.equals( other.templateName ) ) { + return false; + } + return true; + } + + public static void addAllFragmentsIn(Set supportingMappingMethods, + Set targets) { + for ( SupportingMappingMethod supportingMappingMethod : supportingMappingMethods ) { + SupportingConstructorFragment fragment = supportingMappingMethod.getSupportingConstructorFragment(); + if ( fragment != null ) { + targets.add( supportingMappingMethod.getSupportingConstructorFragment() ); + } + } + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingField.java b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingField.java new file mode 100644 index 000000000..e9d0d1e3c --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingField.java @@ -0,0 +1,76 @@ +/* + * 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.internal.model; + +import java.util.Set; + +import org.mapstruct.ap.internal.model.source.builtin.BuiltInFieldReference; + +/** + * supports the + * + * @author Sjaak Derksen + */ +public class SupportingField extends Field { + + private final String templateName; + private final SupportingMappingMethod definingMethod; + + public SupportingField(SupportingMappingMethod definingMethod, BuiltInFieldReference fieldReference, String name) { + super( fieldReference.getType(), name, true ); + this.templateName = getTemplateNameForClass( fieldReference.getClass() ); + this.definingMethod = definingMethod; + } + + @Override + public String getTemplateName() { + return templateName; + } + + public SupportingMappingMethod getDefiningMethod() { + return definingMethod; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ( ( templateName == null ) ? 0 : templateName.hashCode() ); + return result; + } + + @Override + public boolean equals(Object obj) { + if ( this == obj ) { + return true; + } + if ( obj == null ) { + return false; + } + if ( getClass() != obj.getClass() ) { + return false; + } + SupportingField other = (SupportingField) obj; + if ( templateName == null ) { + if ( other.templateName != null ) { + return false; + } + } + else if ( !templateName.equals( other.templateName ) ) { + return false; + } + return true; + } + + public static void addAllFieldsIn(Set supportingMappingMethods, Set targets) { + for ( SupportingMappingMethod supportingMappingMethod : supportingMappingMethods ) { + Field field = supportingMappingMethod.getSupportingField(); + if ( field != null ) { + targets.add( supportingMappingMethod.getSupportingField() ); + } + } + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingMappingMethod.java similarity index 50% rename from processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java rename to processor/src/main/java/org/mapstruct/ap/internal/model/SupportingMappingMethod.java index 278783902..8827bc5d3 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/SupportingMappingMethod.java @@ -8,29 +8,79 @@ package org.mapstruct.ap.internal.model; import java.util.Set; import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.source.builtin.BuiltInFieldReference; import org.mapstruct.ap.internal.model.source.builtin.BuiltInMethod; +import org.mapstruct.ap.internal.model.source.builtin.NewDatatypeFactoryConstructorFragment; +import org.mapstruct.ap.internal.util.Strings; /** * A mapping method which is not based on an actual method declared in the original mapper interface but is added as * private method to map a certain source/target type combination. Based on a {@link BuiltInMethod}. * + * Specific templates all point to this class, for instance: + * {@link org.mapstruct.ap.internal.model.source.builtin.XmlGregorianCalendarToCalendar}, + * but also used fields and constructor elements, e.g. + * {@link org.mapstruct.ap.internal.model.source.builtin.FinalField} and + * {@link NewDatatypeFactoryConstructorFragment} + * * @author Gunnar Morling */ -public class VirtualMappingMethod extends MappingMethod { +public class SupportingMappingMethod extends MappingMethod { private final String templateName; private final Set importTypes; + private final Field supportingField; + private final SupportingConstructorFragment supportingConstructorFragment; - public VirtualMappingMethod(BuiltInMethod method) { + public SupportingMappingMethod(BuiltInMethod method, Set existingFields) { super( method ); this.importTypes = method.getImportTypes(); this.templateName = getTemplateNameForClass( method.getClass() ); + if ( method.getFieldReference() != null ) { + this.supportingField = getSafeField( method.getFieldReference(), existingFields ); + } + else { + this.supportingField = null; + } + if ( method.getConstructorFragment() != null ) { + this.supportingConstructorFragment = new SupportingConstructorFragment( + this, + method.getConstructorFragment() + ); + } + else { + this.supportingConstructorFragment = null; + } } - public VirtualMappingMethod(HelperMethod method) { + private Field getSafeField(BuiltInFieldReference ref, Set existingFields) { + Field result = null; + String name = ref.getVariableName(); + for ( Field existingField : existingFields ) { + if ( existingField.getType().equals( ref.getType() ) ) { + // field type already exist, use that one + return existingField; + } + } + for ( Field existingField : existingFields ) { + if ( existingField.getVariableName().equals( ref.getVariableName() ) ) { + // field with name exist, however its a wrong type + name = Strings.getSafeVariableName( name, Field.getFieldNames( existingFields ) ); + } + } + if ( result == null ) { + result = new SupportingField( this, ref, name ); + } + + return result; + } + + public SupportingMappingMethod(HelperMethod method) { super( method ); this.importTypes = method.getImportTypes(); this.templateName = getTemplateNameForClass( method.getClass() ); + this.supportingField = null; + this.supportingConstructorFragment = null; } @Override @@ -66,6 +116,14 @@ public class VirtualMappingMethod extends MappingMethod { throw new IllegalArgumentException( "No type for given name '" + name + "' found in 'importTypes'." ); } + public Field getSupportingField() { + return supportingField; + } + + public SupportingConstructorFragment getSupportingConstructorFragment() { + return supportingConstructorFragment; + } + @Override public int hashCode() { final int prime = 31; @@ -85,7 +143,7 @@ public class VirtualMappingMethod extends MappingMethod { if ( getClass() != obj.getClass() ) { return false; } - VirtualMappingMethod other = (VirtualMappingMethod) obj; + SupportingMappingMethod other = (SupportingMappingMethod) obj; if ( templateName == null ) { if ( other.templateName != null ) { return false; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/SourceRHS.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/SourceRHS.java index fc73ab4a6..dd36fb2cc 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/SourceRHS.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/SourceRHS.java @@ -69,7 +69,7 @@ public class SourceRHS extends ModelElement implements Assignment { @Override public String createLocalVarName(String desiredName) { - String result = Strings.getSaveVariableName( desiredName, existingVariableNames ); + String result = Strings.getSafeVariableName( desiredName, existingVariableNames ); existingVariableNames.add( result ); return result; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java index 363be4d41..19544de10 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/ForgedMethod.java @@ -87,7 +87,7 @@ public class ForgedMethod implements Method { ParameterProvidedMethods parameterProvidedMethods, ForgedMethodHistory history, MappingOptions mappingOptions, boolean forgedNameBased) { String sourceParamName = Strings.decapitalize( sourceType.getName() ); - String sourceParamSafeName = Strings.getSaveVariableName( sourceParamName ); + String sourceParamSafeName = Strings.getSafeVariableName( sourceParamName ); this.parameters = new ArrayList( 1 + additionalParameters.size() ); Parameter sourceParameter = new Parameter( sourceParamSafeName, sourceType ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/AbstractToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/AbstractToXmlGregorianCalendar.java new file mode 100644 index 000000000..691d93d0f --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/AbstractToXmlGregorianCalendar.java @@ -0,0 +1,57 @@ +/* + * 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.internal.model.source.builtin; + +import java.util.Set; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.common.TypeFactory; +import org.mapstruct.ap.internal.util.Strings; + +import static org.mapstruct.ap.internal.util.Collections.asSet; + +/** + * @author Sjaak Derksen + */ +public abstract class AbstractToXmlGregorianCalendar extends BuiltInMethod { + + private final Type returnType; + private final Set importTypes; + private final Type dataTypeFactoryType; + + public AbstractToXmlGregorianCalendar(TypeFactory typeFactory) { + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); + this.dataTypeFactoryType = typeFactory.getType( DatatypeFactory.class ); + this.importTypes = asSet( + returnType, + dataTypeFactoryType, + typeFactory.getType( DatatypeConfigurationException.class ) + ); + } + + @Override + public Set getImportTypes() { + return importTypes; + } + + @Override + public Type getReturnType() { + return returnType; + } + + @Override + public BuiltInFieldReference getFieldReference() { + return new FinalField( dataTypeFactoryType, Strings.decapitalize( DatatypeFactory.class.getSimpleName() ) ); + } + + @Override + public BuiltInConstructorFragment getConstructorFragment() { + return new NewDatatypeFactoryConstructorFragment( ); + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInConstructorFragment.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInConstructorFragment.java new file mode 100644 index 000000000..ae8dbcc7c --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInConstructorFragment.java @@ -0,0 +1,12 @@ +/* + * 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.internal.model.source.builtin; + +/** + * ConstructorFragments are 'code snippets' added to the constructor to initialize fields used by {@link BuiltInMethod} + */ +public interface BuiltInConstructorFragment { +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInFieldReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInFieldReference.java new file mode 100644 index 000000000..0eb15f289 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInFieldReference.java @@ -0,0 +1,27 @@ +/* + * 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.internal.model.source.builtin; + +import org.mapstruct.ap.internal.model.common.Type; + +/** + * reference used by BuiltInMethod to create an additional field in the mapper. + */ +public interface BuiltInFieldReference { + + /** + * + * @return variable name of the field + */ + String getVariableName(); + + /** + * + * @return type of the field + */ + Type getType(); + +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMethod.java index 4b1c4eeec..8716b235e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMethod.java @@ -5,14 +5,11 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.first; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; - import javax.lang.model.element.ExecutableElement; import org.mapstruct.ap.internal.conversion.SimpleConversion; @@ -26,6 +23,8 @@ import org.mapstruct.ap.internal.model.source.ParameterProvidedMethods; import org.mapstruct.ap.internal.util.MapperConfiguration; import org.mapstruct.ap.internal.util.Strings; +import static org.mapstruct.ap.internal.util.Collections.first; + /** * Represents a "built-in" mapping method which will be added as private method to the generated mapper. Built-in * methods are used in cases where a {@link SimpleConversion} doesn't suffice, e.g. as several lines of source code or a @@ -275,4 +274,13 @@ public abstract class BuiltInMethod implements Method { public MappingOptions getMappingOptions() { return MappingOptions.empty(); } + + public BuiltInFieldReference getFieldReference() { + return null; + } + + public BuiltInConstructorFragment getConstructorFragment() { + return null; + } + } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.java index fc35f9a36..c8cd89169 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.java @@ -5,45 +5,38 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Set; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; - import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class CalendarToXmlGregorianCalendar extends BuiltInMethod { +public class CalendarToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public CalendarToXmlGregorianCalendar(TypeFactory typeFactory) { + super( typeFactory ); this.parameter = new Parameter( "cal ", typeFactory.getType( Calendar.class ) ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( GregorianCalendar.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( GregorianCalendar.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( this.importTypes ); + return result; } @Override @@ -51,8 +44,4 @@ public class CalendarToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.java index 88c10f8ae..478f03ee4 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Set; @@ -16,6 +14,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JavaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * {@link BuiltInMethod} for mapping between {@link Calendar} and {@link ZonedDateTime}. *

diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.java index a293abe8d..036edd83a 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.java @@ -5,45 +5,38 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Date; import java.util.GregorianCalendar; import java.util.Set; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; - import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class DateToXmlGregorianCalendar extends BuiltInMethod { +public class DateToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public DateToXmlGregorianCalendar(TypeFactory typeFactory) { + super( typeFactory ); this.parameter = new Parameter( "date", typeFactory.getType( Date.class ) ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( GregorianCalendar.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( GregorianCalendar.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( this.importTypes ); + return result; } @Override @@ -51,8 +44,4 @@ public class DateToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/FinalField.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/FinalField.java new file mode 100644 index 000000000..5ce416711 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/FinalField.java @@ -0,0 +1,35 @@ +/* + * 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.internal.model.source.builtin; + +import org.mapstruct.ap.internal.model.common.Type; + +/** + * A mapper instance field, initialized as null + * + * @author Sjaak Derksen + */ +public class FinalField implements BuiltInFieldReference { + + private final Type type; + private String variableName; + + public FinalField(Type type, String variableName) { + this.type = type; + this.variableName = variableName; + } + + @Override + public String getVariableName() { + return variableName; + } + + @Override + public Type getType() { + return type; + } + +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.java index 09baa8b68..1ae3efe3c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.java @@ -5,16 +5,15 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; - import javax.xml.bind.JAXBElement; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java index 90a41ab9e..d0b06300c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.java @@ -5,14 +5,8 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; - import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; @@ -21,30 +15,20 @@ import org.mapstruct.ap.internal.util.JodaTimeConstants; /** * @author Sjaak Derksen */ -public class JodaDateTimeToXmlGregorianCalendar extends BuiltInMethod { +public class JodaDateTimeToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; - private final Set importTypes; public JodaDateTimeToXmlGregorianCalendar(TypeFactory typeFactory) { - this.parameter = new Parameter( - "dt", - typeFactory.getType( JodaTimeConstants.DATE_TIME_FQN ) - ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - - this.importTypes = asSet( - returnType, - parameter.getType(), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) - ); + super( typeFactory ); + this.parameter = new Parameter("dt", typeFactory.getType( JodaTimeConstants.DATE_TIME_FQN ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.add( parameter.getType() ); + return result; } @Override @@ -52,8 +36,4 @@ public class JodaDateTimeToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.java index 32e5c7c55..fa87bff2d 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.java @@ -5,48 +5,38 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; - -import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConstants; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class JodaLocalDateTimeToXmlGregorianCalendar extends BuiltInMethod { +public class JodaLocalDateTimeToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public JodaLocalDateTimeToXmlGregorianCalendar(TypeFactory typeFactory) { - this.parameter = new Parameter( - "dt", - typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN ) - ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - + super( typeFactory ); + this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN ) ); this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeConstants.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( DatatypeConstants.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; } @Override @@ -54,8 +44,4 @@ public class JodaLocalDateTimeToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java index ef04de528..e958ca952 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.java @@ -5,48 +5,38 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; - -import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConstants; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class JodaLocalDateToXmlGregorianCalendar extends BuiltInMethod { +public class JodaLocalDateToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public JodaLocalDateToXmlGregorianCalendar(TypeFactory typeFactory) { - this.parameter = new Parameter( - "dt", - typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN ) - ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - + super( typeFactory ); + this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN ) ); this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeConstants.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( DatatypeConstants.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; } @Override @@ -54,8 +44,4 @@ public class JodaLocalDateToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.java index 771a2febb..e9daac39c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.java @@ -5,48 +5,38 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; - -import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConstants; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class JodaLocalTimeToXmlGregorianCalendar extends BuiltInMethod { +public class JodaLocalTimeToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public JodaLocalTimeToXmlGregorianCalendar(TypeFactory typeFactory) { - this.parameter = new Parameter( - "dt", - typeFactory.getType( JodaTimeConstants.LOCAL_TIME_FQN ) - ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); - + super( typeFactory ); + this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_TIME_FQN ) ); this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeConstants.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( DatatypeConstants.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; } @Override @@ -54,8 +44,4 @@ public class JodaLocalTimeToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java index 4c1861725..96c8a8a31 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java @@ -5,53 +5,43 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.time.LocalDate; import java.util.Set; - -import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeConstants; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Gunnar Morling */ -public class LocalDateToXmlGregorianCalendar extends BuiltInMethod { +public class LocalDateToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public LocalDateToXmlGregorianCalendar(TypeFactory typeFactory) { + super( typeFactory ); this.parameter = new Parameter( "localDate", typeFactory.getType( LocalDate.class ) ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ), typeFactory.getType( DatatypeConstants.class ) ); } + @Override + public Set getImportTypes() { + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; + } + @Override public Parameter getParameter() { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } - - @Override - public Set getImportTypes() { - return importTypes; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.java new file mode 100644 index 000000000..a302b5937 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.java @@ -0,0 +1,12 @@ +/* + * 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.internal.model.source.builtin; + +public class NewDatatypeFactoryConstructorFragment implements BuiltInConstructorFragment { + + public NewDatatypeFactoryConstructorFragment() { + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.java index d8a660afb..9f0d2ad20 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.java @@ -5,49 +5,43 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import java.util.Set; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; - import org.mapstruct.ap.internal.model.common.ConversionContext; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ -public class StringToXmlGregorianCalendar extends BuiltInMethod { +public class StringToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public StringToXmlGregorianCalendar(TypeFactory typeFactory) { + super( typeFactory ); this.parameter = new Parameter( "date", typeFactory.getType( String.class ) ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( - returnType, typeFactory.getType( GregorianCalendar.class ), typeFactory.getType( SimpleDateFormat.class ), typeFactory.getType( DateFormat.class ), - typeFactory.getType( ParseException.class ), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( ParseException.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; } @Override @@ -55,11 +49,6 @@ public class StringToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } - @Override public String getContextParameter(ConversionContext conversionContext) { return conversionContext.getDateFormat() != null ? "\"" + conversionContext.getDateFormat() + "\"" : "null"; diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.java index 3c8706fef..f4d8ceee9 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.java @@ -5,17 +5,16 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Calendar; import java.util.Set; - import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.java index 49693748f..7f270be5f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.java @@ -5,17 +5,16 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Date; import java.util.Set; - import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java index 3c79036ac..ab2db49e8 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.XMLGregorianCalendar; @@ -16,6 +14,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java index 8828f2578..d26cde021 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.XMLGregorianCalendar; @@ -16,6 +14,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.java index 2e3a07526..700c19c21 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.XMLGregorianCalendar; @@ -16,6 +14,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.java index ba294978a..b3eae2031 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.util.Set; import javax.xml.datatype.DatatypeConstants; import javax.xml.datatype.XMLGregorianCalendar; @@ -16,6 +14,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JodaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java index d339b88cb..1da6951b9 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java @@ -5,17 +5,16 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.time.LocalDate; import java.util.Set; - import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Gunnar Morling */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.java index 97b80b987..73ac51fa2 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.java @@ -5,12 +5,9 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.text.SimpleDateFormat; import java.util.Date; import java.util.Set; - import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.ConversionContext; @@ -18,6 +15,8 @@ import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * @author Sjaak Derksen */ diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.java index 43d8b08f7..7e7a0751d 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.java @@ -5,8 +5,6 @@ */ package org.mapstruct.ap.internal.model.source.builtin; -import static org.mapstruct.ap.internal.util.Collections.asSet; - import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Set; @@ -17,6 +15,8 @@ import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; import org.mapstruct.ap.internal.util.JavaTimeConstants; +import static org.mapstruct.ap.internal.util.Collections.asSet; + /** * {@link BuiltInMethod} for mapping between {@link Calendar} and {@link ZonedDateTime}. *

diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.java index 6c6492988..27a39cd2b 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.java @@ -8,9 +8,6 @@ package org.mapstruct.ap.internal.model.source.builtin; import java.time.ZonedDateTime; import java.util.GregorianCalendar; import java.util.Set; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import org.mapstruct.ap.internal.model.common.Parameter; import org.mapstruct.ap.internal.model.common.Type; @@ -21,28 +18,26 @@ import static org.mapstruct.ap.internal.util.Collections.asSet; /** * @author Christian Bandowski */ -public class ZonedDateTimeToXmlGregorianCalendar extends BuiltInMethod { +public class ZonedDateTimeToXmlGregorianCalendar extends AbstractToXmlGregorianCalendar { private final Parameter parameter; - private final Type returnType; private final Set importTypes; public ZonedDateTimeToXmlGregorianCalendar(TypeFactory typeFactory) { + super( typeFactory ); this.parameter = new Parameter( "zdt ", typeFactory.getType( ZonedDateTime.class ) ); - this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); this.importTypes = asSet( - returnType, parameter.getType(), - typeFactory.getType( DatatypeFactory.class ), - typeFactory.getType( GregorianCalendar.class ), - typeFactory.getType( DatatypeConfigurationException.class ) + typeFactory.getType( GregorianCalendar.class ) ); } @Override public Set getImportTypes() { - return importTypes; + Set result = super.getImportTypes(); + result.addAll( importTypes ); + return result; } @Override @@ -50,8 +45,4 @@ public class ZonedDateTimeToXmlGregorianCalendar extends BuiltInMethod { return parameter; } - @Override - public Type getReturnType() { - return returnType; - } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/AnnotationBasedComponentModelProcessor.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/AnnotationBasedComponentModelProcessor.java index db42a7158..5cd44e27c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/AnnotationBasedComponentModelProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/AnnotationBasedComponentModelProcessor.java @@ -63,12 +63,15 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle } List annotations = getMapperReferenceAnnotations(); - ListIterator iterator = mapper.getReferencedMappers().listIterator(); + ListIterator iterator = mapper.getFields().listIterator(); while ( iterator.hasNext() ) { - MapperReference reference = iterator.next(); - iterator.remove(); - iterator.add( replacementMapperReference( reference, annotations, injectionStrategy ) ); + + Field reference = iterator.next(); + if ( reference instanceof MapperReference ) { + iterator.remove(); + iterator.add( replacementMapperReference( reference, annotations, injectionStrategy ) ); + } } if ( injectionStrategy == InjectionStrategyPrism.CONSTRUCTOR ) { @@ -97,8 +100,18 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle decorator.setFields( replacement ); } + private List toMapperReferences(List fields) { + List mapperReferences = new ArrayList( ); + for ( Field field : fields ) { + if ( field instanceof MapperReference ) { + mapperReferences.add( (MapperReference) field ); + } + } + return mapperReferences; + } + private void buildConstructors(Mapper mapper) { - if ( !mapper.getReferencedMappers().isEmpty() ) { + if ( !toMapperReferences( mapper.getFields() ).isEmpty() ) { AnnotatedConstructor annotatedConstructor = buildAnnotatedConstructorForMapper( mapper ); if ( !annotatedConstructor.getMapperReferences().isEmpty() ) { @@ -117,10 +130,11 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle } private AnnotatedConstructor buildAnnotatedConstructorForMapper(Mapper mapper) { + List mapperReferences = toMapperReferences( mapper.getFields() ); List mapperReferencesForConstructor = - new ArrayList( mapper.getReferencedMappers().size() ); + new ArrayList( mapperReferences.size() ); - for ( MapperReference mapperReference : mapper.getReferencedMappers() ) { + for ( MapperReference mapperReference : mapperReferences ) { if ( mapperReference.isUsed() ) { mapperReferencesForConstructor.add( (AnnotationMapperReference) mapperReference ); } @@ -130,11 +144,13 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle removeDuplicateAnnotations( mapperReferencesForConstructor, mapperReferenceAnnotations ); - return new AnnotatedConstructor( + return AnnotatedConstructor.forComponentModels( mapper.getName(), mapperReferencesForConstructor, mapperReferenceAnnotations, - additionalPublicEmptyConstructor() ); + mapper.getConstructor(), + additionalPublicEmptyConstructor() + ); } private AnnotatedConstructor buildAnnotatedConstructorForDecorator(Decorator decorator) { @@ -151,13 +167,16 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle removeDuplicateAnnotations( mapperReferencesForConstructor, mapperReferenceAnnotations ); - return new AnnotatedConstructor( + return AnnotatedConstructor.forComponentModels( decorator.getName(), mapperReferencesForConstructor, mapperReferenceAnnotations, - additionalPublicEmptyConstructor() ); + decorator.getConstructor(), + additionalPublicEmptyConstructor() + ); } + /** * Removes duplicate constructor parameter annotations. If an annotation is already present on the constructor, it * does not have be defined on the constructor parameter, too. For example, for CDI, the javax.inject.Inject @@ -204,7 +223,7 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle * @param injectionStrategyPrism strategy for injection * @return the mapper reference replacing the original one */ - protected MapperReference replacementMapperReference(Field originalReference, List annotations, + protected Field replacementMapperReference(Field originalReference, List annotations, InjectionStrategyPrism injectionStrategyPrism) { boolean finalField = injectionStrategyPrism == InjectionStrategyPrism.CONSTRUCTOR && !additionalPublicEmptyConstructor(); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java index 40b7c3a3d..abc982863 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/MapperCreationProcessor.java @@ -7,8 +7,10 @@ package org.mapstruct.ap.internal.processor; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import javax.lang.model.element.ExecutableElement; @@ -26,6 +28,7 @@ import org.mapstruct.ap.internal.model.Decorator; import org.mapstruct.ap.internal.model.DefaultMapperReference; import org.mapstruct.ap.internal.model.DelegatingMethod; import org.mapstruct.ap.internal.model.EnumMappingMethod; +import org.mapstruct.ap.internal.model.Field; import org.mapstruct.ap.internal.model.IterableMappingMethod; import org.mapstruct.ap.internal.model.MapMappingMethod; import org.mapstruct.ap.internal.model.Mapper; @@ -33,6 +36,7 @@ import org.mapstruct.ap.internal.model.MapperReference; import org.mapstruct.ap.internal.model.MappingBuilderContext; import org.mapstruct.ap.internal.model.MappingMethod; import org.mapstruct.ap.internal.model.StreamMappingMethod; +import org.mapstruct.ap.internal.model.SupportingConstructorFragment; import org.mapstruct.ap.internal.model.ValueMappingMethod; import org.mapstruct.ap.internal.model.common.Type; import org.mapstruct.ap.internal.model.common.TypeFactory; @@ -56,6 +60,8 @@ import org.mapstruct.ap.internal.util.Message; import org.mapstruct.ap.internal.util.Strings; import org.mapstruct.ap.internal.version.VersionInformation; +import static org.mapstruct.ap.internal.model.SupportingConstructorFragment.addAllFragmentsIn; +import static org.mapstruct.ap.internal.model.SupportingField.addAllFieldsIn; import static org.mapstruct.ap.internal.util.Collections.first; import static org.mapstruct.ap.internal.util.Collections.join; @@ -139,15 +145,26 @@ public class MapperCreationProcessor implements ModelElementProcessor methods) { - List mapperReferences = mappingContext.getMapperReferences(); + List mappingMethods = getMappingMethods( mapperConfig, methods ); - mappingMethods.addAll( mappingContext.getUsedVirtualMappings() ); + mappingMethods.addAll( mappingContext.getUsedSupportedMappings() ); mappingMethods.addAll( mappingContext.getMappingsToGenerate() ); + // handle fields + List fields = new ArrayList( mappingContext.getMapperReferences() ); + Set supportingFieldSet = new LinkedHashSet( ); + addAllFieldsIn( mappingContext.getUsedSupportedMappings(), supportingFieldSet ); + fields.addAll( supportingFieldSet ); + + // handle constructorfragments + Set constructorFragments = new LinkedHashSet(); + addAllFragmentsIn( mappingContext.getUsedSupportedMappings(), constructorFragments ); + Mapper mapper = new Mapper.Builder() .element( element ) .mappingMethods( mappingMethods ) - .mapperReferences( mapperReferences ) + .fields( fields ) + .constructorFragments( constructorFragments ) .options( options ) .versionInformation( versionInformation ) .decorator( getDecorator( element, methods, mapperConfig.implementationName(), diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java index 0866656de..9c6f51bf1 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java @@ -25,11 +25,13 @@ import javax.lang.model.util.Types; import org.mapstruct.ap.internal.conversion.ConversionProvider; import org.mapstruct.ap.internal.conversion.Conversions; +import org.mapstruct.ap.internal.model.Field; 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.VirtualMappingMethod; +import org.mapstruct.ap.internal.model.SupportingField; +import org.mapstruct.ap.internal.model.SupportingMappingMethod; import org.mapstruct.ap.internal.model.common.Assignment; import org.mapstruct.ap.internal.model.common.ConversionContext; import org.mapstruct.ap.internal.model.common.DefaultConversionContext; @@ -73,7 +75,7 @@ public class MappingResolverImpl implements MappingResolver { * Private methods which are not present in the original mapper interface and are added to map certain property * types. */ - private final Set usedVirtualMappings = new HashSet(); + private final Set usedSupportedMappings = new HashSet(); public MappingResolverImpl(FormattingMessager messager, Elements elementUtils, Types typeUtils, TypeFactory typeFactory, List sourceModel, @@ -110,8 +112,8 @@ public class MappingResolverImpl implements MappingResolver { } @Override - public Set getUsedVirtualMappings() { - return usedVirtualMappings; + public Set getUsedSupportedMappings() { + return usedSupportedMappings; } private MapperReference findMapperReference(Method method) { @@ -134,10 +136,10 @@ public class MappingResolverImpl implements MappingResolver { private final boolean savedPreferUpdateMapping; private final FormattingParameters formattingParameters; - // resolving via 2 steps creates the possibillity of wrong matches, first builtin method matches, - // second doesn't. In that case, the first builtin method should not lead to a virtual method + // resolving via 2 steps creates the possibility of wrong matches, first builtin method matches, + // second doesn't. In that case, the first builtin method should not lead to a supported method // so this set must be cleared. - private final Set virtualMethodCandidates; + private final Set supportingMethodCandidates; private ResolvingAttempt(List sourceModel, Method mappingMethod, FormattingParameters formattingParameters, SourceRHS sourceRHS, SelectionCriteria criteria) { @@ -147,7 +149,7 @@ public class MappingResolverImpl implements MappingResolver { this.formattingParameters = formattingParameters == null ? FormattingParameters.EMPTY : formattingParameters; this.sourceRHS = sourceRHS; - this.virtualMethodCandidates = new HashSet(); + this.supportingMethodCandidates = new HashSet(); this.selectionCriteria = criteria; this.savedPreferUpdateMapping = criteria.isPreferUpdateMapping(); } @@ -204,21 +206,21 @@ public class MappingResolverImpl implements MappingResolver { Assignment builtInMethod = resolveViaBuiltInMethod( sourceType, targetType ); if ( builtInMethod != null ) { builtInMethod.setAssignment( sourceRHS ); - usedVirtualMappings.addAll( virtualMethodCandidates ); + usedSupportedMappings.addAll( supportingMethodCandidates ); return builtInMethod; } // 2 step method, first: method(method(source)) referencedMethod = resolveViaMethodAndMethod( sourceType, targetType ); if ( referencedMethod != null ) { - usedVirtualMappings.addAll( virtualMethodCandidates ); + usedSupportedMappings.addAll( supportingMethodCandidates ); return referencedMethod; } // 2 step method, then: method(conversion(source)) referencedMethod = resolveViaConversionAndMethod( sourceType, targetType ); if ( referencedMethod != null ) { - usedVirtualMappings.addAll( virtualMethodCandidates ); + usedSupportedMappings.addAll( supportingMethodCandidates ); return referencedMethod; } @@ -228,7 +230,7 @@ public class MappingResolverImpl implements MappingResolver { // 2 step method, finally: conversion(method(source)) conversion = resolveViaMethodAndConversion( sourceType, targetType ); if ( conversion != null ) { - usedVirtualMappings.addAll( virtualMethodCandidates ); + usedSupportedMappings.addAll( supportingMethodCandidates ); return conversion; } @@ -252,7 +254,7 @@ public class MappingResolverImpl implements MappingResolver { // add helper methods required in conversion for ( HelperMethod helperMethod : conversionProvider.getRequiredHelperMethods( ctx ) ) { - usedVirtualMappings.add( new VirtualMappingMethod( helperMethod ) ); + usedSupportedMappings.add( new SupportingMappingMethod( helperMethod ) ); } return conversionProvider.to( ctx ); } @@ -282,7 +284,12 @@ public class MappingResolverImpl implements MappingResolver { getBestMatch( builtInMethods.getBuiltInMethods(), sourceType, targetType ); if ( matchingBuiltInMethod != null ) { - virtualMethodCandidates.add( new VirtualMappingMethod( matchingBuiltInMethod.getMethod() ) ); + + Set allUsedFields = new HashSet( mapperReferences ); + SupportingField.addAllFieldsIn( supportingMethodCandidates, allUsedFields ); + SupportingMappingMethod supportingMappingMethod = + new SupportingMappingMethod( matchingBuiltInMethod.getMethod(), allUsedFields ); + supportingMethodCandidates.add( supportingMappingMethod ); ConversionContext ctx = new DefaultConversionContext( typeFactory, messager, @@ -337,7 +344,7 @@ public class MappingResolverImpl implements MappingResolver { } else { // both should match; - virtualMethodCandidates.clear(); + supportingMethodCandidates.clear(); methodRefY = null; } } @@ -374,7 +381,7 @@ public class MappingResolverImpl implements MappingResolver { } else { // both should match - virtualMethodCandidates.clear(); + supportingMethodCandidates.clear(); methodRefY = null; } } @@ -417,7 +424,7 @@ public class MappingResolverImpl implements MappingResolver { } else { // both should match; - virtualMethodCandidates.clear(); + supportingMethodCandidates.clear(); conversionYRef = null; } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java index bb923a597..546e55f8e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Strings.java @@ -127,8 +127,8 @@ public class Strings { return string == null || string.isEmpty(); } - public static String getSaveVariableName(String name, String... existingVariableNames) { - return getSaveVariableName( name, Arrays.asList( existingVariableNames ) ); + public static String getSafeVariableName(String name, String... existingVariableNames) { + return getSafeVariableName( name, Arrays.asList( existingVariableNames ) ); } /** @@ -141,7 +141,7 @@ public class Strings { * @return a variable name based on the given original name, not conflicting with any of the given other names or * any Java keyword; starting with a lower-case letter */ - public static String getSaveVariableName(String name, Collection existingVariableNames) { + public static String getSafeVariableName(String name, Collection existingVariableNames) { name = decapitalize( sanitizeIdentifierName( name ) ); name = joinAndCamelize( extractParts( name ) ); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotatedConstructor.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotatedConstructor.ftl index bf5fa4a20..e3ddd51bd 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotatedConstructor.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotatedConstructor.ftl @@ -1,20 +1,24 @@ -<#-- + <#-- Copyright MapStruct Authors. Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> -<#if publicEmptyConstructor> -public ${name}() { -} + <#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.AnnotatedConstructor" --> + <#if noArgumentConstructor??> + <@includeModel object=noArgumentConstructor/> <#list annotations as annotation> <#nt><@includeModel object=annotation/> public ${name}(<#list mapperReferences as mapperReference><#list mapperReference.annotations as annotation><@includeModel object=annotation/> <@includeModel object=mapperReference.type/> ${mapperReference.variableName}<#if mapperReference_has_next>, ) { + <#if noArgumentConstructor?? && !noArgumentConstructor.fragments.empty>this(); <#list mapperReferences as mapperReference> this.${mapperReference.variableName} = ${mapperReference.variableName}; + <#list fragments as fragment> + <#nt><@includeModel object=fragment/> + } \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl index 66458d3b8..4391bcb97 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.Annotation" --> @<@includeModel object=type/><#if (properties?size > 0) >(<#list properties as property>${property}<#if property_has_next>, ) \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotationMapperReference.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotationMapperReference.ftl index 4391960ef..616c4900e 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotationMapperReference.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/AnnotationMapperReference.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.AnnotationMapperReference" --> <#if includeAnnotationsOnField> <#list annotations as annotation> <#nt><@includeModel object=annotation/> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl index f5d5c9dbc..2d702efe2 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.BeanMappingMethod" --> <#if overridden>@Override <#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> { <#assign targetType = resultType /> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/DecoratorConstructor.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/DecoratorConstructor.ftl index e592a1d14..9c9f397c2 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/DecoratorConstructor.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/DecoratorConstructor.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.DecoratorConstructor" --> public ${name}() { this( new ${delegateName}() ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/DefaultMapperReference.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/DefaultMapperReference.ftl index 3adf75061..e4eeddb4b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/DefaultMapperReference.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/DefaultMapperReference.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.DefaultMapperReference" --> private final <@includeModel object=type/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=type/>.class );<#else>new <@includeModel object=type/>(); \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/DelegatingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/DelegatingMethod.ftl index 5c467772e..f27c242de 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/DelegatingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/DelegatingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.DelegatingMethod" --> @Override public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) <@throws/> { <#if returnType.name != "void">return delegate.${name}( <#list parameters as param>${param.name}<#if param_has_next>, ); diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/EnumMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/EnumMappingMethod.ftl index 0caa97fa2..3d0c9e074 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/EnumMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/EnumMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.EnumMappingMethod" --> @Override public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) { <#list beforeMappingReferencesWithoutMappingTarget as callback> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/Field.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/Field.ftl index ddaa403ad..af4cb01f6 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/Field.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/Field.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.Field" --> private final <@includeModel object=type/> ${variableName}; \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl index 16942480f..3bdae0582 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.GeneratedType" --> <#if hasPackageName()> package ${packageName}; 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 02af54254..86634ef13 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 @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.IterableCreation" --> <@compress single_line=true> <#if factoryMethod??> <@includeModel object=factoryMethod targetType=resultType/> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableMappingMethod.ftl index 10a4fd367..3af75079b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/IterableMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.IterableMappingMethod" --> <#if overridden>@Override <#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> { <#list beforeMappingReferencesWithoutMappingTarget as callback> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.ftl index 3eef87a30..310eda210 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/LifecycleCallbackMethodReference.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.LifecycleCallbackMethodReference" --> <@compress single_line=true> <#if hasReturnType()> <@includeModel object=methodResultType /> ${targetVariableName} = diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/MapMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/MapMappingMethod.ftl index 471c3dbe9..1c6e6bad3 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/MapMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/MapMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.MapMappingMethod" --> <#if overridden>@Override <#lt>${accessibility.keyword} <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> { <#list beforeMappingReferencesWithoutMappingTarget as callback> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/MethodReference.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/MethodReference.ftl index 4dc538e29..30a830bc5 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/MethodReference.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/MethodReference.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.MethodReference" --> <@compress single_line=true> <#-- method is either internal to the mapper class, or external (via uses) declaringMapper!=null --> <#if declaringMapper??> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.ftl index 97f8c0ea2..ae4f65a81 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/NestedPropertyMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.NestedPropertyMappingMethod" --> <#lt>private <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> { if ( ${sourceParameter.name} == null ) { return ${returnType.null}; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/NoArgumentConstructor.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/NoArgumentConstructor.ftl new file mode 100644 index 000000000..e0ae1454b --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/NoArgumentConstructor.ftl @@ -0,0 +1,13 @@ + <#-- + + Copyright MapStruct Authors. + + Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + +--> + <#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.NoArgumentConstructor" --> +public ${name}() { + <#list fragments as fragment> + <#nt><@includeModel object=fragment/> + + } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/PropertyMapping.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/PropertyMapping.ftl index 4f0632ed5..20e6f1734 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/PropertyMapping.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/PropertyMapping.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.PropertyMapping" --> <@includeModel object=assignment targetBeanName=ext.targetBeanName existingInstanceMapping=ext.existingInstanceMapping diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/ServicesEntry.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/ServicesEntry.ftl index 9d61e9310..facbb5f8b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/ServicesEntry.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/ServicesEntry.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.ServicesEntry" --> ${implementationPackage}.${implementationName} diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/StreamMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/StreamMappingMethod.ftl index bb1ad1628..3eab47794 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/StreamMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/StreamMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.StreamMappingMethod" --> <#if overridden>@Override <#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, )<@throws/> { <#--TODO does it even make sense to do a callback if the result is a Stream, as they are immutable--> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/TypeConversion.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/TypeConversion.ftl index 1dc7ec63b..370fe7c97 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/TypeConversion.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/TypeConversion.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.TypeConversion" --> <@compress single_line=true> ${openExpression}<@_assignment/>${closeExpression} <#macro _assignment> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/ValueMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/ValueMappingMethod.ftl index bc3a63cde..5fa1c0a11 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/ValueMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/ValueMappingMethod.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.ValueMappingMethod" --> <#if overridden>@Override <#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) { <#list beforeMappingReferencesWithoutMappingTarget as callback> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/AdderWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/AdderWrapper.ftl index 6e124ef6f..c75942de9 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/AdderWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/AdderWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.AdderWrapper" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.handleExceptions> if ( ${sourceReference} != null ) { diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ArrayCopyWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ArrayCopyWrapper.ftl index 3dd22a2cb..edb0d3cad 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ArrayCopyWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ArrayCopyWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.ArrayCopyWrapper" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.handleExceptions> <@lib.sourceLocalVarAssignment/> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/EnumConstantWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/EnumConstantWrapper.ftl index 94a8682a0..7364f7922 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/EnumConstantWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/EnumConstantWrapper.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.EnumConstantWrapper" --> ${ext.targetType.name}.${assignment} \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ExistingInstanceSetterWrapperForCollectionsAndMaps.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ExistingInstanceSetterWrapperForCollectionsAndMaps.ftl index ab254e4b5..17eb9de51 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ExistingInstanceSetterWrapperForCollectionsAndMaps.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/ExistingInstanceSetterWrapperForCollectionsAndMaps.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.ExistingInstanceSetterWrapperForCollectionsAndMaps" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.sourceLocalVarAssignment/> <@lib.handleExceptions> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/GetterWrapperForCollectionsAndMaps.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/GetterWrapperForCollectionsAndMaps.ftl index c4ab06e7a..4ef55f3b4 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/GetterWrapperForCollectionsAndMaps.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/GetterWrapperForCollectionsAndMaps.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.GetterWrapperForCollectionsAndMaps" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.sourceLocalVarAssignment/> if ( ${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWriteAccesing /> != null ) { diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/Java8FunctionWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/Java8FunctionWrapper.ftl index 927372a93..2795dac7b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/Java8FunctionWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/Java8FunctionWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.Java8FunctionWrapper" --> <#assign sourceVarName><#if assignment.sourceLocalVarName?? >${assignment.sourceLocalVarName}<#else>${assignment.sourceReference} <#if (thrownTypes?size == 0) > <#compress> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/LocalVarWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/LocalVarWrapper.ftl index 52d6efb67..53e987e47 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/LocalVarWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/LocalVarWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.LocalVarWrapper" --> <#if (thrownTypes?size == 0) > <#if !ext.isTargetDefined?? ><@includeModel object=ext.targetType/> ${ext.targetWriteAccessorName} = <@_assignment/>; <#else> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl index 201e7bc70..613e8f6ab 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.SetterWrapper" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.handleExceptions> <@lib.sourceLocalVarAssignment/> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMaps.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMaps.ftl index 726c73cb3..122c76bc6 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMaps.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMaps.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMaps" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.sourceLocalVarAssignment/> <@lib.handleExceptions> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMapsWithNullCheck.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMapsWithNullCheck.ftl index 3e0395c9e..6e38d7fbf 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMapsWithNullCheck.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapperForCollectionsAndMapsWithNullCheck.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.SetterWrapperForCollectionsAndMapsWithNullCheck" --> <#import "../macro/CommonMacros.ftl" as lib> <@lib.sourceLocalVarAssignment/> <@lib.handleExceptions> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.ftl index 305cb23c9..833120b01 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.assignment.UpdateWrapper" --> <#import '../macro/CommonMacros.ftl' as lib > <@lib.handleExceptions> <#if includeSourceNullCheck> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Parameter.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Parameter.ftl index 821712188..41afd0a42 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Parameter.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Parameter.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.Parameter" --> <@includeModel object=type asVarArgs=varArgs/> ${name} \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/SourceRHS.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/SourceRHS.ftl index 9594a665b..7cab67b52 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/SourceRHS.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/SourceRHS.ftl @@ -5,4 +5,5 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.SourceRHS" --> <#if sourceLocalVarName??>${sourceLocalVarName}<#else>${sourceReference} \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Type.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Type.ftl index 1cb4fa629..d6b2d6850 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Type.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/common/Type.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.Type" --> <@compress single_line=true> <#if wildCardExtendsBound> ? extends <@includeModel object=typeBound /> diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl index c462710bd..38cf1ad85 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl @@ -5,17 +5,13 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("Calendar")/> cal ) { if ( cal == null ) { return null; } - try { - <@includeModel object=findType("GregorianCalendar")/> gcal = new <@includeModel object=findType("GregorianCalendar")/>( cal.getTimeZone() ); - gcal.setTimeInMillis( cal.getTimeInMillis() ); - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( gcal ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + <@includeModel object=findType("GregorianCalendar")/> gcal = new <@includeModel object=findType("GregorianCalendar")/>( cal.getTimeZone() ); + gcal.setTimeInMillis( cal.getTimeInMillis() ); + return ${supportingField.variableName}.newXMLGregorianCalendar( gcal ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl index f203fce79..e97a1fc69 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("ZonedDateTime")/> ${name}(<@includeModel object=findType("Calendar")/> cal) { if ( cal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl index fa7f5985b..7488bc988 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl @@ -5,17 +5,13 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("Date")/> date ) { if ( date == null ) { return null; } - try { - <@includeModel object=findType("GregorianCalendar")/> c = new <@includeModel object=findType("GregorianCalendar")/>(); - c.setTime( date ); - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( c ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + <@includeModel object=findType("GregorianCalendar")/> c = new <@includeModel object=findType("GregorianCalendar")/>(); + c.setTime( date ); + return ${supportingField.variableName}.newXMLGregorianCalendar( c ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/FinalField.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/FinalField.ftl new file mode 100644 index 000000000..3d04bc3ae --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/FinalField.ftl @@ -0,0 +1,9 @@ +<#-- + + Copyright MapStruct Authors. + + Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + +--> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingField" --> +private final <@includeModel object=type/> ${variableName}; \ No newline at end of file diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl index 913e8d4d3..078be41af 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private T ${name}( <@includeModel object=findType("JAXBElement") raw=true/> element ) { if ( element == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl index 63f12392c..aceec1f83 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl @@ -5,23 +5,18 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("DateTime")/> dt ) { if ( dt == null ) { return null; } - - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( - dt.getYear(), - dt.getMonthOfYear(), - dt.getDayOfMonth(), - dt.getHourOfDay(), - dt.getMinuteOfHour(), - dt.getSecondOfMinute(), - dt.getMillisOfSecond(), - dt.getZone().getOffset( null ) / 60000 ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendar( + dt.getYear(), + dt.getMonthOfYear(), + dt.getDayOfMonth(), + dt.getHourOfDay(), + dt.getMinuteOfHour(), + dt.getSecondOfMinute(), + dt.getMillisOfSecond(), + dt.getZone().getOffset( null ) / 60000 ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl index 7fa0f17e0..19a9ce0a2 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl @@ -5,23 +5,19 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalDateTime")/> dt ) { if ( dt == null ) { return null; } - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( - dt.getYear(), - dt.getMonthOfYear(), - dt.getDayOfMonth(), - dt.getHourOfDay(), - dt.getMinuteOfHour(), - dt.getSecondOfMinute(), - dt.getMillisOfSecond(), - <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendar( + dt.getYear(), + dt.getMonthOfYear(), + dt.getDayOfMonth(), + dt.getHourOfDay(), + dt.getMinuteOfHour(), + dt.getSecondOfMinute(), + dt.getMillisOfSecond(), + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl index 5a41f31b8..4b9b855fb 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl @@ -5,19 +5,15 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalDate")/> dt ) { if ( dt == null ) { return null; } - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarDate( - dt.getYear(), - dt.getMonthOfYear(), - dt.getDayOfMonth(), - <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendarDate( + dt.getYear(), + dt.getMonthOfYear(), + dt.getDayOfMonth(), + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl index cee4edd7b..43a8e67a9 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl @@ -5,20 +5,17 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalTime")/> dt ) { if ( dt == null ) { return null; } - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarTime( - dt.getHourOfDay(), - dt.getMinuteOfHour(), - dt.getSecondOfMinute(), - dt.getMillisOfSecond(), - <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendarTime( + dt.getHourOfDay(), + dt.getMinuteOfHour(), + dt.getSecondOfMinute(), + dt.getMillisOfSecond(), + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); + } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl index 93e10b1ee..469bbf750 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl @@ -5,20 +5,15 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> -private static <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("java.time.LocalDate")/> localDate ) { +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("java.time.LocalDate")/> localDate ) { if ( localDate == null ) { return null; } - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarDate( - localDate.getYear(), - localDate.getMonthValue(), - localDate.getDayOfMonth(), - <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED - ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendarDate( + localDate.getYear(), + localDate.getMonthValue(), + localDate.getDayOfMonth(), + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.ftl new file mode 100644 index 000000000..1dcbbe34b --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/NewDatatypeFactoryConstructorFragment.ftl @@ -0,0 +1,14 @@ +<#-- + + Copyright MapStruct Authors. + + Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + +--> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingConstructorFragment" --> +try { + ${definingMethod.supportingField.variableName} = <@includeModel object=definingMethod.supportingField.type/>.newInstance(); +} +catch ( <@includeModel object=definingMethod.findType("DatatypeConfigurationException")/> ex ) { + throw new RuntimeException( ex ); +} diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl index 660526ac7..00c178b14 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( String date, String dateFormat ) { if ( date == null ) { return null; @@ -15,15 +16,12 @@ private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( String <@includeModel object=findType("DateFormat")/> df = new <@includeModel object=findType("SimpleDateFormat")/>( dateFormat ); <@includeModel object=findType("GregorianCalendar")/> c = new <@includeModel object=findType("GregorianCalendar")/>(); c.setTime( df.parse( date ) ); - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( c ); + return ${supportingField.variableName}.newXMLGregorianCalendar( c ); } else { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( date ); + return ${supportingField.variableName}.newXMLGregorianCalendar( date ); } } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } catch ( <@includeModel object=findType("ParseException")/> ex ) { throw new RuntimeException( ex ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl index a8b3ba2bd..cfc4ed2ad 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("Calendar")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl index bd3841c22..f4ebaab9c 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("java.util.Date")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl index 555d6b273..15fa82bc5 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("DateTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl index c5adac02a..3067f5dd6 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("org.joda.time.LocalDate")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl index ae640c6b8..6ace97519 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("org.joda.time.LocalDateTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl index c8115f29c..392401b3f 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("org.joda.time.LocalTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl index a526c6b99..81141a41f 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private static <@includeModel object=findType("java.time.LocalDate")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl index f75ce8b48..99c0237ee 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private String ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal, String dateFormat ) { if ( xcal == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl index 3c57b7923..6a96241e3 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl @@ -5,6 +5,7 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("Calendar")/> ${name}(<@includeModel object=findType("ZonedDateTime")/> dateTime) { if ( dateTime == null ) { return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.ftl index a49558f7d..b187daa95 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToXmlGregorianCalendar.ftl @@ -5,15 +5,11 @@ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 --> +<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("ZonedDateTime")/> zdt ) { if ( zdt == null ) { return null; } - try { - return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( <@includeModel object=findType("GregorianCalendar")/>.from( zdt ) ); - } - catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { - throw new RuntimeException( ex ); - } + return ${supportingField.variableName}.newXMLGregorianCalendar( <@includeModel object=findType("GregorianCalendar")/>.from( zdt ) ); } diff --git a/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java b/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java index 575288ec9..b21db49a3 100644 --- a/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java +++ b/processor/src/test/java/org/mapstruct/ap/internal/util/StringsTest.java @@ -76,27 +76,27 @@ public class StringsTest { @Test public void testGetSaveVariableNameWithArrayExistingVariables() throws Exception { - assertThat( Strings.getSaveVariableName( "int[]" ) ).isEqualTo( "intArray" ); - assertThat( Strings.getSaveVariableName( "Extends" ) ).isEqualTo( "extends1" ); - assertThat( Strings.getSaveVariableName( "class" ) ).isEqualTo( "class1" ); - assertThat( Strings.getSaveVariableName( "Class" ) ).isEqualTo( "class1" ); - assertThat( Strings.getSaveVariableName( "Case" ) ).isEqualTo( "case1" ); - assertThat( Strings.getSaveVariableName( "Synchronized" ) ).isEqualTo( "synchronized1" ); - assertThat( Strings.getSaveVariableName( "prop", "prop", "prop_" ) ).isEqualTo( "prop1" ); + assertThat( Strings.getSafeVariableName( "int[]" ) ).isEqualTo( "intArray" ); + assertThat( Strings.getSafeVariableName( "Extends" ) ).isEqualTo( "extends1" ); + assertThat( Strings.getSafeVariableName( "class" ) ).isEqualTo( "class1" ); + assertThat( Strings.getSafeVariableName( "Class" ) ).isEqualTo( "class1" ); + assertThat( Strings.getSafeVariableName( "Case" ) ).isEqualTo( "case1" ); + assertThat( Strings.getSafeVariableName( "Synchronized" ) ).isEqualTo( "synchronized1" ); + assertThat( Strings.getSafeVariableName( "prop", "prop", "prop_" ) ).isEqualTo( "prop1" ); } @Test public void testGetSaveVariableNameVariablesEndingOnNumberVariables() throws Exception { - assertThat( Strings.getSaveVariableName( "prop1", "prop1" ) ).isEqualTo( "prop1_1" ); - assertThat( Strings.getSaveVariableName( "prop1", "prop1", "prop1_1" ) ).isEqualTo( "prop1_2" ); + assertThat( Strings.getSafeVariableName( "prop1", "prop1" ) ).isEqualTo( "prop1_1" ); + assertThat( Strings.getSafeVariableName( "prop1", "prop1", "prop1_1" ) ).isEqualTo( "prop1_2" ); } @Test public void testGetSaveVariableNameWithCollection() throws Exception { - assertThat( Strings.getSaveVariableName( "int[]", new ArrayList() ) ).isEqualTo( "intArray" ); - assertThat( Strings.getSaveVariableName( "Extends", new ArrayList() ) ).isEqualTo( "extends1" ); - assertThat( Strings.getSaveVariableName( "prop", Arrays.asList( "prop", "prop1" ) ) ).isEqualTo( "prop2" ); - assertThat( Strings.getSaveVariableName( "prop.font", Arrays.asList( "propFont", "propFont_" ) ) ) + assertThat( Strings.getSafeVariableName( "int[]", new ArrayList() ) ).isEqualTo( "intArray" ); + assertThat( Strings.getSafeVariableName( "Extends", new ArrayList() ) ).isEqualTo( "extends1" ); + assertThat( Strings.getSafeVariableName( "prop", Arrays.asList( "prop", "prop1" ) ) ).isEqualTo( "prop2" ); + assertThat( Strings.getSafeVariableName( "prop.font", Arrays.asList( "propFont", "propFont_" ) ) ) .isEqualTo( "propFont1" ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/DatatypeFactoryTest.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/DatatypeFactoryTest.java new file mode 100644 index 000000000..fe52452c7 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/DatatypeFactoryTest.java @@ -0,0 +1,69 @@ +/* + * 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.builtin; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.test.builtin.bean.CalendarProperty; +import org.mapstruct.ap.test.builtin.bean.DatatypeFactory; +import org.mapstruct.ap.test.builtin.bean.DateProperty; +import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarFactorizedProperty; +import org.mapstruct.ap.test.builtin.mapper.ToXmlGregCalMapper; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@WithClasses( { + DatatypeFactory.class, + XmlGregorianCalendarFactorizedProperty.class, + ToXmlGregCalMapper.class, + CalendarProperty.class, + DateProperty.class + +} ) +@RunWith(AnnotationProcessorTestRunner.class) +public class DatatypeFactoryTest { + + @Test + public void testNoConflictsWithOwnDatatypeFactory() throws ParseException { + + DateProperty source1 = new DateProperty(); + source1.setProp( createDate( "31-08-1982 10:20:56" ) ); + + CalendarProperty source2 = new CalendarProperty(); + source2.setProp( createCalendar( "02.03.1999" ) ); + + XmlGregorianCalendarFactorizedProperty target1 = ToXmlGregCalMapper.INSTANCE.map( source1 ); + assertThat( target1 ).isNotNull(); + assertThat( target1.getProp() ).isNotNull(); + assertThat( target1.getProp().toString() ).isEqualTo( "1982-08-31T10:20:56.000+02:00" ); + + XmlGregorianCalendarFactorizedProperty target2 = ToXmlGregCalMapper.INSTANCE.map( source2 ); + assertThat( target2 ).isNotNull(); + assertThat( target2.getProp() ).isNotNull(); + assertThat( target2.getProp().toString() ).isEqualTo( "1999-03-02T00:00:00.000+01:00" ); + + } + + private Date createDate(String date) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" ); + return sdf.parse( date ); + } + + private Calendar createCalendar(String cal) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" ); + GregorianCalendar gcal = new GregorianCalendar(); + gcal.setTime( sdf.parse( cal ) ); + return gcal; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/DatatypeFactory.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/DatatypeFactory.java new file mode 100644 index 000000000..648905677 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/DatatypeFactory.java @@ -0,0 +1,13 @@ +/* + * 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.builtin.bean; + +public class DatatypeFactory { + + public XmlGregorianCalendarFactorizedProperty create() { + return new XmlGregorianCalendarFactorizedProperty( "test" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/XmlGregorianCalendarFactorizedProperty.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/XmlGregorianCalendarFactorizedProperty.java new file mode 100644 index 000000000..afbea20a7 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/bean/XmlGregorianCalendarFactorizedProperty.java @@ -0,0 +1,26 @@ +/* + * 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.builtin.bean; + +import javax.xml.datatype.XMLGregorianCalendar; + +public class XmlGregorianCalendarFactorizedProperty { + + private XMLGregorianCalendar prop; + private String control; + + XmlGregorianCalendarFactorizedProperty( String in ) { + this.control = in; + } + + public XMLGregorianCalendar getProp() { + return prop; + } + + public void setProp( XMLGregorianCalendar prop ) { + this.prop = prop; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/ToXmlGregCalMapper.java b/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/ToXmlGregCalMapper.java new file mode 100644 index 000000000..9ab5dc366 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/ToXmlGregCalMapper.java @@ -0,0 +1,24 @@ +/* + * 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.builtin.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.builtin.bean.CalendarProperty; +import org.mapstruct.ap.test.builtin.bean.DatatypeFactory; +import org.mapstruct.ap.test.builtin.bean.DateProperty; +import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarFactorizedProperty; +import org.mapstruct.factory.Mappers; + +@Mapper( uses = DatatypeFactory.class ) +public interface ToXmlGregCalMapper { + + ToXmlGregCalMapper INSTANCE = Mappers.getMapper( ToXmlGregCalMapper.class ); + + XmlGregorianCalendarFactorizedProperty map(CalendarProperty source); + + XmlGregorianCalendarFactorizedProperty map(DateProperty source); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordDto.java b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordDto.java new file mode 100644 index 000000000..9160b7c76 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordDto.java @@ -0,0 +1,33 @@ +/* + * 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.injectionstrategy.shared; + +import javax.xml.datatype.XMLGregorianCalendar; + +/** + * @author Sjaak Derksen + */ +public class CustomerRecordDto { + + private XMLGregorianCalendar registrationDate; + private CustomerDto customer; + + public XMLGregorianCalendar getRegistrationDate() { + return registrationDate; + } + + public void setRegistrationDate(XMLGregorianCalendar registrationDate) { + this.registrationDate = registrationDate; + } + + public CustomerDto getCustomer() { + return customer; + } + + public void setCustomer(CustomerDto customer) { + this.customer = customer; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordEntity.java b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordEntity.java new file mode 100644 index 000000000..0f115fc32 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/shared/CustomerRecordEntity.java @@ -0,0 +1,33 @@ +/* + * 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.injectionstrategy.shared; + +import java.util.Date; + +/** + * @author Sjaak Derksen + */ +public class CustomerRecordEntity { + + private Date registrationDate; + private CustomerEntity customer; + + public Date getRegistrationDate() { + return registrationDate; + } + + public void setRegistrationDate(Date registrationDate) { + this.registrationDate = registrationDate; + } + + public CustomerEntity getCustomer() { + return customer; + } + + public void setCustomer(CustomerEntity customer) { + this.customer = customer; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/CustomerRecordSpringConstructorMapper.java b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/CustomerRecordSpringConstructorMapper.java new file mode 100644 index 000000000..a7f9e8a8b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/CustomerRecordSpringConstructorMapper.java @@ -0,0 +1,23 @@ +/* + * 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.injectionstrategy.spring.constructor; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.injectionstrategy.shared.CustomerRecordDto; +import org.mapstruct.ap.test.injectionstrategy.shared.CustomerRecordEntity; + +/** + * @author Kevin Grüneberg + */ +@Mapper(componentModel = "spring", + uses = { CustomerSpringConstructorMapper.class, GenderSpringConstructorMapper.class }, + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + disableSubMappingMethodsGeneration = true) +public interface CustomerRecordSpringConstructorMapper { + + CustomerRecordDto asTarget(CustomerRecordEntity customerRecordEntity); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/SpringConstructorMapperTest.java b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/SpringConstructorMapperTest.java index 41335e1e6..267ad8dd3 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/SpringConstructorMapperTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/injectionstrategy/spring/constructor/SpringConstructorMapperTest.java @@ -5,13 +5,22 @@ */ package org.mapstruct.ap.test.injectionstrategy.spring.constructor; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto; import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity; +import org.mapstruct.ap.test.injectionstrategy.shared.CustomerRecordDto; +import org.mapstruct.ap.test.injectionstrategy.shared.CustomerRecordEntity; import org.mapstruct.ap.test.injectionstrategy.shared.Gender; import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto; import org.mapstruct.ap.testutil.IssueKey; @@ -33,10 +42,13 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Kevin Grüneberg */ @WithClasses( { + CustomerRecordDto.class, + CustomerRecordEntity.class, CustomerDto.class, CustomerEntity.class, Gender.class, GenderDto.class, + CustomerRecordSpringConstructorMapper.class, CustomerSpringConstructorMapper.class, GenderSpringConstructorMapper.class, ConstructorSpringConfig.class @@ -47,13 +59,26 @@ import static org.assertj.core.api.Assertions.assertThat; @Configuration public class SpringConstructorMapperTest { + private static TimeZone originalTimeZone; + @Rule public final GeneratedSource generatedSource = new GeneratedSource(); @Autowired - private CustomerSpringConstructorMapper customerMapper; + private CustomerRecordSpringConstructorMapper customerRecordMapper; private ConfigurableApplicationContext context; + @BeforeClass + public static void setDefaultTimeZoneToCet() { + originalTimeZone = TimeZone.getDefault(); + TimeZone.setDefault( TimeZone.getTimeZone( "Europe/Berlin" ) ); + } + + @AfterClass + public static void restoreOriginalTimeZone() { + TimeZone.setDefault( originalTimeZone ); + } + @Before public void springUp() { context = new AnnotationConfigApplicationContext( getClass() ); @@ -68,19 +93,25 @@ public class SpringConstructorMapperTest { } @Test - public void shouldConvertToTarget() { + public void shouldConvertToTarget() throws Exception { // given CustomerEntity customerEntity = new CustomerEntity(); customerEntity.setName( "Samuel" ); customerEntity.setGender( Gender.MALE ); + CustomerRecordEntity customerRecordEntity = new CustomerRecordEntity(); + customerRecordEntity.setCustomer( customerEntity ); + customerRecordEntity.setRegistrationDate( createDate( "31-08-1982 10:20:56" ) ); // when - CustomerDto customerDto = customerMapper.asTarget( customerEntity ); + CustomerRecordDto customerRecordDto = customerRecordMapper.asTarget( customerRecordEntity ); // then - assertThat( customerDto ).isNotNull(); - assertThat( customerDto.getName() ).isEqualTo( "Samuel" ); - assertThat( customerDto.getGender() ).isEqualTo( GenderDto.M ); + assertThat( customerRecordDto ).isNotNull(); + assertThat( customerRecordDto.getCustomer() ).isNotNull(); + assertThat( customerRecordDto.getCustomer().getName() ).isEqualTo( "Samuel" ); + assertThat( customerRecordDto.getCustomer().getGender() ).isEqualTo( GenderDto.M ); + assertThat( customerRecordDto.getRegistrationDate() ).isNotNull(); + assertThat( customerRecordDto.getRegistrationDate().toString() ).isEqualTo( "1982-08-31T10:20:56.000+02:00" ); } @Test @@ -91,4 +122,10 @@ public class SpringConstructorMapperTest { .contains( "@Autowired" + lineSeparator() + " public CustomerSpringConstructorMapperImpl(GenderSpringConstructorMapper" ); } + + private Date createDate(String date) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" ); + return sdf.parse( date ); + } + }