You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mapstruct#73 Add support for using constructor arguments when instantiating mapping targets
By default the constructor argument names are used to extract the target properties.
If a constructor is annotated with an annotation named `@ConstructorProperties` (from any package) then it would be used to extract the target properties.
If a mapping target has a parameterless empty constructor it would be used to instantiate the target.
When there are multiple constructors then an annotation named `@Default` (from any package) can be used to mark a constructor that should be used by default when instantiating the target.
Supports mapping into Java 14 Records and Kotlin data classes out of the box
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc
+77
Original file line number
Diff line number
Diff line change
@@ -528,3 +528,80 @@ Otherwise, you would need to write a custom `BuilderProvider`
528
528
====
529
529
In case you want to disable using builders then you can use the `NoOpBuilderProvider` by creating a `org.mapstruct.ap.spi.BuilderProvider` file in the `META-INF/services` directory with `org.mapstruct.ap.spi.NoOpBuilderProvider` as it's content.
530
530
====
531
+
532
+
[[mapping-with-constructors]]
533
+
=== Using Constructors
534
+
535
+
MapStruct supports using constructors for mapping target types.
536
+
When doing a mapping MapStruct checks if there is a builder for the type being mapped.
537
+
If there is no builder, then MapStruct looks for a single accessible constructor.
538
+
When there are multiple constructors then the following is done to pick the one which should be used:
539
+
540
+
* If a parameterless constructor exists then it would be used to construct the object, and the other constructors will be ignored
541
+
* If there are multiple constructors then the one annotated with annotation named `@Default` (from any package) will be used
542
+
543
+
When using a constructor then the names of the parameters of the constructor will be used and matched to the target properties.
544
+
When the constructor has an annotation named `@ConstructorProperties` (from any package) then this annotation will be used to get the names of the parameters.
545
+
546
+
[NOTE]
547
+
====
548
+
When an object factory method or a method annotated with `@ObjectFactory` exists, it will take precedence over any constructor defined in the target.
549
+
The target object constructor will not be used in that case.
550
+
====
551
+
552
+
553
+
.Person with constructor parameters
554
+
====
555
+
[source, java, linenums]
556
+
[subs="verbatim,attributes"]
557
+
----
558
+
public class Person {
559
+
560
+
private final String name;
561
+
private final String surname;
562
+
563
+
public Person(String name, String surname) {
564
+
this.name = name;
565
+
this.surname = surname;
566
+
}
567
+
}
568
+
----
569
+
====
570
+
571
+
.Person With Constructor Mapper definition
572
+
====
573
+
[source, java, linenums]
574
+
[subs="verbatim,attributes"]
575
+
----
576
+
public interface PersonMapper {
577
+
578
+
Person map(PersonDto dto);
579
+
}
580
+
----
581
+
====
582
+
583
+
.Generated mapper with constructor
584
+
====
585
+
[source, java, linenums]
586
+
[subs="verbatim,attributes"]
587
+
----
588
+
// GENERATED CODE
589
+
public class PersonMapperImpl implements PersonMapper {
Copy file name to clipboardExpand all lines: integrationtest/src/test/java/org/mapstruct/itest/testutil/extension/ProcessorTestTemplateInvocationContextProvider.java
+2-1
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,8 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
0 commit comments