Compare commits

...

418 Commits

Author SHA1 Message Date
Yang Tang
e4bc1cdf1e
#3884 Ensure NullValuePropertyMappingStrategy.SET_TO_DEFAULT initializes empty collection/map when target is null
Signed-off-by: TangYang <tangyang9464@163.com>
2025-06-15 08:29:45 +02:00
Filip Hrisafov
c90c93630e #3886: Records do not have property write accessors (apart from the record components) 2025-06-14 23:40:02 +02:00
Filip Hrisafov
f4d1818171 Fix issue key in Issue3807Test 2025-06-14 21:33:26 +02:00
dependabot[bot]
d68819a233 Bump org.springframework:spring-context from 6.2.2 to 6.2.7 in /parent
Bumps [org.springframework:spring-context](https://github.com/spring-projects/spring-framework) from 6.2.2 to 6.2.7.
- [Release notes](https://github.com/spring-projects/spring-framework/releases)
- [Commits](https://github.com/spring-projects/spring-framework/compare/v6.2.2...v6.2.7)

---
updated-dependencies:
- dependency-name: org.springframework:spring-context
  dependency-version: 6.2.7
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-06-14 17:02:12 +02:00
Filip Hrisafov
46ce011e4b
Refactor options and add an enum (#3877) 2025-06-01 07:52:18 +02:00
Filip Hrisafov
9847eaf195
#3876: Move Windows and Mac OS builds outside of the main workflow 2025-05-31 18:14:13 +02:00
Yang Tang
ce84c81de2
#3659: Support @AnnotatedWith on decorators
Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-31 17:52:05 +02:00
Yang Tang
bff88297e3
#3807: Properly recognize the type of public generic fields
Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-31 13:29:39 +02:00
Yang Tang
8fc97f5f62
#3806: Properly apply NullValuePropertyMappingStrategy.IGNORE for collections / maps without setters
Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-31 11:13:50 +02:00
Yang Tang
5464c3cff8
#3711: Support generic @Context
Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-31 11:10:24 +02:00
Aleksey Ivashin
6b6600c370
#1958: Add support for ignoring multiple target properties at once 2025-05-25 17:05:18 +02:00
Yang Tang
0badba7003
#3849: Resolve duplicate invocation of overloaded lifecycle methods with inheritance
Add compiler option `mapstruct.disableLifecycleOverloadDeduplicateSelector` to disable the deduplication if needed.

Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-25 16:35:38 +02:00
Yang Tang
3a5c70224d
#3809 Fix conditional mapping with @TargetPropertyName failing for nested update mappings
Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-25 15:40:43 +02:00
Şamil Can
42c87d1da9
#3848: Mark String to number as lossy conversion 2025-05-25 15:22:47 +02:00
Dennis Melzer
05f27e96e2
#3852 Initialize Optionals with empty instead of null 2025-05-25 14:58:23 +02:00
Yang Tang
6e6fd01a2e
#3821: Add support for custom exception for subclass exhaustive strategy for @SubclassMapping
---------

Signed-off-by: TangYang <tangyang9464@163.com>
2025-05-17 18:40:51 +02:00
roelmang
fce73aee6a #3729 Support for using inner class Builder without using static factory method 2025-05-11 21:58:47 +02:00
Filip Hrisafov
2fb5776350 Add release notes for next version 2025-05-11 20:07:53 +02:00
zral
602e29083f
#1140 Add warning when target has no properties 2025-05-11 19:59:59 +02:00
Cause Chung
2c84d04463 #3240 Add Support for Java21 SequencedSet and SequencedMap 2025-05-11 17:33:35 +02:00
dependabot[bot]
668eeb5de1 Bump com.google.protobuf:protobuf-java from 3.21.7 to 3.25.5 in /parent
Bumps [com.google.protobuf:protobuf-java](https://github.com/protocolbuffers/protobuf) from 3.21.7 to 3.25.5.
- [Release notes](https://github.com/protocolbuffers/protobuf/releases)
- [Changelog](https://github.com/protocolbuffers/protobuf/blob/main/protobuf_release.bzl)
- [Commits](https://github.com/protocolbuffers/protobuf/compare/v3.21.7...v3.25.5)

---
updated-dependencies:
- dependency-name: com.google.protobuf:protobuf-java
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-05-11 15:57:52 +02:00
fml2
d5f825193c
#3815: chore(docs): Improved wording about @Condition usage 2025-05-10 14:12:20 +02:00
Zegveld
39551242d7
#3786: Improve error message when mapping non-iterable to array 2025-01-24 14:41:35 +01:00
Filip Hrisafov
c08ba4ca7e
Update setup-java and checkout actions to v4 (#3804) 2025-01-22 08:51:49 +01:00
Filip Hrisafov
57d4f88a48
Java EA GitHub Actions improvements (#3803)
* Use Java 17 for building MapStruct (still Java 8 compatible)
* Upgrade to Spring 6 for tests
* Adjust excludes and min tests version for some integration tests
2025-01-20 07:56:19 +01:00
Filip Hrisafov
e0a7d3d0e6 Use latest Maven Wrapper 2025-01-19 15:05:59 +01:00
Tran Ngoc Nhan
8f96291911
Fix documentation typo and code polish (#3787) 2025-01-18 13:04:42 +01:00
Daniel Hammer
4812d2b030
Align README with v1.6.3 release (#3784) 2024-12-07 14:57:13 +01:00
jinhyogyeom
f98a742f98
Standardize Class Names to PascalCase in tests (#3773) 2024-12-01 11:22:51 +01:00
Tran Ngoc Nhan
084cf3abc1
Fix javadoc typos (#3780) 2024-11-29 09:33:41 +01:00
dudxor4587
f3d2b2e65b
Delete unnecessary conditions and modify return statement (#3772) 2024-11-21 23:14:16 +01:00
Roman Obolonskyi
737af6b50a
#3628 Add support for locale parameter for numberFormat and dateFormat 2024-11-17 16:46:59 +01:00
Minji Kim
bee983cd3c
Fix typos in comments (#3769) 2024-11-15 22:13:36 +01:00
hsjni0110
8de18e5a65
fix typos in method and variable names (#3766) 2024-11-13 21:35:20 +01:00
cussle
0df3f6af95
docs & refactor: fix typos and improve readability in multiple classes (#3767)
- AnnotatedConstructor: Fixed a variable name typo (noArgConstructorToInBecluded → noArgConstructorToBeIncluded).
- AbstractBaseBuilder: Improved Javadoc by fixing typos and clarifying wording.
- SourceRhsSelector: Corrected a typo in the class-level Javadoc.
- InheritanceSelector: Enhanced readability by fixing typos and refining comments.
2024-11-13 21:22:57 +01:00
GitHub Action
5bf2b152af Next version 1.7.0-SNAPSHOT 2024-11-09 11:40:01 +00:00
GitHub Action
b4e25e49de Releasing version 1.6.3 2024-11-09 11:31:12 +00:00
Filip Hrisafov
772fae4c77 Prepare release notes for 1.6.3 2024-11-09 12:20:14 +01:00
Filip Hrisafov
efdf435770 #3751 Improve readme to include support for Java 16+ records 2024-11-09 12:17:29 +01:00
Filip Hrisafov
c2bd847599 #3732 Do not generate obsolete imports for LocalDateTime <-> LocalDate conversion 2024-11-09 12:17:13 +01:00
Filip Hrisafov
21fdaa0f82 #3747 Do not generate redundant if condition with constructor mapping and RETURN_DEFAULT null value mapping strategy 2024-11-03 13:46:48 +01:00
Srimathi-S
32f1fea7b5
#3370 Prevent stack overflow error for Immutables with custom builder 2024-11-03 12:52:52 +01:00
Filip Hrisafov
26c5bcd923
Update readme with 1.6.2 2024-09-27 09:15:17 +02:00
GitHub Action
4e0d73db1d Next version 1.7.0-SNAPSHOT 2024-09-16 08:06:43 +00:00
GitHub Action
212607b447 Releasing version 1.6.2 2024-09-16 07:55:31 +00:00
Filip Hrisafov
4fd22d6b26 Prepare release notes for 1.6.2 2024-09-16 09:54:30 +02:00
Filip Hrisafov
a3b4139070 #3717 Fix ClassCastException when getting thrown types for a record accessor 2024-09-16 09:52:57 +02:00
GitHub Action
c74e62a94c Next version 1.7.0-SNAPSHOT 2024-09-15 16:01:43 +00:00
GitHub Action
10d69878a1 Releasing version 1.6.1 2024-09-15 15:52:17 +00:00
Filip Hrisafov
c36f9ae5d1 Prepare release notes for 1.6.1 2024-09-15 17:11:24 +02:00
Filip Hrisafov
3011dd77d7 #3678 before / after mapping for type using builder should only be kept if they are using the actual type in @TargetType or @MappingTarget 2024-09-15 16:45:21 +02:00
Filip Hrisafov
4c1df35ba6 #3703 Use include model instead of manually writing the type name for return type for afterMappingReferencesWithFinalizedReturnType 2024-09-15 16:45:08 +02:00
Filip Hrisafov
2686e852b6
#3661 Use correct type for the Record component read accessors 2024-09-14 01:17:45 +02:00
Filip Hrisafov
12c9c6c1f0 Use email variable for GitHub Bot git email 2024-09-06 16:35:37 +03:00
Filip Hrisafov
796dd94674 Update next release changelog with latest changes 2024-09-02 15:33:08 +02:00
Filip Hrisafov
5232df2707 Try to stabilise MapMappingTest and CarMapperTest 2024-09-02 15:18:37 +02:00
Obolrom
4d9894ba25
#3113 Use LinkedHashSet, LinkedHashSet new factory methods for java >= 19 2024-09-02 10:26:48 +02:00
김기서
23f4802374
Fix method name typo (#3691) 2024-09-02 09:05:01 +02:00
Obolrom
1e89d7497b
Fix method name typo (#3622) 2024-09-02 08:44:17 +02:00
hduelme
c6010c917a Fix typo in readme Maven plugin config 2024-08-31 16:34:54 +02:00
Filip Hrisafov
58dcb9d813
Update latest version and remove some obsolete badges 2024-08-28 11:56:21 +02:00
Filip Hrisafov
c89b616f8c
#3668 Do not apply implicit mappings when using SubclassExhaustiveStrategy#RUNTIME_EXCEPTION and return type is abstract 2024-08-24 12:22:37 +02:00
Filip Hrisafov
6c8a2e184b #3667, #3673 MappingReference should custom MappingOption equality instead of the default only target name based one 2024-08-24 11:29:28 +02:00
Filip Hrisafov
60cd0a4420
#3670 Fix regression when using InheritInverseConfiguration with nested target properties and reversing target = "." 2024-08-24 11:27:52 +02:00
Stefan Simon
b452d7f2c8
#3652 Inverse Inheritance should be possible for ignore-mappings without source 2024-08-18 17:46:35 +02:00
GitHub Action
96d0698417 Next version 1.7.0-SNAPSHOT 2024-08-12 21:08:07 +00:00
GitHub Action
38ec5c5335 Releasing version 1.6.0 2024-08-12 20:59:31 +00:00
thunderhook
81ca739040 #3638 Remove deprecation note of enum mapping via @Mapping 2024-07-25 10:40:12 +02:00
Filip Hrisafov
0f24633d04 Fix update website script to be able to run Linux 2024-07-20 18:05:08 +02:00
GitHub Action
6365a606c1 Next version 1.6.0-SNAPSHOT 2024-07-20 15:45:11 +00:00
GitHub Action
6ef64ea3aa Releasing version 1.6.0.RC1 2024-07-20 15:36:11 +00:00
Filip Hrisafov
bbb9bb403c Fix typo in changelog 2024-07-20 17:21:29 +02:00
Filip Hrisafov
5ce9c537e9 Add release notes 2024-07-20 16:29:17 +02:00
Filip Hrisafov
e2edb1a086 #3504 Add example classes for the passing target type documentation 2024-07-20 16:26:38 +02:00
Stefan Simon
3047760fd0
#3591 Fix duplicate method generation with recursive auto mapping 2024-07-20 16:19:59 +02:00
Obolrom
df49ce5ff9
#3609 Pass bean mapping ignored unmapped source properties to subclass forged methods
Co-authored-by: thunderhook <8238759+thunderhook@users.noreply.github.com>
2024-07-20 14:06:49 +02:00
Filip Hrisafov
66f4288842
#3601 Always use SourceParameterCondition when checking source parameter
This is a breaking change, with this change whenever a source parameter is used as a source for a target property the condition has to apply to source parameters and not properties
2024-07-20 13:53:39 +02:00
thunderhook
52877d36c2 #3634 fix typo in experimental note 2024-07-20 13:06:00 +02:00
thunderhook
eef3bdfca4 #3639 fix documentation link 2024-07-20 13:05:28 +02:00
hduelme
8fa2f40944
Enforce whitespaces around the for colon with CheckStyle (#3642) 2024-07-15 23:18:32 +02:00
Connor McGowan
037da5a1e1
#3635 Fix documentation of unmappedSourcePolicy default (#3637) 2024-07-07 21:19:38 +02:00
Filip Hrisafov
69371708ee
#3574 Respect only explicit mappings but fail on unmapped source fields
* #3574 Respect only explicit mappings but fail on unmapped source fields

This reverts #2560, because we've decided that `@BeanMapping(ignoreByDefault = true)` should only be applied to target properties and not to source properties.
Source properties are anyway ignored, the `BeanMapping#unmappedSourcePolicy` should be used to control what should happen with unmapped source policy
2024-07-06 10:31:32 +02:00
Filip Hrisafov
babb9dedd9 #3602 Doing a release should reset NEXT_RELEASE_CHANGELOG.md 2024-06-30 14:47:27 +02:00
Filip Hrisafov
baa02bf377 #3602 Fix path for update-website.sh scrip in release workflow 2024-05-11 09:32:41 +02:00
GitHub Action
8a679b325d Next version 1.6.0-SNAPSHOT 2024-05-11 07:10:47 +00:00
GitHub Action
21a8b88a0f Releasing version 1.6.0.Beta2 2024-05-11 07:01:58 +00:00
Filip Hrisafov
8e53b4181f #3602 Fix setup-java action for release workflow 2024-05-11 08:47:39 +02:00
Filip Hrisafov
9a5e6b1892
#3602 Automate release with JReleaser
Add JReleaser for automating the release and add a step for automating the publishing of the website
2024-05-11 08:27:20 +02:00
Filip Hrisafov
b33942a010 #3561 Add test case 2024-05-01 08:15:44 +02:00
Filip Hrisafov
0a2a0aa526
#2610 Add support for conditions on source parameters + fix incorrect use of source parameter in presence check method (#3543)
The new `@SourceParameterCondition` is also going to cover the problems in #3270 and #3459.
The changes in the `MethodFamilySelector` are also fixing #3561
2024-04-29 08:05:52 +02:00
Filip Hrisafov
0a935c67a7 #3565 Presence check methods should not be considered as valid mapping candidates 2024-04-28 20:22:53 +02:00
Zegveld
5fbd36c443
#3577 Improve Mapping#ignoreByDefault documentation 2024-04-28 20:20:11 +02:00
hduelme
8e66445fe9
#3564 Correct issue key for Issue3485Test 2024-04-02 12:00:41 +02:00
Oliver Erhart
e815e3cb1e
#3524 Provide tests with Lombok style super builders 2024-03-10 09:15:37 +01:00
hduelme
2c12e75bfc
#3485 Exception for Mapping to target = "." without source 2024-03-03 18:59:32 +01:00
Filip Hrisafov
c374b5267f
#3360 Do not report unmapped source and target properties when result type is abstract due to runtime exception subclass exhaustive strategy (#3526) 2024-02-11 19:53:38 +01:00
Zegveld
bb1cd63485
#2788 Improve unmapped source properties message for forged methods 2024-02-11 13:29:34 +01:00
Filip Hrisafov
ca1fd0d85d
#3331 Do not handle defined mappings if the result type is abstract due to runtime exception subclass exhaustive strategy (#3487) 2024-02-11 12:51:19 +01:00
Oliver Erhart
8191c850e0
#3323 Support access to the source property name 2024-02-11 10:42:23 +01:00
hduelme
0a43bc088f
Add missing generic type to Javadoc Builder (#3499) 2024-01-28 18:07:57 +01:00
hduelme
90a3ce0b46
Use primitive types in NativeTypes (#3501) 2024-01-28 18:06:42 +01:00
Chanyut Yuvacharuskul
6830258f77
Fix typo in chapter-10-advanced-mapping-options.asciidoc (#3500) 2024-01-28 17:51:57 +01:00
hduelme
6322138028
Add missing generic diamond operator to MappingOptions (#3498) 2024-01-28 17:50:33 +01:00
Filip Hrisafov
60f162ca88
#3463 DefaultBuilderProvider should be able to handle methods in parent interfaces 2024-01-28 17:47:39 +01:00
Filip Hrisafov
6cb126cd7c
#3462 Stream getters should not be treated as alternative setter 2023-12-31 09:34:34 +01:00
Oliver Erhart
7e6fee8714
#1064 Provide a switch to turn off CheckStyle on generated test sources 2023-12-27 13:40:04 +01:00
Filip Hrisafov
6d99f7b8f3
#3473 Add Java 21 and EA to build matrix
Fix tests not running green on Java 21
Update Spring to run correctly on Java 21
2023-12-18 07:35:26 +01:00
mosesonline
fa857e9ff4
bump some lib versions (#3460) 2023-12-10 15:26:47 +01:00
Ravil Galeyev
930f5709b6
#3400 Remove unnecessary casts to long and double 2023-11-29 22:25:22 +01:00
Muhammad Usama
2bb2aefed8
#3413 Using Mapping#expression and Mapping#conditionaQualifiedBy(Name) should lead to compile error 2023-11-24 06:34:15 +01:00
wandi34
2af291ce2f
Fix Typo Mappper in SubclassMapping Doc 2023-11-11 21:48:52 +01:00
Filip Hrisafov
04deac2b3a [maven-release-plugin] prepare for next development iteration 2023-11-04 23:06:17 +01:00
Filip Hrisafov
0ac0c42dbc [maven-release-plugin] prepare release 1.6.0.Beta1 2023-11-04 23:06:16 +01:00
Oliver Erhart
79f01e2de0
Change master to main branch and fix CI status badge (#3423) 2023-11-04 21:35:40 +01:00
Oliver Erhart
b77d321ffb
Added recent contributors (including myself) 2023-11-01 00:01:45 +01:00
Xiu Hong Kooi
5d39314bd2
#3376 support mapping from iterables to collection 2023-10-31 23:55:11 +01:00
Filip Hrisafov
c59eca2a77
#3361 Inheriting mappings should only be applied if the target has been redefined 2023-09-30 21:52:07 +02:00
dependabot[bot]
97c389d58b Bump org.codehaus.plexus:plexus-utils from 3.0.20 to 3.0.24 in /parent
Bumps [org.codehaus.plexus:plexus-utils](https://github.com/codehaus-plexus/plexus-utils) from 3.0.20 to 3.0.24.
- [Release notes](https://github.com/codehaus-plexus/plexus-utils/releases)
- [Commits](https://github.com/codehaus-plexus/plexus-utils/compare/plexus-utils-3.0.20...plexus-utils-3.0.24)

---
updated-dependencies:
- dependency-name: org.codehaus.plexus:plexus-utils
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-30 11:11:47 +02:00
Nikolas Charalambidis
032ee4d77a #3374 Lombok compatibility documentation 2023-09-17 09:44:31 +02:00
GVladi
f61a3acec3 #3089 Improve support for Map attributes for Immutables
Co-Authored-By: thunderhook <8238759+thunderhook@users.noreply.github.com>
2023-09-16 10:56:52 +02:00
Filip Hrisafov
ea997f83ce
#2340 Add FUNDING.yml 2023-08-19 10:09:36 +02:00
Ben Zegveld
8cc2bdd092 #3163: Strip wild card when checking for type assignability 2023-08-13 09:26:53 +02:00
Zegveld
721288140a
Feature/2663 (#3007)
#2663 Fix for 2-step mapping with generics.

---------

Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
2023-08-04 10:14:53 +02:00
Filip Hrisafov
812faeef51 Use presence checks for checking source parameter presence
Instead of explicitly doing a null check use a PresenceCheck mechanism for
* BeanMappingMethod
* ContainerMappingMethod
* MapMappingMethod
2023-08-01 15:41:19 +02:00
Anton Erofeev
b2dc64136d
#3292 Simplify expressions, remove redundant expressions 2023-08-01 14:17:50 +02:00
Venkatesh Prasad Kannan
279ab22482
#3309 Add BeanMapping#unmappedSourcePolicy 2023-08-01 09:48:20 +02:00
Filip Hrisafov
28d827a724 Add test case for subclass mapping and bean mapping ignore by default 2023-07-30 10:39:10 +02:00
Lucas Resch
0460c373c0
#3229: Implement InjectionStrategy.SETTER 2023-07-30 10:38:24 +02:00
Roberto Oliveira
230e84efd1 #3340 Update tarLongFileMode to use POSIX 2023-07-29 09:13:55 +02:00
Filip Hrisafov
4abf2d4202 #3317 Do not generate source parameter if check for only primitives 2023-07-09 15:01:06 +02:00
Filip Hrisafov
53c73324ff #3310 Make sure that adders work properly when they are in a generic class 2023-07-09 15:00:57 +02:00
Zegveld
04434af17a
#3296: Skip default and static methods when determining prototype methods 2023-07-08 18:03:56 +02:00
dependabot[bot]
1b1325da6d Bump guava from 29.0-jre to 32.0.0-jre in /parent
Bumps [guava](https://github.com/google/guava) from 29.0-jre to 32.0.0-jre.
- [Release notes](https://github.com/google/guava/releases)
- [Commits](https://github.com/google/guava/commits)

---
updated-dependencies:
- dependency-name: com.google.guava:guava
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-16 12:37:01 +02:00
Filip Hrisafov
86919c637f
#3144 Map to Bean should only be possible for single source mappings and if explicitly used in multi source mappings 2023-05-28 09:55:40 +02:00
Filip Hrisafov
d075d9a5b6 Upgrade Freemarker to 2.3.32 2023-05-27 15:55:15 +02:00
Filip Hrisafov
62d1bd3490
#3280 Refactor method selection and use a context to be able to more easily access information 2023-05-27 15:04:34 +02:00
Filip Hrisafov
c2eed45df1 #3126 Apply target this references in the BeanMappingMethod 2023-05-27 11:17:01 +02:00
Oliver Erhart
51f4e7eba9
#3231 Prefer record constructor annotated with @Default 2023-05-24 06:04:13 +02:00
José Carlos Campanero Ortiz
84c443df9c
#3245 Remove redundant null checks in nested properties 2023-05-24 05:44:36 +02:00
Oliver Erhart
6d205e5bc4
#1454 Support for lifecycle methods on type being built with builders
Add missing support for lifecycle methods with builders:

* `@BeforeMapping` with `@TargetType` the type being build
* `@AftereMapping` with `@TargetType` the type being build
* `@AfterMapping` with `@MappingTarget` the type being build
2023-05-21 22:49:41 +02:00
paparadva
7c90592d05 #2863 Add validation of String type to @TargetPropertyName 2023-05-21 22:21:34 +02:00
Filip Hrisafov
efaa67aadf #3104 Update methods with NullValuePropertyMappingStrategy.IGNORE should use SetterWrapperForCollectionsAndMapsWithNullCheck 2023-05-20 17:04:59 +02:00
Filip Hrisafov
a89c34f00c #3238 Compile error instead of null pointer exception for invalid ignore with target this 2023-05-20 17:04:22 +02:00
Jason Bodnar
d0e4c48228
#3172 Add mapping between Locale and String 2023-05-08 22:23:03 +02:00
Zegveld
bc5a877121
#3054: Allow abstract return type when all directly sealed subtypes are covered by subclass mappings
Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
2023-05-01 11:54:24 +02:00
dependabot[bot]
be94569791 Bump protobuf-java from 3.21.2 to 3.21.7 in /parent
Bumps [protobuf-java](https://github.com/protocolbuffers/protobuf) from 3.21.2 to 3.21.7.
- [Release notes](https://github.com/protocolbuffers/protobuf/releases)
- [Changelog](https://github.com/protocolbuffers/protobuf/blob/main/generate_changelog.py)
- [Commits](https://github.com/protocolbuffers/protobuf/compare/v3.21.2...v3.21.7)

---
updated-dependencies:
- dependency-name: com.google.protobuf:protobuf-java
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-01 11:32:21 +02:00
MengxingYuan
f3dac94701
#2781 Remove unmapped source properties when source parameter is directly mapped 2023-05-01 10:28:51 +02:00
Etien Rožnik
4843123e6e
#3165 Support adders for array / iterable to collection 2023-05-01 09:42:58 +02:00
José Carlos Campanero Ortiz
a8df94cc20
#2987 Support for defining Javadoc in the generated mapper implementation 2023-05-01 09:22:59 +02:00
todzhang's cloudsdocker
970984785d
Update one typo in JavaDoc (#2938) 2023-05-01 09:12:23 +02:00
Bragolgirith
d3b4a168b7
#3199 Add support for implicit conversion between java.time.LocalDate and java.time.LocalDateTime 2023-05-01 09:11:05 +02:00
ro0sterjam
931591a385
#3071 Support defining custom processor options by custom SPI 2023-04-30 17:02:39 +02:00
Zegveld
2f78d3f4e2
#3125: Allow subclassmapping inheritance for methods with identical signature
Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
2023-04-30 16:33:00 +02:00
Filip Hrisafov
1ab5db6a27
Update latest release version to 1.5.5.Final 2023-04-23 22:20:05 +02:00
Filip Hrisafov
86a668661a #3159 Do null value check for collections with default expression 2023-04-23 18:30:08 +02:00
Filip Hrisafov
1bc3436c5c #3239 Mapping composition is no longer experimental 2023-04-22 22:31:03 +02:00
Filip Hrisafov
979b35a2f4 #3153 Do not use compress directive to strip whitespaces for value mapping switch method
When using the compress directive it is going to strip whitespaces from the templates as well (i.e. what the user defined in `ValueMapping#source` and / or `ValueMapping#target
2023-04-22 19:32:02 +02:00
Filip Hrisafov
e69843f46e #3158 BeanMapping#ignoreByDefault should work properly for constructor properties 2023-04-22 19:31:30 +02:00
Filip Hrisafov
c6ea69eaf9 #3186 Do not use conversions in 2-step mapping when they are disabled 2023-04-22 19:31:01 +02:00
Filip Hrisafov
d10d48ccff #3248 BeanMapping#ignoreUnmappedSourceProperties should be inherited for @InheritConfiguration 2023-04-22 19:21:28 +02:00
Claudio Nave
b1034e6703 #3202 Improve line location report for invalid qualifier for SubclassMapping 2023-04-22 18:16:07 +02:00
Filip Hrisafov
9adcb06c34 #3236 Add missing jakarta-cdi to the documentation 2023-04-22 17:46:27 +02:00
Filip Hrisafov
00f891be58 #3112 Add missing brackets 2023-04-22 17:14:46 +02:00
Johnny Lim
3c81d36810
Polish links in docs (#3214) 2023-04-16 21:51:33 +02:00
Filip Hrisafov
162fdb44f4
#3195 Update the location for asking questions 2023-04-16 10:17:32 +02:00
Ben Zegveld
03563d8ffe #3174 Also allow multiple SubclassMapping annotations on an annotation @interface. 2023-04-15 18:31:12 +02:00
Filip Hrisafov
6e9fa87ba9
#3142 Nested forged methods should declare throws from lifecycle methods 2023-04-13 21:52:15 +02:00
Filip Hrisafov
2be1f306a1 #3135 BeanMapping#mappingControl should be inherited by forged methods 2023-04-13 21:50:40 +02:00
José Carlos Campanero Ortiz
62c7ce1cdf
#3112 Document <THROW_EXCEPTION> in the reference guide 2023-04-13 21:48:13 +02:00
Claudio Nave
89db26a1af
#3119 Add qualifiedBy and qualifiedByName to SubclassMapping annotation 2023-03-17 09:06:13 +01:00
Claudio Nave
a7ba12676d
#3110 Fix throws declaration for ValueMapping annotated methods (#3122)
#3110 Fix throws declaration for ValueMapping annotated methods
2023-02-05 12:17:02 +01:00
Filip Hrisafov
fd27380185
#2953 Add support for globally defining nullValueMapMappingStrategy and nullValueIterableMappingStrategy 2022-11-13 14:22:25 +01:00
Filip Hrisafov
82b19b0d8a #3077 Add test case 2022-11-13 14:21:03 +01:00
Zegveld
8894cd5935
#3057: limit do not allow self to subclassmappings. (#3063)
* #3057: limit do not allow self to subclassmappings.
* #3057: determine method candidates after all other fields are set in the constructor.

Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
2022-11-04 14:21:05 +01:00
Filip Hrisafov
16e3ceadec
#2952 Do not treat a getter as an alternative write accessor when using CollectionMappingStrategy#TARGET_IMMUTABLE 2022-11-03 23:29:47 +01:00
Orange Add
93f7c3b8ea
#3015 Fix annotations to the forged methods 2022-11-03 23:22:35 +01:00
Orange Add
6a394ad466
#3037 Support @ValueMapping in meta annotations 2022-11-03 23:21:43 +01:00
Orange Add
bb099a55ee
#3040: Allow using only BeanMapping#mappingControl 2022-11-03 22:02:58 +01:00
Filip Hrisafov
81b2f70dac #3039 Upgrade FreeMarker to 2.3.31 2022-11-03 21:49:06 +01:00
Filip Hrisafov
a5d3542c24 Update latest release version to 1.5.3.Final 2022-10-07 20:43:15 +02:00
Filip Hrisafov
481ab36ca3 #3036 Add missing exclude to full feature test 2022-10-03 21:16:25 +02:00
Filip Hrisafov
3a325ea66b
#3036 Fix compile errors when intersection types are used in lifecycle methods 2022-10-03 21:12:19 +02:00
Filip Hrisafov
266c5fa41c
#1216 Pick candidate method with most specific return type
When there are multiple candidate methods returning different types.
We should be able to use the method with the most specific return type (if such a method exists)
2022-10-02 19:20:13 +02:00
Zegveld
411cc24dac
#2955 Fix @AfterMapping with return type not called for update mappings 2022-10-02 09:34:03 +02:00
José Carlos Campanero Ortiz
90a487ac06
#1427 Add support for custom name in Spring stereotype annotations 2022-10-01 13:47:49 +02:00
Filip Hrisafov
af1eab0ece
#2743 BeanMappingOptions should not be inherited for forged methods 2022-09-29 22:10:27 +02:00
Zegveld
608d476ed2
#3018: Use MappingControl with SubclassMapping 2022-09-29 21:35:51 +02:00
Orange Add
e979f506fa
#2773 Copy @Deprecated annotation from method or mapper to implementation 2022-09-28 22:17:59 +02:00
Filip Hrisafov
73e8fd6152
#2840, #2913, #2921: MethodMatcher should not match widening methods
In the MethodMatcher we need to do a special check when the target type is primitive.
The reason for that is that a Long is assignable to a primitive double.
However, doing that means that information can be lost and thus we should not pick such methods.
When the target type is primitive, then a method will be matched if and only if boxed equivalent of the target type is assignable to the boxed equivalent of the candidate return type
2022-09-27 09:35:54 +02:00
Johnny Lim
811cd569bb
Javadoc and documentation polishing (#3026) 2022-09-26 19:02:01 +02:00
Filip Hrisafov
8d670e7db7 #3008 Replace old issue template with new GitHub issue form templates 2022-09-21 22:10:42 +02:00
Prasanth Omanakuttan
d593afed69
Avoid unnecessary unboxing of Boolean (#3003) 2022-09-12 18:45:22 +02:00
José Carlos Campanero Ortiz
97c6755288
#2963 Add support for enum to integer conversion 2022-09-04 13:58:10 +02:00
Iaroslav Bogdanchikov
bbf63ae177
#2730 Add support for Jakarta XML Binding 2022-09-02 22:04:01 +02:00
Filip Hrisafov
21069e5a2e #2895 Generate more readable annotations 2022-09-02 17:24:37 +02:00
Prasanth Omanakuttan
68571de01b Update Typos in java-doc
Closes #2989
2022-08-29 21:32:00 +02:00
Orange Add
3cc2aa7675
#2825 Fix SubclassMapping stackoverflow exception 2022-08-28 12:41:25 +02:00
Orange Add
ac356cab25
#2983 Add @AnnotateWith support to non bean mapping methods 2022-08-28 12:33:46 +02:00
Filip Hrisafov
4708f4b2aa #2950 Disable CDI in the full features tests on Java 8 2022-08-26 19:36:51 +02:00
Filip Hrisafov
4fa66229d9
#2990 Stabilise top level imports 2022-08-26 19:35:54 +02:00
Taihao Zhang
3f798744ac
Fix typo in docs (#2982) 2022-08-24 19:40:08 +02:00
Filip Hrisafov
853e7b27df
#2925 Fix IllegalArgumentException when resolving generic parameters
When resolving the parameter for a method like:

```
<T> Optional<T> from(T value)
```

There was an exception in javac because getting a DeclaredType from an Optional
with a primitive type argument throws an exception.
Therefore, when assigning the type arguments we get the boxed equivalent.
This problem does not happen in the Eclipse compiler
2022-08-24 19:39:09 +02:00
Filip Hrisafov
42500ca755 #2907 Add test case for nested import of array 2022-08-24 19:38:40 +02:00
Filip Hrisafov
71b1a7b8a2 #2945 Stabilise top level imports
Make sure that GeneratedType always gets the imported types
from a Type before adding them
2022-08-24 19:38:40 +02:00
Filip Hrisafov
b24e831cf0
#2937 Fix conditional check for collections with adders 2022-08-24 19:11:52 +02:00
Filip Hrisafov
237543c47c
#2897 Always import types defined in Mapper#imports 2022-08-24 18:59:31 +02:00
Filip Hrisafov
fd4a2548b3 #2928 Add IntelliJ and Eclipse plugin information 2022-08-24 18:55:05 +02:00
Filip Hrisafov
874bf1fd2c
#2950 Add support for Jakarta CDI 2022-08-24 18:38:44 +02:00
Filip Hrisafov
ef4c26b075
#2949 Do not inverse inherit BeanMapping#ignoreUnmappedSourceProperties 2022-08-24 18:36:43 +02:00
Filip Hrisafov
3e0c62ac36 Publish snapshots when on main 2022-08-21 11:26:42 +02:00
Filip Hrisafov
4118a44630 #2974 Fix typos in documentation
Closes #2974
2022-08-21 10:56:25 +02:00
Hakan
54321d6e66
#2839 Keep thrown types when creating a new ForgedMethod with the same arguments
This fixes a compilation error when mapping fields with the same type due to not wrapping in a `try-catch` block
2022-08-20 15:23:32 +02:00
Prasanth Omanakuttan
46900cabde
Update Typos in javadoc (#2958) 2022-08-20 13:37:43 +02:00
Zegveld
17997ef617
#2901: Fix @TargetType annotation on a @Condition annotated method for a Collection value 2022-08-20 13:01:47 +02:00
Zegveld
849085e026
#1574: Support for annotating the generated code with custom annotations
Add new `@AnnotateWith` annotation.
This annotation can be used to instruct the MapStruct processor
to generate custom annotations in the generated code.
2022-08-20 12:59:38 +02:00
Nikola Ivačič
8fa286fe4c #2688: Support accessing to the target property name 2022-08-01 19:17:33 +02:00
dependabot[bot]
62e73464b2 Bump kotlin-stdlib in /integrationtest/src/test/resources/kotlinDataTest
Bumps [kotlin-stdlib](https://github.com/JetBrains/kotlin) from 1.3.70 to 1.6.0.
- [Release notes](https://github.com/JetBrains/kotlin/releases)
- [Changelog](https://github.com/JetBrains/kotlin/blob/v1.6.0/ChangeLog.md)
- [Commits](https://github.com/JetBrains/kotlin/compare/v1.3.70...v1.6.0)

---
updated-dependencies:
- dependency-name: org.jetbrains.kotlin:kotlin-stdlib
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-07-24 17:55:38 +02:00
Cassius Vinicius de Magalhães
6914889510 Update Chapter 11.2 - Inverse Mappings
Clarification of the inverse mapping usage.
2022-07-14 21:30:16 +02:00
Filip Hrisafov
dd5ac3b637 #2929 Improve documentation for BeanMapping#ignoreByDefault 2022-07-09 21:35:36 +02:00
Iaroslav Bogdanchikov
4b43f9079d #2922 Fix protobuf tests for M1 Macs 2022-07-08 20:35:26 +02:00
fml2
a2b4454a66 fix(docs): No Lombok classes in the runtime 2022-07-01 17:47:55 +02:00
Filip Hrisafov
de8c0c7070 Use UTF-8 when compiling the tests
The test infrastructure that we are using should use UTF-8 for generating the StandardJavaFileManager
2022-06-29 08:37:50 +02:00
Ben Zegveld
88745d151e #2882: target type is now correctly passed on through the MethodReferencePresenceCheck to the MethodReference. 2022-06-18 23:32:26 +02:00
Filip Hrisafov
07d144ebd1 Update readme with latest released 1.5.2.Final release 2022-06-18 19:11:39 +02:00
Filip Hrisafov
1459aabfc3 [maven-release-plugin] prepare for next development iteration 2022-06-18 19:01:18 +02:00
Filip Hrisafov
19973ff818 [maven-release-plugin] prepare release 1.5.2.Final 2022-06-18 19:01:16 +02:00
Sergei Portnov
406ae3fc13
#2891 Fix subclass mapping while superclass has non-empty constructor
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
2022-06-18 18:47:07 +02:00
Filip Hrisafov
98eb46aee9
#2880 Fix missing import for array mapping methods
Co-authored-by: Martin Kamp Jensen <martin.kamp.jensen@se.com>
2022-06-18 13:59:03 +02:00
Filip Hrisafov
fa800926e7 #2837 Add support for text blocks in expressions 2022-06-18 12:32:05 +02:00
Filip Hrisafov
22ad9f636d
#2806 Try to stabilise some date conversion tests by locking them on reading the default timezone 2022-06-14 22:03:32 +02:00
Filip Hrisafov
05ae9922ea Update GitHub actions
Run tests with Java 18
Change actions/checkout to v3
Change actions/setup-java to v3
2022-06-14 22:01:43 +02:00
Filip Hrisafov
d7c0d15fe1 Change required Java version for running MapStruct in the readme 2022-06-05 15:58:37 +02:00
Filip Hrisafov
9247c5d7fb #2870 Use codecov action v2 2022-06-05 13:22:37 +02:00
Filip Hrisafov
4c9aa00369 Update readme with latest released 1.5.1.Final release 2022-06-05 08:53:10 +02:00
Filip Hrisafov
20e97714d4 [maven-release-plugin] prepare for next development iteration 2022-06-05 08:42:56 +02:00
Filip Hrisafov
ec9288ce66 [maven-release-plugin] prepare release 1.5.1.Final 2022-06-05 08:42:55 +02:00
Filip Hrisafov
46b78bfe59 #2867 Fix NPE when reporting message on parent mappers 2022-06-05 08:35:31 +02:00
Filip Hrisafov
0726563024 Update readme with latest released 1.5.0.Final release 2022-06-02 23:30:17 +02:00
Filip Hrisafov
5efe5e291c [maven-release-plugin] prepare for next development iteration 2022-06-02 23:11:41 +02:00
Filip Hrisafov
efa11ba312 [maven-release-plugin] prepare release 1.5.0.Final 2022-06-02 23:11:41 +02:00
Filip Hrisafov
a1a0786cf2
#2846 Add test case showing that everything works as expected 2022-06-02 22:14:42 +02:00
Zegveld
0559c47c21
#2739 Enhance documentation around SPI usage 2022-05-30 21:51:57 +02:00
Filip Hrisafov
c945ccd628 #2835 Upgrade jacoco-maven-plugin to latest 0.8.8 to support Java 17 2022-05-30 21:50:13 +02:00
Filip Hrisafov
9769f51756 #2851 Fix typo in readme 2022-05-30 21:49:57 +02:00
Hao Zhang
a4162809a4
Doc: correct the annotation processor version (#2859)
The lombok-mapstruct-binding anotation procossor version given by document will result a compile problem, correct it by the example repository so that work fine
2022-05-28 11:37:21 +02:00
Zegveld
437a70d6df
#2807: Include LifeCycleMethod importTypes in the list of importTypes. (#2808)
Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
2022-04-08 20:57:40 +02:00
Filip Hrisafov
6604617730 #2794 Compile error when condition expression used with constant or expression 2022-04-03 15:20:00 +02:00
Zegveld
03d44b5a87
#2795: use 'includeModel' for the 'sourcePresenceCheckerReference' in the 'UpdateWrapper'. (#2796)
* #2795: use 'includeModel' for the 'sourcePresenceCheckerReference' in the 'UpdateWrapper'.
* Simplify the tests

Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
2022-04-02 18:55:06 +02:00
Zegveld
2473c3eaaa
#2797: Add the nested type import types in the NestedPropertyMappingMethod
* #2797: Reproduction scenario
* Add the nested type import types in the NestedPropertyMappingMethod

Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
2022-04-02 11:59:25 +02:00
dependabot[bot]
07eeea6bc9 Bump spring-beans from 5.3.15 to 5.3.18 in /parent
Bumps [spring-beans](https://github.com/spring-projects/spring-framework) from 5.3.15 to 5.3.18.
- [Release notes](https://github.com/spring-projects/spring-framework/releases)
- [Commits](https://github.com/spring-projects/spring-framework/compare/v5.3.15...v5.3.18)

---
updated-dependencies:
- dependency-name: org.springframework:spring-beans
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-04-02 11:25:14 +02:00
Filip Hrisafov
08a0313840 [maven-release-plugin] prepare for next development iteration 2022-03-21 08:14:09 +01:00
Filip Hrisafov
7e00af6ff4 [maven-release-plugin] prepare release 1.5.0.RC1 2022-03-21 08:14:08 +01:00
Filip Hrisafov
190b486b79
Add users that have contributed post 1.5.0.Beta2 to copyright.txt 2022-03-19 12:09:30 +01:00
Chris DeLashmutt
ab52867831
#2748 Support mapping map keys with invalid chars for methods 2022-03-19 11:51:19 +01:00
Filip Hrisafov
ad00adfa86
#2538 Allow using 2 step mappings with only one of the 2 methods being qualified 2022-03-12 23:57:17 +01:00
Zegveld
0a69492983
#2755: use raw Type when calling a static method. (#2762) 2022-03-12 18:02:01 +01:00
Zegveld
b6a3aa1512
#2758: fallback to param.variableName if ext.targetBeanName is not present in MethodReference handling. (#2759) 2022-03-12 18:01:15 +01:00
Filip Hrisafov
0b2c7e58b2
Add Christian Kosmowski to the copyright.txt 2022-02-23 15:55:20 +01:00
Zegveld
9b434f80f8
#2715: Updated documentation to reflect impact of conditions on update mappers. (#2740)
* #2715: added an example with an update mapper for Conditional behavior.
2022-02-06 20:05:42 +01:00
Zegveld
7bb85d05c0
2696: Invert @SubclassMappings with @InheritInverseConfiguration. (#2708)
* #2696: Added support for '@InheritInverseConfiguration' with '@SubclassMappings'.
* #2696: Overriding of inverse inheritence implemented. New order has preference over inherited order.
2022-02-06 20:03:23 +01:00
Filip Hrisafov
2a2c11e871
#2629 Use ModuleElement when getting type element
Prior to this MapStruct would only use `Elements#getTypeElement`.
With this PR if the mapper being generated is within a module MapStruct will use that module
for the methods that are needed (getTypeElement and getPackageElement).

Adapt the build to require a minimum Java 11 for building the processor module.
Adapt the GitHub actions to properly run integration tests with Java 8
Ignore Java 9 usages for the animal-sniffer-plugin
2022-01-30 20:52:22 +01:00
Filip Hrisafov
37835a5607
#2677 Use type without bounds when looking for read / presence accessor in a SourceReference 2022-01-30 20:49:05 +01:00
Filip Hrisafov
12070186a4 #2567 Add support for Jakarta Injection
Support for Jakarta is done in 2 ways:

* current jsr330 component model - In this case Jakarta Inject will be used if javax.inject is not available
* new jakarta component model - In this case Jakarta Inject will always be used
2022-01-30 13:31:50 +01:00
Filip Hrisafov
aed3ff5295 #1997 Use builders to construct empty objects in update wrapper 2022-01-30 12:38:42 +01:00
Filip Hrisafov
464adc9143 #2682 Change RetentionPolicy of @DecoratedWith to CLASS
In some circumstances (used with other types of aggregating processors, e.g. Spring)
the Gradle incremental compilation works correctly only for classes annotated with the `CLASS` or `RUNTIME`
retention policy.

The `@DecoratedWith` is the only annotation from MapStruct that was `SOURCE` retention.
With this commit we are changing its retention policy in order for better compatibility with the Gradle Incremental compilation
2022-01-29 17:03:03 +01:00
Filip Hrisafov
5f4d355838
#1661 Add support for globally disabling builders 2022-01-29 11:46:34 +01:00
Filip Hrisafov
aade31f095 #2468 Update needed dependencies for running CDI tests on Java 16+ 2022-01-29 11:25:40 +01:00
Filip Hrisafov
20ff51ebb8
#2728 Add new WithTestDependency annotation for our processor testing
Adding this dependency allows us to dynamically pick the dependencies that we want to have on the test compilation classpath.
It would allow us to more granularly test things with different dependencies, such as javax inject and jakarta inject
2022-01-29 11:13:16 +01:00
Filip Hrisafov
ec30f5d279
#2725 Update tools-gem to 1.0.0.Alpha3 and run GitHub Action with Java 19-ea
Also update Spring to 5.3.15 to be able to run on Java 19
2022-01-29 11:03:44 +01:00
Zegveld
9105041522
#2668: Added support for collections and maps with a no-args constructor (#2671)
#2668: Added support for collections and maps with a no-args constructor. Added a compiler error in case of a collection or map without either a no-arg constructor or a copy constructor.
2022-01-29 00:37:24 +01:00
Justyna
b22efd9ad7
#2674: Add check if method without implementation doesn’t have @AfterMapping / @BeforeMapping annotation 2022-01-24 14:17:24 +01:00
Goni-Dev
0a8e9b738c
#2709 Corrected description for example demonstrating default expression 2022-01-23 17:26:21 +01:00
dependabot[bot]
0f297ae60f Bump protobuf-java from 3.6.0 to 3.16.1 in /parent
Bumps [protobuf-java](https://github.com/protocolbuffers/protobuf) from 3.6.0 to 3.16.1.
- [Release notes](https://github.com/protocolbuffers/protobuf/releases)
- [Changelog](https://github.com/protocolbuffers/protobuf/blob/master/generate_changelog.py)
- [Commits](https://github.com/protocolbuffers/protobuf/compare/v3.6.0...v3.16.1)

---
updated-dependencies:
- dependency-name: com.google.protobuf:protobuf-java
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-01-22 13:24:54 +01:00
Ben Zegveld
277b6f5d2b #2719: added a note at the builder documentation to point towards the Before-/AfterMapping documentation. 2022-01-22 13:12:06 +01:00
Filip Hrisafov
59c5f40ac3
#2686 Add documentation about when mappers are injected 2022-01-18 18:22:47 +01:00
Filip Hrisafov
f7f65ac1de #2687 Add documentation for NullValueMappingStrategy for collections and maps 2022-01-15 10:34:37 +01:00
Valentin Kulesh
42cfa05c40
#2704 Fix broken reference to constants within nested enums 2021-12-26 11:22:05 +01:00
Ben Zegveld
0a7b8134d4 #2689: documentation: fix example title. 2021-12-25 11:50:48 +01:00
Filip Hrisafov
930b07aab8 [maven-release-plugin] prepare for next development iteration 2021-12-12 12:48:56 +01:00
Filip Hrisafov
0de10ca83c [maven-release-plugin] prepare release 1.5.0.Beta2 2021-12-12 12:48:55 +01:00
Zegveld
ea45666d66
#2673: Fix optional wrapping pattern throwing exception when primitive types are present
MethodMatcher incorrectly reported that a primitive type matches a candidate for a type var
2021-12-11 14:16:19 +01:00
Filip Hrisafov
5de813c16f #2666 Presence Check should be applied to source parameters when used in @Mapping 2021-12-04 12:37:15 +01:00
Filip Hrisafov
00df0bc3d0
#2680 Refactor accessors
Split the `PresenceCheck`s accessor from the current `Accessor`.
Introduce a `ReadAccessor` that would allow us to more easily implement certain things.
Remove `MAP_GET` and `MAP_CONTAINS` from the AccessorType and use the new refactored mechanism
2021-12-03 19:13:41 +01:00
dersvenhesse
754aaf2ef4 [DOCS] Fixed reference variable 2021-12-03 19:10:25 +01:00
Filip Hrisafov
13bc0c023c
#2553 Support source property paths for maps 2021-11-20 08:48:08 +01:00
Filip Hrisafov
29008e12bf #2005 Parameter type should only be checked if we are mapping from a single argument source 2021-11-20 08:00:32 +01:00
Zegveld
72e6b1feb5
#2636: defaultValue combined with qualified should not convert if not needed (#2637) 2021-11-14 20:11:05 +01:00
Filip Hrisafov
735a5bef6a
#2225 Add support for suppressing the generation of the timestamp through Mapper and MapperConfig 2021-11-06 09:21:16 +01:00
Filip Hrisafov
166eb699c7 #1752 Always return mapping target when using update methods with return 2021-10-31 17:41:49 +01:00
Filip Hrisafov
907d605160 #2624 Nested target methods should be inherited for forged Map to Bean methods 2021-10-31 17:11:54 +01:00
Zegveld
ca2529f862
#598: Errors/Warnings now end up in the @Mapper annotated class. (#2634)
#598: Errors/Warnings now end up in the @Mapper annotated class.

Co-authored-by: Ben Zegveld <Ben.Zegveld@gmail.com>
2021-10-31 16:46:35 +01:00
Henning Pöttker
e32fc8c283 #2351 NullValueMappingStrategy for Maps and Iterables
With two new parameters for Mapper and MapperConfig, it is now possible to override the nullValueMappingStrategy specifically for MapMapping and IterableMapping.
2021-10-30 13:36:26 +02:00
Filip Hrisafov
80d26a1a9c
#148, #1386, #2593 Only import top level classes
Instead of importing all classes, inner classes will be used through their top level classes only.
This also fixes the problem in #2593 since inner classes are no longer imported but used through their top classes
2021-10-25 08:22:26 +02:00
Filip Hrisafov
564455ee45 #2596 Record components should have the highest priority for read accessors 2021-10-25 08:21:18 +02:00
Filip Hrisafov
935c03e822 #2614 Do not use FQN when mapping Stream to Array 2021-10-25 08:20:59 +02:00
Filip Hrisafov
e86c0faf04 #2611 Scope org.eclipse.tycho:tycho-compiler-jdt under test
Since the removal of the Eclipse Specific compiler workarounds in c2e803403027f3fae92bd15b0ba50ab7df5063e6
the org.eclipse.tycho:tycho-compiler-jdt dependency is no longer needed in the compile code, we only need it for tests
2021-10-24 12:50:49 +02:00
Zegveld
5df6b7a75b
#131, #2438, #366 Add support for Type-Refinement (Downcast) Mapping (#2512)
Add new `@SubclassMapping` for creating Downcast mapping.
When a parent mapping method is annotated with `@SubclassMapping` 
it will now generate an instanceof check inside the parent mapping 
and generate the subclass mappings if they are not manually defined. 

There is also `SubclassExhaustiveStrategy` for controlling what MapStruct should do in case the target type is abstract and there is no suitable way to create it.
2021-10-19 20:44:25 +02:00
valery1707
f167e7a20c Fix typo in JavaDoc 2021-10-16 21:01:20 +02:00
Filip Hrisafov
8b84f5b7d7 #2591 Update dependencies so tests run on Java 18
Update GitHub Actions for tests to run on Java 11, 13, 16, 17 and 18-ea
2021-09-21 22:17:24 +02:00
Filip Hrisafov
b59a23965a #2554 Records should not treat collections as alternative target accessors 2021-09-19 11:42:33 +02:00
Filip Hrisafov
2c23b935db #2541 fix incorrect name for TypeVar with ElementType.TYPE_USE for javac-with-errorprone 2021-09-19 11:41:57 +02:00
Yusuf Kemal Özcan
f0a13bb306 #2555 Add unmappedSourcePolicy annotation processor argument 2021-09-07 22:12:01 +02:00
Tobias Meggendorfer
9057d68cd2
Use DefaultLocale for more stable Issue2544MapperTest (#2569) 2021-08-31 21:45:25 +02:00
Tobias Meggendorfer
9ed4e389f8 #2560 Ignore source properties if ignoreByDefault = true 2021-08-30 21:36:55 +02:00
Daniel Franco
7064e0bc97
Update maven wrapper version to 3.8.2 (#2557) 2021-08-23 16:19:02 +02:00
Adam Szatyin
c52ff812aa
#2552 Add built in conversion between URL and String 2021-08-17 19:21:07 +02:00
Bas Claessen
eb12c485ee
#2515 add ambiguous constructors to ambiguous constructor error message 2021-08-14 09:07:22 +02:00
Bas Claessen
06c416043c
#2515 add ambiguous constructors to ambiguous constructor error message 2021-08-14 09:06:54 +02:00
Henning Pöttker
0d8729767b
Remove remaining references to Hickory (#2511) 2021-08-14 08:38:36 +02:00
Zegveld
c1fa9bd0bd
#2537 Fix incorrect unmapped source property when only defined in Mapping#target 2021-08-14 08:37:20 +02:00
Zegveld
8ad55b164f
#2544: fix missing helper methods for ReverseConversion 2021-08-11 11:35:45 +02:00
Zegveld
196528e578
#2530: fix missing supporting fields for ReverseConversion 2021-08-11 06:49:05 +02:00
Amogh
e6e9b6ce92
#2525 add available transformations to CaseEnumTransformationStrategy exception 2021-07-22 10:08:54 +02:00
Filip Hrisafov
8c554b9556 Change reference from Google Group to GitHub Discussions in reference guide 2021-07-18 15:55:17 +02:00
Filip Hrisafov
43dfd92e05 [maven-release-plugin] prepare for next development iteration 2021-07-18 15:15:44 +02:00
Filip Hrisafov
a91b93f357 [maven-release-plugin] prepare release 1.5.0.Beta1 2021-07-18 15:15:44 +02:00
Sjaak Derksen
a95d1c59c3
#2505 deepclone generates enum mapping method (#2507) 2021-07-03 15:28:10 +02:00
Filip Hrisafov
38744d9f73 Add users that have contributed to 1.5 to the copyright.txt 2021-06-27 18:43:13 +02:00
Christian Kosmowski
985ca2fe64 #1075 Support for Mapping from Map<String, ???> to Bean
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
2021-06-27 07:34:37 +02:00
Filip Hrisafov
fb9c7a3ded #2491 Do not use types not part of java.base in MapStruct processor
MapStruct should not use types that are outside of java.base.
This makes sure that no additional dependencies (such as jaxb-api) are needed on the annotation processor path
2021-06-27 07:34:14 +02:00
Filip Hrisafov
c5c292f602 #2501 Add test case 2021-06-27 07:33:45 +02:00
Andrew
1bf698785c
Fix typo in Named.java (#2500) 2021-06-24 19:19:02 +02:00
chaos
4c338fa1db fix(pom): fix gradle repo url 404
original url is 404 replace with new url
2021-06-23 13:02:14 +02:00
Tobias Meggendorfer
5d8fcfa033 #2481 Report ignored source properties which are missing 2021-06-21 19:50:58 +02:00
Filip Hrisafov
845d83e9d5 #2439 Do not throw NPE getting accessors for null typeElement
Provide better error message if the source type has no read properties
2021-06-20 08:55:57 +02:00
Filip Hrisafov
934a47323a #2478 Make sure that forged methods do not generate duplicate method parameters 2021-06-19 15:55:41 +02:00
Sjaak Derksen
7f38efad4d
#2463 Selection stops when method type-args are flipped (#2487) 2021-06-19 13:56:26 +02:00
Filip Hrisafov
55c62ab43f #2108 Make sure Javadoc can be generated with Java 11
Remove the org.jboss.apiviz.APIviz doclet since it is no longer compatible with Java 11.
Add new group in the Javadoc for the MapStruct Processor SPI
Fix Javadoc warnings
2021-06-19 11:50:48 +02:00
Filip Hrisafov
046077f701 #2483: Update Asciidoctor to latest versions 2021-06-19 11:49:38 +02:00
Filip Hrisafov
08016d9ef2
#2446 Use DefaultLocale and DefaultTimeZone from JUnit Pioneer
Configure the JUnit Platform to run the processor tests in parallel by running different test classes in concurrent threads
2021-06-12 10:16:40 +02:00
Filip Hrisafov
9ce9d4fb3a #2375 Add records cross module integration test 2021-06-12 09:23:36 +02:00
Filip Hrisafov
857f87276f #2466 Update dependencies so tests run on Java 16+
Update GitHub Actions for tests to run on Java 11, 13, 16 and 17-ea.
Update Lombok so tests can run on 16+
Add jaxb-runtime dependency to the maven-jaxb2-plugin see https://github.com/highsource/maven-jaxb2-plugin/issues/148
Disable cdi integration test on Java 16+ until we find a solution for them
2021-06-12 09:20:33 +02:00
Filip Hrisafov
70ea65f7aa #2437 Do not visit the same type element twice when retrieving methods
When using the diamond inheritance a TypeElement might be visited twice.
This is however, incorrect. We need to visit a TypeElement exactly once.
2021-05-16 19:08:45 +02:00
Ewald volkert
a6ac4f3fd6 #2329 use fields for custom date formats with DateTimeFormatter in order to optimise its usage
* GetDateTimeFormatterField
** a FieldReference that creates DateTimeFormatters
   for given dateFormat as mapper fields
** variableName is created using given dateFormat

* AbstractJavaTimeToStringConversion
  provides GetDateTimeFormatterField as required helper field
  using DateTimeFormatter instances provided as mapper fields
  by GetDateTimeFormatterField

* ConversionProvider
  might provide supporting fields directly

* Refactoring
  moved/renamed BuiltInFieldReference, BuiltInConstuctorFragment to
package model/common

* MappingBuilderContext provides access to mapper support fields
  (that are independent of mapper methods)

* MappingResolverImpl / MapperCreationProcessor
  process supporting fields provided by ConversionProvider

* HelperMethod
** extended to supply additional template parameters to
   be more flexible in freemarker templates
** extended to support mapper field reference / constructor fragment

* SupportingMappingMethod
** provide templateParameters to freemarker
** hashCode/equals base on name property instead of template name,
   as we use one specific template multiple times with different
   parameters generating multiple methods
** #getSafeField extracted to SupportingField

* SupportingField
** removed hashCode/equals based on template name,
   as we use one specific template multiple times with different
   parameters generating multiple methods
   (superclass equals/hashcode is fine for that)
** added support for template parameters to be more flexible when
  compiling templates

* Tests to verify DateTimeFormatter instance field creation
2021-05-16 13:22:56 +02:00
Lukas Lazar
cc1562c5ad #2132 Add unmappedTargetPolicy to @BeanMapping 2021-05-15 18:01:22 +02:00
Filip Hrisafov
4576103752 #2445 Improve error reporting when EnumTransformationStrategy throws an error during transformation 2021-05-15 13:04:15 +02:00
João Paulo Bassinello
fdf3dcc8ef #2445 Support for case changing enum transformation strategy
Available case transformations: upper, lower, capital
2021-05-15 13:04:15 +02:00
dependabot[bot]
5c22eee6c3 Bump commons-io from 2.6 to 2.7 in /parent
Bumps commons-io from 2.6 to 2.7.

Signed-off-by: dependabot[bot] <support@github.com>
2021-05-01 13:41:46 +02:00
jason.bodnar@blackbaud.com
0cb053df8d #2391 Add implicit conversion between UUID <-> String 2021-05-01 13:28:03 +02:00
Filip Hrisafov
627be53088 Migrate mapstruct core tests to JUnit Jupiter 2021-04-25 13:05:26 +02:00
Filip Hrisafov
2d66f08ee5 Migrate process tests to use new JUnit Jupiter Infrastructure
Update test annotations to be JUnit Jupiter compatible

Replace all Test annotations from tests that are run with the AnnotationProcessorTestRunner with ProcessorTest.
Replace JUnit 4 Test#expected with assertThatThrownBy from AssertJ.
Replace Rule for GeneratedSource with RegisterExtension.
Fix some tests that were not reverting the changes to the default Locale and TimeZone.
Replace usage of org.junit.Assert with equivalent from org.junit.jupiter.api.Assertions or AssertJ.
2021-04-25 13:05:26 +02:00
Filip Hrisafov
5bbd1a78ea Migrate the processor test infrastructure from JUnit 4 to JUnit Jupiter
With JUnit Jupiter it is still not possible to set the ClassLoader for loading the test class.
However, since 5.8 M1 there is a way to hook into the launcher discovery process and change the Current Thread ContextClassLoader which would load the classes with our customer ClassLoader.
Once JUnit Jupiter 201 is resolved we can simplify this.

The CompilationCache is stored in the GlobalCache with the CompilationRequest as key.
This means that even when methods are not executed in some particular order if they have same WithClasses then they would reuse the cache.
2021-04-25 13:05:26 +02:00
Filip Hrisafov
51cdbd67e3 #2051, #2084 Add new @Condition annotation for custom presence check methods 2021-04-25 12:09:23 +02:00
Filip Hrisafov
a2e1404b93 Refactor presence checks to object in order to simplify the conditional mapping 2021-04-25 12:09:23 +02:00
Filip Hrisafov
1c8fff1475 #2423 Use SetterWrapperForCollectionsAndMapsWithNullCheck if the source has a presence check method 2021-04-24 22:01:09 +02:00
Filip Hrisafov
903e6f3f44 #597 Add String <-> StringBuilder conversion in the documentation 2021-04-24 16:47:27 +02:00
Filip Hrisafov
2be536bb65 #2303 Generated code should use iteration order preserving LinkedHash(Map|Set) instead of Hash(Map|Set) 2021-04-24 14:19:01 +02:00
Filip Hrisafov
5f1b3d7862 #2402 Always add source parameter name when constructing the source references for target this 2021-04-23 07:58:24 +02:00
Filip Hrisafov
c9199b7068 #2393 Use includeModel when generating GeneratedType
With this we make sure that the implementation type will have a correct import in case of a clash with another mapper named the same
2021-04-16 08:41:53 +02:00
Silvère Marie
85d3b310f7 Fix method naming 2021-03-30 08:45:17 +02:00
Sjaak Derksen
1187e357c1
#2239 matching generics (#2320) 2021-03-28 17:34:59 +02:00
Filip Hrisafov
e7f6813d9a #2356 Implicitly ignore reverse inherited mappings that do not have read and write methods 2021-03-27 16:12:44 +01:00
Filip Hrisafov
1964c809d8 Fix typo in missing code formatting in documentation
Closes #2385
2021-03-16 20:24:51 +01:00
Filip Hrisafov
197dd4327a #2339 Polish PR #2362
Use MappingEntry for defaultTarget and nullTarget in ValueMappingMethod to simplify certain things
2021-03-15 00:32:02 +01:00
jude.niroshan11@gmail.com
c4135e68ed #2339 Support throwing an exception as an Enum Mapping option 2021-03-15 00:32:01 +01:00
Jude Niroshan
228660c74f
#2366 Update documentation in regards to Java Module System 2021-03-07 14:11:45 +01:00
Jeroen van Wilgenburg
d9fdd86b94 #2368 fix order of target parameter in tests (and removed some spaces) 2021-03-01 20:58:13 +01:00
Jeroen van Wilgenburg
d5703d3ee8 #2368 fix order of target parameter in documentation 2021-03-01 20:58:13 +01:00
Filip Hrisafov
85af901ea7 #2350 Generate core string to enum mapping when AnyRemaining or AnyUnmapped is not used 2021-02-07 11:21:59 +01:00
Filip Hrisafov
07f5189a72 #2347 Do not generate mapper implementation for private mappers
Provide a compiler error message instead of generating code that will not compile
2021-02-07 11:03:04 +01:00
Filip Hrisafov
f4b62ded89 #2352 Add source element type for Iterable mappings
When the Iterable type we are mapping is not generic
(i.e. it is a custom type extending an Iterable) then the source element type
which is included in the loop was not imported.
2021-02-07 10:37:49 +01:00
Filip Hrisafov
630a8da904 Fix a typo in the Mapper#componentModel 2021-02-06 16:44:07 +01:00
Filip Hrisafov
b643061b57 #2277 Add tests with fixtures with testing the generated source code 2021-02-06 16:31:18 +01:00
dmngb
c59ca79e7f
#2277 default component model: mapper reference use singleton INSTANCE if it exists (#2280)
This allows to easily avoid the runtime dependency on mapstruct.jar:
we can avoid Mappers.getMapper(...) for instantiating used mappers if
the code follows the conventional pattern for creating mapper singletons.

Co-authored-by: GIBOU Damien <damien.gibou@biomerieux.com>
2021-02-06 16:10:32 +01:00
Filip Hrisafov
aeadf8cb77
Update readme with latest released 1.4.2.Final release 2021-01-31 14:52:11 +01:00
Filip Hrisafov
08af258533 #2346 Update documentation and readme about MapStruct and Gradle
Remove obsolete net.ltgt.apt
Use com.diffplug.eclipse.apt for Eclipse
IntelliJ works transparently
2021-01-31 09:26:58 +01:00
Filip Hrisafov
0d8bbacc53 #2250, #2324 Do not throw error when qualifier is missing on iterable mapping
With this change we are relaxing the error handling when qualifiers are used in `@Mapping`
and we allow defining qualifiers for collections / array mapping to mean the same as if it was defined on `@IterableMapping`
i.e. apply the qualifier on the element mapping
2021-01-30 09:43:16 +01:00
Sjaak Derksen
8478a5455b
#2278 inherited property ignored due to ignore on nested level (#2332)
Co-authored-by: sjaakd <sjaakderksen@zonnet.nl>
2021-01-23 09:15:46 +01:00
Filip Hrisafov
dfc7528096 Small fixes for Java 16
* Upgrade japicmp-maven-plugin to 0.15.2
* Do not use deprecated for removal Long constructor
* Update Spring to 5.3.3
* Upgrade Lombok to 1.18.16 + add lombok-mapstruct-binding
2021-01-22 08:33:03 +01:00
Tomas Poledny
4223e3ab81 #2255 Add constants for componentModel 2021-01-17 15:13:42 +01:00
Filip Hrisafov
700293f089 #2301 Implicitly ignore forward inherited mappings from different method types 2021-01-01 21:30:25 +01:00
Filip Hrisafov
f84f756a4c #2293 Use MapperConfig instead of MappingConfig in the documentation 2021-01-01 21:29:48 +01:00
Michael Düsterhus
e73dd1b485 Update chapter-2-set-up.asciidoc
See official maven doc on this: https://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
Otherwise for example intellij doesn't recognize the compiler options on maven import
2020-12-15 19:32:19 +01:00
Filip Hrisafov
84c3bda5a2
#2274, #2023 Fix problems with property mapping using source parameters
Fixes problems when property mapping is using source parameter and has default value / expression or is doing an update
2020-11-22 13:04:26 +01:00
Nikolas Charalambidis
6daea86a1b
Add Lombok subsection in the documentation (#2266) 2020-11-11 22:28:53 +01:00
Filip Hrisafov
6df9243d92 #2253 remove unmapped source properties when source parameter is directly mapped 2020-11-08 09:57:10 +01:00
Filip Hrisafov
75f963adf6 #2263 Fix IndexOutOfBoundsException when resolving TypeVar to a Type 2020-11-08 09:56:15 +01:00
Nikolas Charalambidis
8f9df5b69b
#2258 Fixes vague description of @Default and @ConstructorProperties annotations 2020-11-04 21:39:06 +01:00
Filip Hrisafov
749ded96c1 #2251 Fix incorrect code generated for constructor mapping from implicit source parameter matching 2020-11-01 14:59:44 +01:00
Filip Hrisafov
3256abb79c #2244 Mark mapstruct-processor jar as Spring-Boot-Jar-Type: annotation-processor
Doing this would make sure that starting from Spring Boot 2.4 the mapstruct-processor will not be included in the fat jar produced by the Spring Boot maven plugin if people have it as a maven provided dependency.

This is an alternative if people are not using the maven-compiler-plugin annotationProcessorPaths
2020-10-28 08:54:01 +01:00
Filip Hrisafov
85890dd442 #2245 Local variable should be created when using default value 2020-10-25 14:42:48 +01:00
Kemal Ozcan
e67daa3710 mapstruct#597 Built-In conversion between String and StringBuilder 2020-10-24 17:33:53 +02:00
Saheb Preet Singh
26f62b7ef0 #607 Mapping Iterable<?> object to an object instead of collection 2020-10-21 21:20:46 +02:00
Sjaak Derksen
c2e8034030
861 remove compiler specific workarounds (#2227) 2020-10-21 20:02:28 +02:00
Filip Hrisafov
74d06fea5d #2233 Allow generic of generics in types when matching
e.g. method signature such as
<T> T fromOptional(Optional<T> optional)

when T resolves to another generic class such as Collection<String>
2020-10-21 17:00:05 +02:00
Filip Hrisafov
6102d0cc8e #2236 Different nested target mappings should generate different intermediate methods
Make sure that MappingReferences are taken into consideration when comparing whether 2 mapping methods are equal.
This makes sure that when using nested target mappings that have the same property mappings, but different mappings 2 distinct methods will be created
2020-10-18 22:31:08 +02:00
Filip Hrisafov
50aa9cdbdc Use junit version defined in parent in integration tests 2020-10-17 12:30:43 +02:00
dependabot[bot]
4ddfd2ff51 Bump junit from 4.12 to 4.13.1 in /parent
Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1.
- [Release notes](https://github.com/junit-team/junit4/releases)
- [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md)
- [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1)

Signed-off-by: dependabot[bot] <support@github.com>
2020-10-14 15:46:19 +02:00
Filip Hrisafov
0e902d6412 Update versions in readme to 1.4.1.Final 2020-10-11 10:00:24 +02:00
Filip Hrisafov
58dbaee472 [maven-release-plugin] prepare for next development iteration 2020-10-11 09:35:30 +02:00
Filip Hrisafov
d8f22f8311 [maven-release-plugin] prepare release 1.4.1.Final 2020-10-11 09:35:30 +02:00
Filip Hrisafov
2b95e07d8e Turn of Maven connection pooling to avoid connection issues on CI
The problem and solution are explained in https://github.com/actions/virtual-environments/issues/1499#issuecomment-689467080
2020-10-10 10:22:43 +02:00
Filip Hrisafov
53a5c34ed6 #2206, #2214, #2220: Source property should be correctly determined when only target is defined
When having multiple source properties and only target is defined then the same rules should be applied as if there was no mapping:

* First we check for a matching property in any of the source type
* Second we check if the parameter name matches
2020-10-10 09:44:01 +02:00
Filip Hrisafov
a5f49e591e #2221: Fix configuration inheritance when there are multiple matching source parameters of the same type 2020-10-10 09:43:30 +02:00
Filip Hrisafov
823b5edd9f #2213: primitive arrays should be directly mapped (we are cloning them anyways)
Additionally fix problem when annotations `ElementType.TYPE_USE` not handled correctly for javac
2020-10-03 16:54:01 +02:00
Filip Hrisafov
233fc6de98 #2215: Avoid NPE in IntelliJ EAP 2020.3
Starting from IntelliJ 2020.3 (Build 203.4203.26) the ProcessingEnvironment is wrapped in a Proxy class by IntelliJ.
MapStruct should gracefully handle that and not throw an NPE.
Additionally, we should do best effort to put the used compiler in the generated annotation info
2020-10-03 16:47:31 +02:00
Jasper Vandemalle
4480e0f367 Fix minor typos 2020-10-03 00:35:28 +02:00
Filip Hrisafov
2d750193d1 Update versions in readme to 1.4.0.Final 2020-09-26 11:45:27 +02:00
Filip Hrisafov
eb5b8bb71e [maven-release-plugin] prepare for next development iteration 2020-09-26 10:20:47 +02:00
Filip Hrisafov
9973b92ccb [maven-release-plugin] prepare release 1.4.0.Final 2020-09-26 10:20:45 +02:00
Filip Hrisafov
060f17e3e2 #2125 Report an error when source parameter could not be determined from target mapping 2020-09-21 07:55:45 +02:00
Filip Hrisafov
e17e744b20 Dependency upgrades
Upgrades:
* Maven Enforcer Plugin to 3.0.0-M3
* Maven Surefire Plugin to 3.0.0-M5
* Maven Checkstyle Plugin to 3.1.1
* Maven Bundle Plugin to 5.1.1
* Jacoco Maven Plugin to 0.8.6
* Checkstyle to 8.36.1
* JUnit Jipiter to 5.7.0
* AssertJ to 3.17.2
* Guava to 29.0-jre

Fix AssertJ breaking changes
Use Java 8 or Apache Commons IO instead of Guava where possible
Update GitHub Actions to use JDK 14 and JDK 15-ea
2020-09-20 11:37:23 +02:00
Filip Hrisafov
52ab22bbd8 #2197 Strip leading underscores and digits when sanitizing identifier name 2020-09-20 09:46:41 +02:00
Sjaak Derksen
427f5023ef
#2195 @Beanmapping#resultType should be used to construct return type also if it's a builder (#2196) 2020-09-06 16:12:01 +02:00
Filip Hrisafov
8b22654abd [maven-release-plugin] prepare for next development iteration 2020-08-30 16:31:45 +02:00
Filip Hrisafov
c962962546 [maven-release-plugin] prepare release 1.4.0.CR1 2020-08-30 16:31:44 +02:00
Sjaak Derksen
e0eb0f6bb8
#2164 parameter matching should be done based on name when source name is absent (#2193) 2020-08-30 12:49:09 +02:00
Filip Hrisafov
7dcbef349d
#2169 Add support for defining a custom unexpected value mapping exception
Expose definition via:

* `@EnumMapping`
* `@Mapper`
* `@MapperConfig`
* `EnumMappingStrategy` SPI

Rename `EnumNamingStrategy` to `EnumMappingStrategy`
2020-08-29 13:53:30 +02:00
Filip Hrisafov
a9451b1159 #2185 Fix StackOverflow error when recursive use of mapper in Mapper#uses 2020-08-29 11:56:02 +02:00
Filip Hrisafov
c1feafef4c
#2177 Mapping into a generic class / record with a typed constructor argument should work 2020-08-29 11:22:18 +02:00
Filip Hrisafov
3ce9786cf6
#2174 Forged methods should inherit all thrown exceptions from their property mappings 2020-08-29 08:59:20 +02:00
Makoto Oda
99a1fd609c
Documentation: typo? (#2186) 2020-08-25 20:38:19 +02:00
Makoto Oda
18a5f1bdc0
Documentation: Extra phrase inserted? (#2187) 2020-08-25 20:37:02 +02:00
Makoto Oda
e6279d10c7
Is 'r' missing? (#2188) 2020-08-25 20:34:02 +02:00
Filip Hrisafov
ed16d62a91 #2170 Make sure that an import is created for constructor mapping defined variables 2020-08-02 09:36:22 +02:00
Filip Hrisafov
609824037b #2167 Add missing @since 1.4 to new interfaces, classes and methods 2020-08-01 11:43:46 +02:00
Sjaak Derksen
2e2c20fed7
#2161 use short names instead of FQN's in error messages (#2166) 2020-07-24 23:19:47 +02:00
Filip Hrisafov
ef4bfc9aad [maven-release-plugin] prepare for next development iteration 2020-07-19 18:10:13 +02:00
2016 changed files with 66306 additions and 9486 deletions

2
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,2 @@
github: mapstruct
open_collective: mapstruct

45
.github/ISSUE_TEMPLATE/bug_report.yml vendored Normal file
View File

@ -0,0 +1,45 @@
name: Bug report
description: Create a report and help us improve
labels: ["bug"]
body:
- type: markdown
attributes:
value: |
Please fill in all required fields with as many details as possible.
- type: textarea
id: expected
attributes:
label: Expected behavior
description: |
Describe what you were expecting MapStruct to do
placeholder: |
Here you can also add the generated code that you would like MapStruct to generate
- type: textarea
id: actual
attributes:
label: Actual behavior
description: |
Describe what you observed MapStruct did instead
placeholder: |
Here you can also add the generated code that MapStruct generated
- type: textarea
id: steps
attributes:
label: Steps to reproduce the problem
description: |
- Share your mapping configuration
- An [MCVE (Minimal Complete Verifiable Example)](https://stackoverflow.com/help/minimal-reproducible-example) can be helpful to provide a complete reproduction case
placeholder: |
Share your MapStruct configuration
validations:
required: true
- type: input
id: mapstruct-version
attributes:
label: MapStruct Version
description: |
Which MapStruct version did you use?
Note: While you can obviously continue using older versions of MapStruct, it may well be that your bug is already fixed. If you're using an older version, please also try to reproduce the bug in the latest version of MapStruct before reporting it.
placeholder: ex. MapStruct 1.5.2
validations:
required: true

16
.github/ISSUE_TEMPLATE/config.yml vendored Normal file
View File

@ -0,0 +1,16 @@
contact_links:
- name: MapStruct Discussions
url: https://github.com/mapstruct/mapstruct/discussions
about: Please use the MapStruct GitHub Discussions for open ended discussions and to reach out to the community.
- name: Stack Overflow
url: https://stackoverflow.com/questions/tagged/mapstruct
about: For questions about how to use MapStruct, consider asking your question on Stack Overflow, tagged [mapstruct].
- name: Documentation
url: https://mapstruct.org/documentation/stable/reference/html/
about: The MapStruct reference documentation.
- name: Gitter Chat
url: https://gitter.im/mapstruct/mapstruct-users
about: For realtime communication with the MapStruct community, consider writing in our Gitter chat room.
- name: MapStruct Examples
url: https://github.com/mapstruct/mapstruct-examples
about: Some examples of what can be achieved with MapStruct. (contributions are always welcome)

View File

@ -0,0 +1,43 @@
name: Feature Request
description: Suggest an idea
body:
- type: markdown
attributes:
value: |
Please describe the use-case you have. This will better help us understand the context in which you're looking for a new feature.
- type: textarea
id: use-case
attributes:
label: Use case
description: |
Please describe the use-case you have. This will better help us understand the context in which you're looking for a new feature.
placeholder: Describe the use-case here
validations:
required: true
- type: textarea
id: solution
attributes:
label: Generated Code
description: |
Please describe the possible generated code you'd like to see in MapStruct generate.
Please note, it's not always easy to describe a good solution. Describing the use-case above is much more important to us.
placeholder: Describe the possible solution here.
validations:
required: false
- type: textarea
id: workarounds
attributes:
label: Possible workarounds
description: |
Please describe the possible workarounds you've implemented to work around the lacking functionality.
placeholder: Describe the possible workarounds here.
validations:
required: false
- type: input
id: mapstruct-version
attributes:
label: MapStruct Version
description: What MapStruct version and edition did you try?
placeholder: ex. MapStruct 1.5.2
validations:
required: false

82
.github/scripts/update-website.sh vendored Normal file
View File

@ -0,0 +1,82 @@
#!/bin/bash
#
# Copyright MapStruct Authors.
#
# Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
#
# env vars:
# VERSION
# GH_BOT_EMAIL
# This script has been inspired by the JReleaser update-website.sh (https://github.com/jreleaser/jreleaser/blob/main/.github/scripts/update-website.sh)
set -e
function computePlainVersion() {
echo $1 | sed 's/\([[:digit:]]*\)\.\([[:digit:]]*\)\.\([[:digit:]]*\).*/\1.\2.\3/'
}
function computeMajorMinorVersion() {
echo $1 | sed 's/\([[:digit:]]*\)\.\([[:digit:]]*\).*/\1.\2/'
}
function isStable() {
local PLAIN_VERSION=$(computePlainVersion $1)
if [ "${PLAIN_VERSION}" == "$1" ]; then
echo "yes"
else
echo "no"
fi
}
STABLE=$(isStable $VERSION)
MAJOR_MINOR_VERSION=$(computeMajorMinorVersion $VERSION)
DEV_VERSION=`grep devVersion config.toml | sed 's/.*"\(.*\)"/\1/'`
MAJOR_MINOR_DEV_VERSION=$(computeMajorMinorVersion $DEV_VERSION)
STABLE_VERSION=`grep stableVersion config.toml | sed 's/.*"\(.*\)"/\1/'`
MAJOR_MINOR_STABLE_VERSION=$(computeMajorMinorVersion $STABLE_VERSION)
echo "📝 Updating versions"
SEDOPTION="-i"
if [[ "$OSTYPE" == "darwin"* ]]; then
SEDOPTION="-i ''"
fi
sed $SEDOPTION -e "s/^devVersion = \"\(.*\)\"/devVersion = \"${VERSION}\"/g" config.toml
if [ "${STABLE}" == "yes" ]; then
sed $SEDOPTION -e "s/^stableVersion = \"\(.*\)\"/stableVersion = \"${VERSION}\"/g" config.toml
if [ "${MAJOR_MINOR_STABLE_VERSION}" != ${MAJOR_MINOR_VERSION} ]; then
echo "📝 Updating new stable version"
# This means that we have a new stable version and we need to change the order of the releases.
sed $SEDOPTION -e "s/^order = \(.*\)/order = 500/g" data/releases/${MAJOR_MINOR_VERSION}.toml
NEXT_STABLE_ORDER=$((`ls -1 data/releases | wc -l` - 2))
sed $SEDOPTION -e "s/^order = \(.*\)/order = ${NEXT_STABLE_ORDER}/g" data/releases/${MAJOR_MINOR_STABLE_VERSION}.toml
git add data/releases/${MAJOR_MINOR_STABLE_VERSION}.toml
fi
elif [ "${MAJOR_MINOR_DEV_VERSION}" != "${MAJOR_MINOR_VERSION}" ]; then
echo "📝 Updating new dev version"
# This means that we are updating for a new dev version, but the last dev version is not the one that we are doing.
# Therefore, we need to update add the new data configuration
cp data/releases/${MAJOR_MINOR_DEV_VERSION}.toml data/releases/${MAJOR_MINOR_VERSION}.toml
sed $SEDOPTION -e "s/^order = \(.*\)/order = 1000/g" data/releases/${MAJOR_MINOR_VERSION}.toml
fi
sed $SEDOPTION -e "s/^name = \"\(.*\)\"/name = \"${VERSION}\"/g" data/releases/${MAJOR_MINOR_VERSION}.toml
sed $SEDOPTION -e "s/^releaseDate = \(.*\)/releaseDate = $(date +%F)/g" data/releases/${MAJOR_MINOR_VERSION}.toml
git add data/releases/${MAJOR_MINOR_VERSION}.toml
git add config.toml
echo "📝 Updating distribution resources"
tar -xf tmp/mapstruct-${VERSION}-dist.tar.gz --directory tmp
rm -rf static/documentation/${MAJOR_MINOR_VERSION}
cp -R tmp/mapstruct-${VERSION}/docs static/documentation/${MAJOR_MINOR_VERSION}
mv static/documentation/${MAJOR_MINOR_VERSION}/reference/html/mapstruct-reference-guide.html static/documentation/${MAJOR_MINOR_VERSION}/reference/html/index.html
git add static/documentation/${MAJOR_MINOR_VERSION}
git config --global user.email "${GH_BOT_EMAIL}"
git config --global user.name "GitHub Action"
git commit -a -m "Releasing version ${VERSION}"
git push

View File

@ -2,20 +2,20 @@ name: Java EA
on: [push]
env:
MAVEN_ARGS: -V -B --no-transfer-progress -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120
jobs:
test_jdk_ea:
strategy:
fail-fast: false
matrix:
java: [15-ea]
name: 'Linux JDK ${{ matrix.java }}'
name: 'Linux JDK EA'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: 'Set up JDK'
uses: actions/setup-java@v1
uses: oracle-actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
website: jdk.java.net
release: EA
- name: 'Test'
run: ./mvnw -V -B --no-transfer-progress install -DskipDistribution=true
run: ./mvnw ${MAVEN_ARGS} -Djacoco.skip=true install -DskipDistribution=true

20
.github/workflows/macos.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: Mac OS CI
on: push
env:
MAVEN_ARGS: -V -B --no-transfer-progress -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120
jobs:
mac:
name: 'Mac OS'
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- name: 'Set up JDK 21'
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 21
- name: 'Test'
run: ./mvnw ${MAVEN_ARGS} install

View File

@ -5,62 +5,56 @@ on: [push, pull_request]
env:
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
MAVEN_ARGS: -V -B --no-transfer-progress -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120
jobs:
test_jdk:
strategy:
fail-fast: false
matrix:
java: [11, 13, 14]
java: [21]
name: 'Linux JDK ${{ matrix.java }}'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: 'Set up JDK'
uses: actions/setup-java@v1
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: 'Test'
run: ./mvnw -V -B --no-transfer-progress install -DskipDistribution=true
linux:
name: 'Linux JDK 8'
run: ./mvnw ${MAVEN_ARGS} -Djacoco.skip=${{ matrix.java != 21 }} install -DskipDistribution=${{ matrix.java != 21 }}
- name: 'Generate coverage report'
if: matrix.java == 21
run: ./mvnw jacoco:report
- name: 'Upload coverage to Codecov'
if: matrix.java == 21
uses: codecov/codecov-action@v2
- name: 'Publish Snapshots'
if: matrix.java == 21 && github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'mapstruct/mapstruct'
run: ./mvnw -s etc/ci-settings.xml -DskipTests=true -DskipDistribution=true deploy
integration_test_jdk:
strategy:
fail-fast: false
matrix:
java: [ 8, 11, 17 ]
name: 'Linux JDK ${{ matrix.java }}'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: actions/checkout@v2
- name: 'Set up JDK 8'
uses: actions/setup-java@v1
uses: actions/checkout@v4
- name: 'Set up JDK 21 for building everything'
uses: actions/setup-java@v4
with:
java-version: 8
- name: 'Test'
run: ./mvnw -V -B --no-transfer-progress install
- name: 'Generate coverage report'
run: ./mvnw jacoco:report
- name: 'Upload coverage to Codecov'
uses: codecov/codecov-action@v1
- name: 'Publish Snapshots'
if: github.event_name == 'push' && github.ref == 'refs/heads/master' && github.repository == 'mapstruct/mapstruct'
run: ./mvnw -s etc/ci-settings.xml -DskipTests=true -DskipDistribution=true deploy
windows:
name: 'Windows'
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: 'Set up JDK 8'
uses: actions/setup-java@v1
distribution: 'zulu'
java-version: 21
- name: 'Install Processor'
run: ./mvnw ${MAVEN_ARGS} -DskipTests install -pl processor -am
- name: 'Set up JDK ${{ matrix.java }} for running integration tests'
uses: actions/setup-java@v4
with:
java-version: 8
- name: 'Test'
run: ./mvnw -V -B --no-transfer-progress install
mac:
name: 'Mac OS'
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: 'Set up JDK 8'
uses: actions/setup-java@v1
with:
java-version: 8
- name: 'Test'
run: ./mvnw -V -B --no-transfer-progress install
distribution: 'zulu'
java-version: ${{ matrix.java }}
- name: 'Run integration tests'
run: ./mvnw ${MAVEN_ARGS} verify -pl integrationtest

117
.github/workflows/release.yml vendored Normal file
View File

@ -0,0 +1,117 @@
name: Release
on:
workflow_dispatch:
inputs:
version:
description: 'Release version'
required: true
next:
description: 'Next version'
required: false
jobs:
release:
# This job has been inspired by the moditect release (https://github.com/moditect/moditect/blob/main/.github/workflows/release.yml)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: 21
distribution: 'zulu'
cache: maven
- name: Set release version
id: version
run: |
RELEASE_VERSION=${{ github.event.inputs.version }}
NEXT_VERSION=${{ github.event.inputs.next }}
PLAIN_VERSION=`echo ${RELEASE_VERSION} | awk 'match($0, /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)/) { print substr($0, RSTART, RLENGTH); }'`
COMPUTED_NEXT_VERSION="${PLAIN_VERSION}-SNAPSHOT"
if [ -z $NEXT_VERSION ]
then
NEXT_VERSION=$COMPUTED_NEXT_VERSION
fi
./mvnw -ntp -B versions:set versions:commit -DnewVersion=$RELEASE_VERSION -pl :mapstruct-parent -DgenerateBackupPoms=false
git config --global user.email "${{ vars.GH_BOT_EMAIL }}"
git config --global user.name "GitHub Action"
git commit -a -m "Releasing version $RELEASE_VERSION"
git push
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_ENV
echo "PLAIN_VERSION=$PLAIN_VERSION" >> $GITHUB_ENV
- name: Stage
run: |
export GPG_TTY=$(tty)
./mvnw -ntp -B --file pom.xml \
-Dmaven.site.skip=true -Drelease=true -Ppublication,stage
- name: Release
env:
JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
JRELEASER_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }}
JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
JRELEASER_NEXUS2_MAVEN_CENTRAL_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
JRELEASER_NEXUS2_MAVEN_CENTRAL_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
run: |
./mvnw -ntp -B --file pom.xml -pl :mapstruct-parent -Pjreleaser jreleaser:release
- name: JReleaser output
if: always()
uses: actions/upload-artifact@v4
with:
name: jreleaser-release
path: |
parent/target/jreleaser/trace.log
parent/target/jreleaser/output.properties
- name: Reset NEXT_RELEASE_CHANGELOG.md
run: |
echo -e "### Features\n\n### Enhancements\n\n### Bugs\n\n### Documentation\n\n### Build\n" > NEXT_RELEASE_CHANGELOG.md
- name: Set next version
run: |
./mvnw -ntp -B versions:set versions:commit -DnewVersion=${{ env.NEXT_VERSION }} -pl :mapstruct-parent -DgenerateBackupPoms=false
sed -i -e "s@project.build.outputTimestamp>.*</project.build.outputTimestamp@project.build.outputTimestamp>\${git.commit.author.time}</project.build.outputTimestamp@g" parent/pom.xml
git config --global user.email "${{ vars.GH_BOT_EMAIL }}"
git config --global user.name "GitHub Action"
git commit -a -m "Next version ${{ env.NEXT_VERSION }}"
git push
update-website:
# This job has been inspired by the JReleaser update-website job (https://github.com/jreleaser/jreleaser/blob/main/.github/workflows/release.yml)
name: Update Website
needs: [release]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: mapstruct/mapstruct.org
ref: main
fetch-depth: 0
token: ${{ secrets.GIT_WEBSITE_ACCESS_TOKEN }}
- name: Download assets
shell: bash
run: |
curl -sL https://raw.githubusercontent.com/mapstruct/mapstruct/main/.github/scripts/update-website.sh --output update-website.sh --create-dirs --output-dir tmp
curl -sL "https://github.com/mapstruct/mapstruct/releases/download/${VERSION}/mapstruct-${VERSION}-dist.tar.gz" --output mapstruct-${VERSION}-dist.tar.gz --create-dirs --output-dir tmp
env:
VERSION: ${{ github.event.inputs.version }}
- name: Commit
shell: bash
env:
VERSION: ${{ github.event.inputs.version }}
GH_BOT_EMAIL: ${{ vars.GH_BOT_EMAIL }}
run: |
chmod +x tmp/update-website.sh
tmp/update-website.sh

20
.github/workflows/windows.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: Windows CI
on: push
env:
MAVEN_ARGS: -V -B --no-transfer-progress -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120
jobs:
windows:
name: 'Windows'
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: 'Set up JDK 21'
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 21
- name: 'Test'
run: ./mvnw %MAVEN_ARGS% install

View File

@ -1,117 +0,0 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}

Binary file not shown.

View File

@ -1,2 +1,19 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

View File

@ -1,9 +0,0 @@
- [ ] Is this an issue (and hence not a question)?
If this is a question how to use MapStruct there are several resources available.
- Our reference- and API [documentation](http://mapstruct.org/documentation/reference-guide/).
- Our [examples](https://github.com/mapstruct/mapstruct-examples) repository (contributions always welcome)
- Our [FAQ](http://mapstruct.org/faq/)
- [StackOverflow](https://stackoverflow.com), tag MapStruct
- [Gitter](https://gitter.im/mapstruct/mapstruct-users) (you usually get fast feedback)
- Our [google group](https://groups.google.com/forum/#!forum/mapstruct-users)

29
NEXT_RELEASE_CHANGELOG.md Normal file
View File

@ -0,0 +1,29 @@
### Features
* Support for Java 21 Sequenced Collections (#3240)
### Enhancements
* Add support for locale parameter for numberFormat and dateFormat (#3628)
* Detect Builder without a factory method (#3729) - With this if there is an inner class that ends with `Builder` and has a constructor with parameters,
it will be treated as a potential builder.
Builders through static methods on the type have a precedence.
* Behaviour change: Warning when the target has no target properties (#1140)
### Bugs
* Improve error message when mapping non-iterable to array (#3786)
### Documentation
### Build
### Behaviour Change
#### Warning when the target has no target properties
With this change, if the target bean does not have any target properties, a warning will be shown.
This is like this to avoid potential mistakes by users, where they might think that the target bean has properties, but it does not.

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

View File

@ -29,7 +29,9 @@
<!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
<module name="Translation"/>
<module name="FileLength"/>
<module name="FileLength">
<property name="max" value="2500"/>
</module>
<module name="LineLength">
<property name="max" value="120"/>
@ -144,7 +146,9 @@
</module>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<module name="WhitespaceAround">
<property name="ignoreEnhancedForColon" value="false"/>
</module>
<module name="EmptyLineSeparator">
<property name="tokens" value="IMPORT, CLASS_DEF, INTERFACE_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>

View File

@ -8,6 +8,8 @@ Andrei Arlou - https://github.com/Captain1653
Andres Jose Sebastian Rincon Gonzalez - https://github.com/stianrincon
Arne Seime - https://github.com/seime
Christian Bandowski - https://github.com/chris922
Chris DeLashmutt - https://github.com/cdelashmutt-pivotal
Christian Kosmowski - https://github.com/ckosmowski
Christian Schuster - https://github.com/chschu
Christophe Labouisse - https://github.com/ggtools
Ciaran Liedeman - https://github.com/cliedeman
@ -18,20 +20,32 @@ Darren Rambaud - https://github.com/xyzst
Dekel Pilli - https://github.com/dekelpilli
Dilip Krishnan - https://github.com/dilipkrish
Dmytro Polovinkin - https://github.com/navpil
Ewald Volkert - https://github.com/eforest
Eric Martineau - https://github.com/ericmartineau
Ewald Volkert - https://github.com/eforest
Filip Hrisafov - https://github.com/filiphr
Florian Tavares - https://github.com/neoXfire
Gervais Blaise - https://github.com/gervaisb
Gibou Damien - https://github.com/dmngb
Gunnar Morling - https://github.com/gunnarmorling
Ivo Smid - https://github.com/bedla
Jason Bodnar - https://github.com/Blackbaud-JasonBodnar
Jeroen van Wilgenburg - https://github.com/jvwilge
Jeff Smyth - https://github.com/smythie86
João Paulo Bassinello - https://github.com/jpbassinello
Jonathan Kraska - https://github.com/jakraska
Joshua Spoerri - https://github.com/spoerri
Jude Niroshan - https://github.com/JudeNiroshan
Justyna Kubica-Ledzion - https://github.com/JKLedzion
Kemal Özcan - https://github.com/yekeoe
Kevin Grüneberg - https://github.com/kevcodez
Lukas Lazar - https://github.com/LukeLaz
Nikolas Charalambidis - https://github.com/Nikolas-Charalambidis
Michael Pardo - https://github.com/pardom
Muhammad Usama - https://github.com/the-mgi
Mustafa Caylak - https://github.com/luxmeter
Oliver Ehrenmüller - https://github.com/greuelpirat
Oliver Erhart - https://github.com/thunderhook
Paul Strugnell - https://github.com/ps-powa
Pascal Grün - https://github.com/pascalgn
Pavel Makhov - https://github.com/streetturtle
@ -48,7 +62,12 @@ Sjaak Derksen - https://github.com/sjaakd
Stefan May - https://github.com/osthus-sm
Taras Mychaskiw - https://github.com/twentylemon
Thibault Duperron - https://github.com/Zomzog
Tomáš Poledný - https://github.com/Saljack
Tobias Meggendorfer - https://github.com/incaseoftrouble
Tillmann Gaida - https://github.com/Tillerino
Timo Eckhardt - https://github.com/timoe
Tomek Gubala - https://github.com/vgtworld
Valentin Kulesh - https://github.com/unshare
Vincent Alexander Beelte - https://github.com/grandmasterpixel
Winter Andreas - https://github.dev/wandi34
Xiu Hong Kooi - https://github.com/kooixh

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
@ -22,8 +22,8 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -0,0 +1,176 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;
/**
* This can be used to have mapstruct generate additional annotations on classes/methods.
* <p>
* Examples based on the spring framework annotations.
* </p>
* Marking a class as `Lazy`:
*
* <pre><code>
* &#64;AnnotateWith( value = Lazy.class )
* &#64;Mapper
* public interface FooMapper {
* // mapper code
* }
* </code></pre>
*
* The following code would be generated:
*
* <pre><code>
* &#64;Lazy
* public class FooMapperImpl implements FooMapper {
* // mapper code
* }
* </code></pre>
* Setting the profile on the generated implementation:
*
* <pre><code>
* &#64;AnnotateWith( value = Profile.class, elements = @AnnotateWith.Element( strings = "prod" ) )
* &#64;Mapper
* public interface FooMapper {
* // mapper code
* }
* </code></pre>
*
* The following code would be generated:
*
* <pre><code>
* &#64;Profile( value = "prod" )
* public class FooMapperImpl implements FooMapper {
* // mapper code
* }
* </code></pre>
*
* @author Ben Zegveld
* @since 1.6
*/
@Repeatable( AnnotateWiths.class )
@Retention( CLASS )
@Target( { TYPE, METHOD, ANNOTATION_TYPE } )
public @interface AnnotateWith {
/**
* @return the annotation class that needs to be added.
*/
Class<? extends Annotation> value();
/**
* @return the annotation elements that are to be applied to this annotation.
*/
Element[] elements() default {};
/**
* Used in combination with {@link AnnotateWith} to configure the annotation elements. Only 1 value type may be used
* within the same annotation at a time. For example mixing shorts and ints is not allowed.
*
* @author Ben Zegveld
* @since 1.6
*/
@interface Element {
/**
* @return name of the annotation element.
*/
String name() default "value";
/**
* cannot be used in conjunction with other value fields.
*
* @return short value(s) for the annotation element.
*/
short[] shorts() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return byte value(s) for the annotation element.
*/
byte[] bytes() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return int value(s) for the annotation element.
*/
int[] ints() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return long value(s) for the annotation element.
*/
long[] longs() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return float value(s) for the annotation element.
*/
float[] floats() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return double value(s) for the annotation element.
*/
double[] doubles() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return char value(s) for the annotation element.
*/
char[] chars() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return boolean value(s) for the annotation element.
*/
boolean[] booleans() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return string value(s) for the annotation element.
*/
String[] strings() default {};
/**
* cannot be used in conjunction with other value fields.
*
* @return class value(s) for the annotation element.
*/
Class<?>[] classes() default {};
/**
* only used in conjunction with the {@link #enums()} annotation element.
*
* @return the class of the enum.
*/
Class<? extends Enum<?>> enumClass() default NullEnum.class;
/**
* cannot be used in conjunction with other value fields. {@link #enumClass()} is also required when using
* {@link #enums()}
*
* @return enum value(s) for the annotation element.
*/
String[] enums() default {};
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;
/**
* This can be used to have mapstruct generate additional annotations on classes/methods.
*
* @author Ben Zegveld
* @since 1.6
*/
@Retention( CLASS )
@Target( { TYPE, METHOD } )
public @interface AnnotateWiths {
/**
* The configuration of the additional annotations.
*
* @return The configuration of the additional annotations.
*/
AnnotateWith[] value();
}

View File

@ -14,10 +14,13 @@ import java.lang.annotation.Target;
import org.mapstruct.control.MappingControl;
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
import static org.mapstruct.SubclassExhaustiveStrategy.COMPILE_ERROR;
/**
* Configures the mapping between two bean types.
* <p>
* Unless otherwise specified these properties are inherited to the generated bean mapping methods.
* <p>
* Either {@link #resultType()}, {@link #qualifiedBy()} or {@link #nullValueMappingStrategy()} must be specified.
* </p>
* <p><strong>Example:</strong> Determining the result type</p>
@ -57,6 +60,8 @@ public @interface BeanMapping {
/**
* Specifies the result type of the factory method to be used in case several factory methods qualify.
* <p>
* <b>NOTE</b>: This property is not inherited to generated mapping methods
*
* @return the resultType to select
*/
@ -116,9 +121,32 @@ public @interface BeanMapping {
*/
NullValueCheckStrategy nullValueCheckStrategy() default ON_IMPLICIT_CONVERSION;
/**
* Determines how to handle missing implementation for super classes when using the {@link SubclassMapping}.
*
* Overrides the setting on {@link MapperConfig} and {@link Mapper}.
*
* @return strategy to handle missing implementation combined with {@link SubclassMappings}.
*
* @since 1.5
*/
SubclassExhaustiveStrategy subclassExhaustiveStrategy() default COMPILE_ERROR;
/**
* Specifies the exception type to be thrown when a missing subclass implementation is detected
* in combination with {@link SubclassMappings}, based on the {@link #subclassExhaustiveStrategy()}.
* <p>
* This exception will only be thrown when the {@code subclassExhaustiveStrategy} is set to
* {@link SubclassExhaustiveStrategy#RUNTIME_EXCEPTION}.
*
* @return the exception class to throw when missing implementations are found.
* Defaults to {@link IllegalArgumentException}.
*/
Class<? extends Exception> subclassExhaustiveException() default IllegalArgumentException.class;
/**
* Default ignore all mappings. All mappings have to be defined manually. No automatic mapping will take place. No
* warning will be issued on missing target properties.
* warning will be issued on missing source or target properties.
*
* @return The ignore strategy (default false).
*
@ -133,6 +161,8 @@ public @interface BeanMapping {
* source properties report.
* <p>
* <b>NOTE</b>: This does not support ignoring nested source properties
* <p>
* <b>NOTE</b>: This property is not inherited to generated mapping methods
*
* @return The source properties that should be ignored when performing a report
*
@ -140,6 +170,28 @@ public @interface BeanMapping {
*/
String[] ignoreUnmappedSourceProperties() default {};
/**
* How unmapped properties of the source type of a mapping should be reported.
* If no policy is configured, the policy given via {@link MapperConfig#unmappedSourcePolicy()} or
* {@link Mapper#unmappedSourcePolicy()} will be applied, using {@link ReportingPolicy#IGNORE} by default.
*
* @return The reporting policy for unmapped source properties.
*
* @since 1.6
*/
ReportingPolicy unmappedSourcePolicy() default ReportingPolicy.IGNORE;
/**
* How unmapped properties of the target type of a mapping should be reported.
* If no policy is configured, the policy given via {@link MapperConfig#unmappedTargetPolicy()} or
* {@link Mapper#unmappedTargetPolicy()} will be applied, using {@link ReportingPolicy#WARN} by default.
*
* @return The reporting policy for unmapped target properties.
*
* @since 1.5
*/
ReportingPolicy unmappedTargetPolicy() default ReportingPolicy.WARN;
/**
* The information that should be used for the builder mappings. This can be used to define custom build methods
* for the builder strategy that one uses.

View File

@ -7,6 +7,54 @@ package org.mapstruct;
/**
* Strategy for propagating the value of collection-typed properties from source to target.
* <p>
* In the table below, the dash {@code -} indicates a property name.
* Next, the trailing {@code s} indicates the plural form.
* The table explains the options and how they are applied to the presence / absence of a
* {@code set-s}, {@code add-} and / or {@code get-s} method on the target object.
* <table>
* <caption>Collection mapping strategy options</caption>
* <tr>
* <th>Option</th>
* <th>Only target set-s Available</th>
* <th>Only target add- Available</th>
* <th>Both set-s/add- Available</th>
* <th>No set-s/add- Available</th>
* <th>Existing Target ({@code @TargetType})</th>
* </tr>
* <tr>
* <td>{@link #ACCESSOR_ONLY}</td>
* <td>set-s</td>
* <td>get-s</td>
* <td>set-s</td>
* <td>get-s</td>
* <td>get-s</td>
* </tr>
* <tr>
* <td>{@link #SETTER_PREFERRED}</td>
* <td>set-s</td>
* <td>add-</td>
* <td>set-s</td>
* <td>get-s</td>
* <td>get-s</td>
* </tr>
* <tr>
* <td>{@link #ADDER_PREFERRED}</td>
* <td>set-s</td>
* <td>add-</td>
* <td>add-</td>
* <td>get-s</td>
* <td>get-s</td>
* </tr>
* <tr>
* <td>{@link #TARGET_IMMUTABLE}</td>
* <td>set-s</td>
* <td>exception</td>
* <td>set-s</td>
* <td>exception</td>
* <td>set-s</td>
* </tr>
* </table>
*
* @author Sjaak Derksen
*/

View File

@ -0,0 +1,96 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation marks a method as a <em>presence check method</em> to check for presence in beans
* or it can be used to define additional check methods for something like source parameters.
* <p>
* By default, bean properties are checked against {@code null} or using a presence check method in the source bean.
* If a presence check method is available then it will be used instead.
* <p>
* Presence check methods have to return {@code boolean}.
* The following parameters are accepted for the presence check methods:
* <ul>
* <li>The parameter with the value of the source property.
* e.g. the value given by calling {@code getName()} for the name property of the source bean
* - only possible when using the {@link ConditionStrategy#PROPERTIES}
* </li>
* <li>The mapping source parameter</li>
* <li>{@code @}{@link Context} parameter</li>
* <li>
* {@code @}{@link TargetPropertyName} parameter -
* only possible when using the {@link ConditionStrategy#PROPERTIES}
* </li>
* <li>
* {@code @}{@link SourcePropertyName} parameter -
* only possible when using the {@link ConditionStrategy#PROPERTIES}
* </li>
* </ul>
*
* <strong>Note:</strong> The usage of this annotation is <em>mandatory</em>
* for a method to be considered as a presence check method.
*
* <pre><code class='java'>
* public class PresenceCheckUtils {
*
* &#64;Condition
* public static boolean isNotEmpty(String value) {
* return value != null &#38;&#38; !value.isEmpty();
* }
* }
*
* &#64;Mapper(uses = PresenceCheckUtils.class)
* public interface MovieMapper {
*
* MovieDto map(Movie movie);
* }
* </code></pre>
* <p>
* The following implementation of {@code MovieMapper} will be generated:
*
* <pre><code class='java'>
* public class MovieMapperImpl implements MovieMapper {
*
* &#64;Override
* public MovieDto map(Movie movie) {
* if ( movie == null ) {
* return null;
* }
*
* MovieDto movieDto = new MovieDto();
*
* if ( PresenceCheckUtils.isNotEmpty( movie.getTitle() ) ) {
* movieDto.setTitle( movie.getTitle() );
* }
*
* return movieDto;
* }
* }
* </code></pre>
* <p>
* This annotation can also be used as a meta-annotation to define the condition strategy.
*
* @author Filip Hrisafov
* @see SourceParameterCondition
* @since 1.5
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.CLASS)
public @interface Condition {
/**
* @return the places where the condition should apply to
* @since 1.6
*/
ConditionStrategy[] appliesTo() default ConditionStrategy.PROPERTIES;
}

View File

@ -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;
/**
* Strategy for defining what to what a condition (check) method is applied to
*
* @author Filip Hrisafov
* @since 1.6
*/
public enum ConditionStrategy {
/**
* The condition method should be applied whether a property should be mapped.
*/
PROPERTIES,
/**
* The condition method should be applied to check if a source parameters should be mapped.
*/
SOURCE_PARAMETERS,
}

View File

@ -22,7 +22,7 @@ import java.lang.annotation.Target;
* <p>
* <b>NOTE:</b> This annotation is not supported for the component model {@code cdi}. Use CDI's own
* <a href="https://docs.jboss.org/cdi/spec/1.0/html/decorators.html">{@code @Decorator}</a> feature instead.
* <p>
* </p>
* <h2>Examples</h2>
* <p>
* For the examples below, consider the following mapper declaration:
@ -103,12 +103,12 @@ import java.lang.annotation.Target;
* private PersonMapper personMapper; // injects the decorator, with the injected original mapper
* </pre>
*
* <h3>3. Component model 'jsr330'</h3>
* <h3>3. Component model 'jsr330' or 'jakarta'</h3>
* <h4>Referencing the original mapper</h4>
* <p>
* JSR 330 doesn't specify qualifiers and only allows to specifically name the beans. Hence, the generated
* implementation of the original mapper is annotated with
* {@code @javax.inject.Named("fully-qualified-name-of-generated-impl")} and {@code @Singleton} (please note that when
* JSR 330 / Jakarta Inject doesn't specify qualifiers and only allows to specifically name the beans. Hence,
* the generated implementation of the original mapper is annotated with
* {@code @Named("fully-qualified-name-of-generated-impl")} and {@code @Singleton} (please note that when
* using a decorator, the class name of the mapper implementation ends with an underscore). To inject that bean in your
* decorator, add the same annotation to the delegate field (e.g. by copy/pasting it from the generated class):
*
@ -145,7 +145,7 @@ import java.lang.annotation.Target;
* @author Gunnar Morling
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
public @interface DecoratedWith {
/**

View File

@ -98,12 +98,24 @@ public @interface EnumMapping {
* <ul>
* <li>{@link MappingConstants#SUFFIX_TRANSFORMATION} - applies the given {@link #configuration()} as a
* suffix to the source enum</li>
* <li>{@link MappingConstants#STRIP_SUFFIX_TRANSFORMATION} - strips the the given {@link #configuration()}
* <li>{@link MappingConstants#STRIP_SUFFIX_TRANSFORMATION} - strips the given {@link #configuration()}
* from the end of the source enum</li>
* <li>{@link MappingConstants#PREFIX_TRANSFORMATION} - applies the given {@link #configuration()} as a
* prefix to the source enum</li>
* <li>{@link MappingConstants#STRIP_PREFIX_TRANSFORMATION} - strips the given {@link #configuration()} from
* the start of the source enum</li>
* <li>
* {@link MappingConstants#CASE_TRANSFORMATION} - applies the given {@link #configuration()} case
* transformation to the source enum. Supported configurations are:
* <ul>
* <li><i>upper</i> - Performs upper case transformation to the source enum</li>
* <li><i>lower</i> - Performs lower case transformation to the source enum</li>
* <li>
* <i>capital</i> - Performs capitalisation of the first character of every word in the source enum
* and everything else to lower case. A word is split by "_".
* </li>
* </ul>
* </li>
* </ul>
*
* It is possible to use custom name transformation strategies by implementing the {@code
@ -111,7 +123,7 @@ public @interface EnumMapping {
*
* @return the name transformation strategy
*/
String nameTransformationStrategy();
String nameTransformationStrategy() default "";
/**
* The configuration that should be passed on the appropriate name transformation strategy.
@ -119,5 +131,26 @@ public @interface EnumMapping {
*
* @return the configuration to use
*/
String configuration();
String configuration() default "";
/**
* Exception that should be thrown by the generated code if no mapping matches.
* If no exception is configured, the exception given via {@link MapperConfig#unexpectedValueMappingException()} or
* {@link Mapper#unexpectedValueMappingException()} will be used, using {@link IllegalArgumentException} by default.
*
* <p>
* Note:
* <ul>
* <li>
* The defined exception should at least have a constructor with a {@link String} parameter.
* </li>
* <li>
* If the defined exception is a checked exception then the enum mapping methods should have that exception
* in the throws clause.
* </li>
* </ul>
*
* @return the exception that should be used in the generated code
*/
Class<? extends Exception> unexpectedValueMappingException() default IllegalArgumentException.class;
}

View File

@ -0,0 +1,67 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Configures the ignored of one bean attribute.
*
* <p>
* The name all attributes of for ignored is to be specified via {@link #targets()}.
* </p>
*
* <p>
* <strong>Example 1:</strong> Implicitly mapping fields with the same name:
* </p>
*
* <pre><code class='java'>
* // We need ignored Human.name and Human.lastName
* // we can use &#64;Ignored with parameters "name", "lastName" {@link #targets()}
* &#64;Mapper
* public interface HumanMapper {
* &#64;Ignored( targets = { "name", "lastName" } )
* HumanDto toHumanDto(Human human)
* }
* </code></pre>
* <pre><code class='java'>
* // generates:
* &#64;Override
* public HumanDto toHumanDto(Human human) {
* humanDto.setFullName( human.getFullName() );
* // ...
* }
* </code></pre>
*
* @author Ivashin Aleksey
*/
@Repeatable(IgnoredList.class)
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface Ignored {
/**
* Whether the specified properties should be ignored by the generated mapping method.
* This can be useful when certain attributes should not be propagated from source to target or when properties in
* the target object are populated using a decorator and thus would be reported as unmapped target property by
* default.
*
* @return The target names of the configured properties that should be ignored
*/
String[] targets();
/**
* The prefix that should be applied to all the properties specified via {@link #targets()}.
*
* @return The target prefix to be applied to the defined properties
*/
String prefix() default "";
}

View File

@ -0,0 +1,54 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Configures the ignored list for several bean attributes.
* <p>
* <strong>TIP: When using Java 8 or later, you can omit the {@code @IgnoredList}
* wrapper annotation and directly specify several {@code @Ignored} annotations on one method.</strong>
*
* <p>These two examples are equal.
* </p>
* <pre><code class='java'>
* // before Java 8
* &#64;Mapper
* public interface MyMapper {
* &#64;IgnoredList({
* &#64;Ignored(targets = { "firstProperty" } ),
* &#64;Ignored(targets = { "secondProperty" } )
* })
* HumanDto toHumanDto(Human human);
* }
* </code></pre>
* <pre><code class='java'>
* // Java 8 and later
* &#64;Mapper
* public interface MyMapper {
* &#64;Ignored(targets = { "firstProperty" } ),
* &#64;Ignored(targets = { "secondProperty" } )
* HumanDto toHumanDto(Human human);
* }
* </code></pre>
*
* @author Ivashin Aleksey
*/
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE })
public @interface IgnoredList {
/**
* The configuration of the bean attributes.
*
* @return The configuration of the bean attributes.
*/
Ignored[] value();
}

View File

@ -59,7 +59,7 @@ import java.lang.annotation.Target;
* }
* }
* </code></pre>
* <p>
*
* <pre><code class='java'>
* &#64;Mapper
* public interface CarMapper {
@ -81,8 +81,8 @@ import java.lang.annotation.Target;
public @interface InheritInverseConfiguration {
/**
* The name of the inverse mapping method to inherit the mappings from. Needs only to be specified in case more than
* one inverse method with matching source and target type exists.
* The name of the inverse mapping method to inherit the mappings from. Needs to be specified only in case more than
* one inverse method exists with a matching source and target type exists.
*
* @return The name of the inverse mapping method to inherit the mappings from.
*/

View File

@ -7,9 +7,10 @@ package org.mapstruct;
/**
* Strategy for handling injection. This is only used on annotated based component models such as CDI, Spring and
* JSR330.
* JSR330 / Jakarta.
*
* @author Kevin Grüneberg
* @author Lucas Resch
*/
public enum InjectionStrategy {
@ -17,5 +18,8 @@ public enum InjectionStrategy {
FIELD,
/** Annotations are written on the constructor **/
CONSTRUCTOR
CONSTRUCTOR,
/** A dedicated setter method is created */
SETTER
}

View File

@ -66,19 +66,38 @@ public @interface IterableMapping {
/**
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from an iterable of
* {@code String} to an iterable {@link Date} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the format will consider the specified locale when processing
* the date. Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String dateFormat() default "";
/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used to process the number format.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String numberFormat() default "";
/**
* Specifies the locale to be used when processing {@link #dateFormat()} or {@link #numberFormat()}.
* <p>
* The locale should be a plain tag representing the language, such as "en" for English, "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers.
*/
String locale() default "";
/**
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
* mappers (hand written of internal) qualify and result in an 'Ambiguous mapping methods found' error.

View File

@ -0,0 +1,115 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Allows the definition of Javadoc comments in the MapStruct <code>Mapper</code> generated class.
*
* <p>The annotation provides support for the usual Javadoc comments elements by defining analogous attributes.</p>
*
*
* <p>Please, note that at least one of these attributes must be specified.</p>
*
* <p>
* For instance, the following definition;
* </p>
* <pre><code class='java'>
* &#64;Javadoc(
* value = "This is the description",
* authors = { "author1", "author2" },
* deprecated = "Use {&#64;link OtherMapper} instead",
* since = "0.1"
* )
* </code></pre>
*
* <p>
* will generate:
* </p>
*
* <pre><code class='java'>
* /**
* * This is the description
* *
* * &#64;author author1
* * &#64;author author2
* *
* * &#64;deprecated Use {&#64;link OtherMapper} instead
* * &#64;since 0.1
* *&#47;
* </code></pre>
*
* <p>
* The entire Javadoc comment block can be passed directly:
* </p>
* <pre><code class='java'>
* &#64;Javadoc("This is the description\n"
* + "\n"
* + "&#64;author author1\n"
* + "&#64;author author2\n"
* + "\n"
* + "&#64;deprecated Use {&#64;link OtherMapper} instead\n"
* + "&#64;since 0.1\n"
* )
* </code></pre>
*
* <pre><code class='java'>
* // or using Text Blocks
* &#64;Javadoc(
* """
* This is the description
*
* &#64;author author1
* &#64;author author2
*
* &#64;deprecated Use {&#64;link OtherMapper} instead
* &#64;since 0.1
* """
* )
* </code></pre>
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Javadoc {
/**
* Main Javadoc comment text block.
*
* @return Main Javadoc comment text block.
*/
String value() default "";
/**
* List of authors of the code that it is being documented.
* <p>
* It will generate a list of the Javadoc tool comment element <code>&#64;author</code>
* with the different values and in the order provided.
*
* @return array of javadoc authors.
*/
String[] authors() default { };
/**
* Specifies that the functionality that is being documented is deprecated.
* <p>
* Corresponds to the <code>&#64;deprecated</code> Javadoc tool comment element.
*
* @return Deprecation message about the documented functionality
*/
String deprecated() default "";
/**
* Specifies the version since the functionality that is being documented is available.
* <p>
* Corresponds to the <code>&#64;since</code> Javadoc tool comment element.
*
* @return Version since the functionality is available
*/
String since() default "";
}

View File

@ -56,8 +56,12 @@ public @interface MapMapping {
/**
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from a map with key type
* {@code String} to an map with key type {@link Date} or vice-versa. Will be ignored for all other key types.
* <p>
* If the {@link #locale()} is specified, the format will consider the specified locale when processing the date.
* Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String keyDateFormat() default "";
@ -65,27 +69,50 @@ public @interface MapMapping {
* A format string as processable by {@link SimpleDateFormat} if the annotated method maps from a map with value
* type {@code String} to an map with value type {@link Date} or vice-versa. Will be ignored for all other value
* types.
* <p>
* If the {@link #locale()} is specified, the format will consider the specified locale when processing the date.
* Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String valueDateFormat() default "";
/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other key types.
* <p>
* If the {@link #locale()} is specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String keyNumberFormat() default "";
/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other value types.
* <p>
* If the {@link #locale()} is specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String valueNumberFormat() default "";
/**
* Specifies the locale to be used when processing {@link SimpleDateFormat} or {@link DecimalFormat} for key or
* value mappings in maps. The locale should be a plain tag representing the language, such as "en" for English,
* "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers in maps.
*/
String locale() default "";
/**
* A key value qualifier can be specified to aid the selection process of a suitable mapper. This is useful in
* case multiple mappers (hand written of internal) qualify and result in an 'Ambiguous mapping methods found'

View File

@ -15,6 +15,7 @@ import org.mapstruct.control.MappingControl;
import org.mapstruct.factory.Mappers;
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
import static org.mapstruct.SubclassExhaustiveStrategy.COMPILE_ERROR;
/**
* Marks an interface or abstract class as a mapper and activates the generation of a implementation of that type via
@ -35,7 +36,7 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
* </p>
* <pre><code class='java'>
* // we have MarkMapper (map field "mark" to field "name" to upper case)
* &#64;Mapper(componentModel = "spring")
* &#64;Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
* public class MarkMapper {
* public String mapMark(String mark) {
* return mark.toUpperCase();
@ -43,11 +44,11 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
* }
* // we have CarMapper
* &#64;Mapper(
* componentModel = "spring",
* componentModel = MappingConstants.ComponentModel.SPRING,
* uses = MarkMapper.class,
* injectionStrategy = InjectionStrategy.CONSTRUCTOR)
* public interface CarMapper {
* &#64;Mapping(source = "mark", target = "name")
* &#64;Mapping(target = "name", source = "mark")
* CarDto convertMap(CarEntity carEntity);
* }
* </code></pre>
@ -73,6 +74,7 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
* </code></pre>
*
* @author Gunnar Morling
* @see Javadoc
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
@ -141,14 +143,19 @@ public @interface Mapper {
* can be retrieved via {@code @Autowired}</li>
* <li>
* {@code jsr330}: the generated mapper is annotated with {@code @javax.inject.Named} and
* {@code @Singleton}, and can be retrieved via {@code @Inject}</li>
* {@code @Singleton}, and can be retrieved via {@code @Inject}.
* The annotations will either be from javax.inject or jakarta.inject,
* depending on which one is available, with javax.inject having precedence.</li>
* <li>
* {@code jakarta}: the generated mapper is annotated with {@code @jakarta.inject.Named} and
* {@code @Singleton}, and can be retrieved via {@code @Inject}.</li>
* </ul>
* The method overrides an unmappedTargetPolicy set in a central configuration set
* The method overrides a componentModel set in a central configuration set
* by {@link #config() }
*
* @return The component model for the generated mapper.
*/
String componentModel() default "default";
String componentModel() default MappingConstants.ComponentModel.DEFAULT;
/**
* Specifies the name of the implementation class. The {@code <CLASS_NAME>} will be replaced by the
@ -202,6 +209,32 @@ public @interface Mapper {
*/
NullValueMappingStrategy nullValueMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when {@code null} is passed as source argument value to an {@link IterableMapping} of
* this mapper. If unset, the strategy set with {@link #nullValueMappingStrategy()} will be applied. If neither
* strategy is configured, the strategy given via {@link MapperConfig#nullValueIterableMappingStrategy()} will be
* applied, using {@link NullValueMappingStrategy#RETURN_NULL} by default.
*
* @since 1.5
*
* @return The strategy to be applied when {@code null} is passed as source value to an {@link IterableMapping} of
* this mapper.
*/
NullValueMappingStrategy nullValueIterableMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when {@code null} is passed as source argument value to a {@link MapMapping} of this
* mapper. If unset, the strategy set with {@link #nullValueMappingStrategy()} will be applied. If neither strategy
* is configured, the strategy given via {@link MapperConfig#nullValueMapMappingStrategy()} will be applied, using
* {@link NullValueMappingStrategy#RETURN_NULL} by default.
*
* @since 1.5
*
* @return The strategy to be applied when {@code null} is passed as source value to a {@link MapMapping} of this
* mapper.
*/
NullValueMappingStrategy nullValueMapMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when a source bean property is {@code null} or not present. If no strategy is
* configured, the strategy given via {@link MapperConfig#nullValuePropertyMappingStrategy()} will be applied,
@ -237,6 +270,29 @@ public @interface Mapper {
*/
NullValueCheckStrategy nullValueCheckStrategy() default ON_IMPLICIT_CONVERSION;
/**
* Determines how to handle missing implementation for super classes when using the {@link SubclassMapping}.
*
* Can be overridden by the one on {@link BeanMapping}, but overrides {@link MapperConfig}.
*
* @return strategy to handle missing implementation combined with {@link SubclassMappings}.
*
* @since 1.5
*/
SubclassExhaustiveStrategy subclassExhaustiveStrategy() default COMPILE_ERROR;
/**
* Specifies the exception type to be thrown when a missing subclass implementation is detected
* in combination with {@link SubclassMappings}, based on the {@link #subclassExhaustiveStrategy()}.
* <p>
* This exception will only be thrown when the {@code subclassExhaustiveStrategy} is set to
* {@link SubclassExhaustiveStrategy#RUNTIME_EXCEPTION}.
*
* @return the exception class to throw when missing implementations are found.
* Defaults to {@link IllegalArgumentException}.
*/
Class<? extends Exception> subclassExhaustiveException() default IllegalArgumentException.class;
/**
* Determines whether to use field or constructor injection. This is only used on annotated based component models
* such as CDI, Spring and JSR 330.
@ -255,7 +311,7 @@ public @interface Mapper {
* Can be configured by the {@link MapperConfig#disableSubMappingMethodsGeneration()} as well.
* <p>
* Note: If you need to use {@code disableSubMappingMethodsGeneration} please contact the MapStruct team at
* <a href="http://mapstruct.org">mapstruct.org</a> or
* <a href="https://mapstruct.org">mapstruct.org</a> or
* <a href="https://github.com/mapstruct/mapstruct">github.com/mapstruct/mapstruct</a> to share what problem you
* are facing with the automatic sub-mapping generation.
*
@ -296,4 +352,40 @@ public @interface Mapper {
* @see org.mapstruct.control.MappingControl
*/
Class<? extends Annotation> mappingControl() default MappingControl.class;
/**
* Exception that should be thrown by the generated code if no mapping matches for enums.
* If no exception is configured, the exception given via {@link MapperConfig#unexpectedValueMappingException()}
* will be used, using {@link IllegalArgumentException} by default.
*
* <p>
* Note:
* <ul>
* <li>
* The defined exception should at least have a constructor with a {@link String} parameter.
* </li>
* <li>
* If the defined exception is a checked exception then the enum mapping methods should have that exception
* in the throws clause.
* </li>
* </ul>
*
* @return the exception that should be used in the generated code
*
* @since 1.4
*/
Class<? extends Exception> unexpectedValueMappingException() default IllegalArgumentException.class;
/**
* Flag indicating whether the addition of a time stamp in the {@code @Generated} annotation should be suppressed.
* i.e. not be added.
*
* The method overrides the flag set in a central configuration set by {@link #config()}
* or through an annotation processor option.
*
* @return whether the addition of a timestamp should be suppressed
*
* @since 1.5
*/
boolean suppressTimestampInGenerated() default false;
}

View File

@ -15,6 +15,7 @@ import org.mapstruct.control.MappingControl;
import org.mapstruct.factory.Mappers;
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
import static org.mapstruct.SubclassExhaustiveStrategy.COMPILE_ERROR;
/**
* Marks a class or interface as configuration source for generated mappers. This allows to share common configurations
@ -83,6 +84,8 @@ public @interface MapperConfig {
* their simple name rather than their fully-qualified name.
*
* @return classes to add in the imports of the generated implementation.
*
* @since 1.4
*/
Class<?>[] imports() default { };
@ -128,12 +131,17 @@ public @interface MapperConfig {
* can be retrieved via {@code @Autowired}</li>
* <li>
* {@code jsr330}: the generated mapper is annotated with {@code @javax.inject.Named} and
* {@code @Singleton}, and can be retrieved via {@code @Inject}</li>
* {@code @Singleton}, and can be retrieved via {@code @Inject}.
* The annotations will either be from javax.inject or jakarta.inject,
* depending on which one is available, with javax.inject having precedence.</li>
* <li>
* {@code jakarta}: the generated mapper is annotated with {@code @jakarta.inject.Named} and
* {@code @Singleton}, and can be retrieved via {@code @Inject}.</li>
* </ul>
*
* @return The component model for the generated mapper.
*/
String componentModel() default "default";
String componentModel() default MappingConstants.ComponentModel.DEFAULT;
/**
* Specifies the name of the implementation class. The {@code <CLASS_NAME>} will be replaced by the
@ -174,6 +182,28 @@ public @interface MapperConfig {
*/
NullValueMappingStrategy nullValueMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when {@code null} is passed as source argument value to an {@link IterableMapping}.
* If no strategy is configured, the strategy given via {@link #nullValueMappingStrategy()} will be applied, using
* {@link NullValueMappingStrategy#RETURN_NULL} by default.
*
* @since 1.5
*
* @return The strategy to be applied when {@code null} is passed as source value to an {@link IterableMapping}.
*/
NullValueMappingStrategy nullValueIterableMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when {@code null} is passed as source argument value to a {@link MapMapping}.
* If no strategy is configured, the strategy given via {@link #nullValueMappingStrategy()} will be applied, using
* {@link NullValueMappingStrategy#RETURN_NULL} by default.
*
* @since 1.5
*
* @return The strategy to be applied when {@code null} is passed as source value to a {@link MapMapping}.
*/
NullValueMappingStrategy nullValueMapMappingStrategy() default NullValueMappingStrategy.RETURN_NULL;
/**
* The strategy to be applied when a source bean property is {@code null} or not present. If no strategy is
* configured, {@link NullValuePropertyMappingStrategy#SET_TO_NULL} will be used by default.
@ -208,6 +238,29 @@ public @interface MapperConfig {
*/
NullValueCheckStrategy nullValueCheckStrategy() default ON_IMPLICIT_CONVERSION;
/**
* Determines how to handle missing implementation for super classes when using the {@link SubclassMapping}.
*
* Can be overridden by the one on {@link BeanMapping} or {@link Mapper}.
*
* @return strategy to handle missing implementation combined with {@link SubclassMappings}.
*
* @since 1.5
*/
SubclassExhaustiveStrategy subclassExhaustiveStrategy() default COMPILE_ERROR;
/**
* Specifies the exception type to be thrown when a missing subclass implementation is detected
* in combination with {@link SubclassMappings}, based on the {@link #subclassExhaustiveStrategy()}.
* <p>
* This exception will only be thrown when the {@code subclassExhaustiveStrategy} is set to
* {@link SubclassExhaustiveStrategy#RUNTIME_EXCEPTION}.
*
* @return the exception class to throw when missing implementations are found.
* Defaults to {@link IllegalArgumentException}.
*/
Class<? extends Exception> subclassExhaustiveException() default IllegalArgumentException.class;
/**
* Determines whether to use field or constructor injection. This is only used on annotated based component models
* such as CDI, Spring and JSR 330.
@ -228,7 +281,7 @@ public @interface MapperConfig {
* Can be overridden by {@link Mapper#disableSubMappingMethodsGeneration()}
* <p>
* Note: If you need to use {@code disableSubMappingMethodsGeneration} please contact the MapStruct team at
* <a href="http://mapstruct.org">mapstruct.org</a> or
* <a href="https://mapstruct.org">mapstruct.org</a> or
* <a href="https://github.com/mapstruct/mapstruct">github.com/mapstruct/mapstruct</a> to share what problem you
* are facing with the automatic sub-mapping generation.
*
@ -271,5 +324,38 @@ public @interface MapperConfig {
*/
Class<? extends Annotation> mappingControl() default MappingControl.class;
/**
* Exception that should be thrown by the generated code if no mapping matches for enums.
* If no exception is configured, {@link IllegalArgumentException} will be used by default.
*
* <p>
* Note:
* <ul>
* <li>
* The defined exception should at least have a constructor with a {@link String} parameter.
* </li>
* <li>
* If the defined exception is a checked exception then the enum mapping methods should have that exception
* in the throws clause.
* </li>
* </ul>
*
* @return the exception that should be used in the generated code
*
* @since 1.4
*/
Class<? extends Exception> unexpectedValueMappingException() default IllegalArgumentException.class;
/**
* Flag indicating whether the addition of a time stamp in the {@code @Generated} annotation should be suppressed.
* i.e. not be added.
*
* The method overrides the flag set through an annotation processor option.
*
* @return whether the addition of a timestamp should be suppressed
*
* @since 1.5
*/
boolean suppressTimestampInGenerated() default false;
}

View File

@ -20,7 +20,7 @@ import org.mapstruct.control.MappingControl;
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
/**
* Configures the mapping of one bean attribute or enum constant.
* Configures the mapping of one bean attribute.
* <p>
* The name of the mapped attribute or constant is to be specified via {@link #target()}. For mapped bean attributes it
* is assumed by default that the attribute has the same name in the source bean. Alternatively, one of
@ -54,7 +54,7 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
* <p><strong>Example 2:</strong> Mapping properties with different names</p>
* <pre><code class='java'>
* // We need map Human.companyName to HumanDto.company
* // we can use &#64;Mapping with parameters {@link #source()} and {@link #source()}
* // we can use &#64;Mapping with parameters {@link #source()} and {@link #target()}
* &#64;Mapper
* public interface HumanMapper {
* &#64;Mapping(source="companyName", target="company")
@ -136,9 +136,6 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
* }
* </code></pre>
*
* <b>IMPORTANT NOTE:</b> the enum mapping capability is deprecated and replaced by {@link ValueMapping} it
* will be removed in subsequent versions.
*
* @author Gunnar Morling
*/
@ -178,19 +175,38 @@ public @interface Mapping {
/**
* A format string as processable by {@link SimpleDateFormat} if the attribute is mapped from {@code String} to
* {@link Date} or vice-versa. Will be ignored for all other attribute types and when mapping enum constants.
* <p>
* If the {@link #locale()} is also specified, the format will consider the specified locale when processing
* the date. Otherwise, the system's default locale will be used.
*
* @return A date format string as processable by {@link SimpleDateFormat}.
* @see #locale()
*/
String dateFormat() default "";
/**
* A format string as processable by {@link DecimalFormat} if the annotated method maps from a
* {@link Number} to a {@link String} or vice-versa. Will be ignored for all other element types.
* <p>
* If the {@link #locale()} is also specified, the number format will be applied in the context of the given locale.
* Otherwise, the system's default locale will be used to process the number format.
*
* @return A decimal format string as processable by {@link DecimalFormat}.
* @see #locale()
*/
String numberFormat() default "";
/**
* Specifies the locale to be used when processing {@link #dateFormat()} or {@link #numberFormat()}.
* <p>
* The locale should be a plain tag representing the language, such as "en" for English, "de" for German, etc.
* <p>
* If no locale is specified, the system's default locale will be used.
*
* @return A string representing the locale to be used when formatting dates or numbers.
*/
String locale() default "";
/**
* A constant {@link String} based on which the specified target property is to be set.
* <p>
@ -218,6 +234,10 @@ public @interface Mapping {
* </li>
* </ol>
* <p>
* You can use {@link #qualifiedBy()} or {@link #qualifiedByName()} to force the use of a conversion method
* even when one would not apply. (e.g. {@code String} to {@code String})
* </p>
* <p>
* This attribute can not be used together with {@link #source()}, {@link #defaultValue()},
* {@link #defaultExpression()} or {@link #expression()}.
*
@ -283,9 +303,12 @@ public @interface Mapping {
/**
* Whether the property specified via {@link #target()} should be ignored by the generated mapping method or not.
* This can be useful when certain attributes should not be propagated from source or target or when properties in
* This can be useful when certain attributes should not be propagated from source to target or when properties in
* the target object are populated using a decorator and thus would be reported as unmapped target property by
* default.
* <p>
* If you have multiple properties to ignore,
* you can use the {@link Ignored} annotation instead and group them all at once.
*
* @return {@code true} if the given property should be ignored, {@code false} otherwise
*/
@ -295,6 +318,8 @@ public @interface Mapping {
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
* mapping methods (hand written or generated) qualify and thus would result in an 'Ambiguous mapping methods found'
* error. A qualifier is a custom annotation and can be placed on a hand written mapper class or a method.
* <p>
* Note that {@link #defaultValue()} usage will also be converted using this qualifier.
*
* @return the qualifiers
* @see Qualifier
@ -309,6 +334,8 @@ public @interface Mapping {
* Note that annotation-based qualifiers are generally preferable as they allow more easily to find references and
* are safe for refactorings, but name-based qualifiers can be a less verbose alternative when requiring a large
* number of qualifiers as no custom annotation types are needed.
* <p>
* Note that {@link #defaultValue()} usage will also be converted using this qualifier.
*
* @return One or more qualifier name(s)
* @see #qualifiedBy()
@ -316,6 +343,73 @@ public @interface Mapping {
*/
String[] qualifiedByName() default { };
/**
* A qualifier can be specified to aid the selection process of a suitable presence check method.
* This is useful in case multiple presence check methods qualify and thus would result in an
* 'Ambiguous presence check methods found' error.
* A qualifier is a custom annotation and can be placed on a hand written mapper class or a method.
* This is similar to the {@link #qualifiedBy()}, but it is only applied for {@link Condition} methods.
*
* @return the qualifiers
* @see Qualifier
* @see #qualifiedBy()
* @since 1.5
*/
Class<? extends Annotation>[] conditionQualifiedBy() default { };
/**
* String-based form of qualifiers for condition / presence check methods;
* When looking for a suitable presence check method for a given property, MapStruct will
* only consider those methods carrying directly or indirectly (i.e. on the class-level) a {@link Named} annotation
* for each of the specified qualifier names.
*
* This is similar like {@link #qualifiedByName()} but it is only applied for {@link Condition} methods.
* <p>
* Note that annotation-based qualifiers are generally preferable as they allow more easily to find references and
* are safe for refactorings, but name-based qualifiers can be a less verbose alternative when requiring a large
* number of qualifiers as no custom annotation types are needed.
* </p>
*
*
* @return One or more qualifier name(s)
* @see #conditionQualifiedBy()
* @see #qualifiedByName()
* @see Named
* @since 1.5
*/
String[] conditionQualifiedByName() default { };
/**
* A conditionExpression {@link String} based on which the specified property is to be checked
* whether it is present or not.
* <p>
* Currently, Java is the only supported "expression language" and expressions must be given in form of Java
* expressions using the following format: {@code java(<EXPRESSION>)}. For instance the mapping:
* <pre><code>
* &#64;Mapping(
* target = "someProp",
* conditionExpression = "java(s.getAge() &#60; 18)"
* )
* </code></pre>
* <p>
* will cause the following target property assignment to be generated:
* <pre><code>
* if (s.getAge() &#60; 18) {
* targetBean.setSomeProp( s.getSomeProp() );
* }
* </code></pre>
* <p>
* Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
* imported via {@link Mapper#imports()}.
* <p>
* This attribute can not be used together with {@link #expression()} or {@link #constant()}.
*
* @return An expression specifying a condition check for the designated property
*
* @since 1.5
*/
String conditionExpression() default "";
/**
* Specifies the result type of the mapping method to be used in case multiple mapping methods qualify.
*
@ -409,6 +503,4 @@ public @interface Mapping {
*/
Class<? extends Annotation> mappingControl() default MappingControl.class;
}

View File

@ -36,6 +36,14 @@ public final class MappingConstants {
*/
public static final String ANY_UNMAPPED = "<ANY_UNMAPPED>";
/**
* In an {@link ValueMapping} this represents any target that will be mapped to an
* {@link java.lang.IllegalArgumentException} which will be thrown at runtime.
* <p>
* NOTE: The value is only applicable to {@link ValueMapping#target()} and not to {@link ValueMapping#source()}.
*/
public static final String THROW_EXCEPTION = "<THROW_EXCEPTION>";
/**
* In an {@link EnumMapping} this represent the enum transformation strategy that adds a suffix to the source enum.
*
@ -66,4 +74,81 @@ public final class MappingConstants {
*/
public static final String STRIP_PREFIX_TRANSFORMATION = "stripPrefix";
/**
* In an {@link EnumMapping} this represent the enum transformation strategy that applies case transformation
* at the source.
*
* @since 1.5
*/
public static final String CASE_TRANSFORMATION = "case";
/**
* Specifies the component model constants to which the generated mapper should adhere.
* It can be used with the annotation {@link Mapper#componentModel()} or {@link MapperConfig#componentModel()}
*
* <p>
* <strong>Example:</strong>
* </p>
* <pre><code class='java'>
* // Spring component model
* &#64;Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
* </code></pre>
*
* @since 1.5.0
*/
public static final class ComponentModel {
private ComponentModel() {
}
/**
* The mapper uses no component model, instances are typically retrieved
* via {@link org.mapstruct.factory.Mappers#getMapper(java.lang.Class)}
*
*/
public static final String DEFAULT = "default";
/**
* The generated mapper is an application-scoped CDI bean and can be retrieved via @Inject.
* The annotations are either from {@code javax} or {@code jakarta}.
* Priority have the {@code javax} annotations.
* In case you want to only use Jakarta then use {@link #JAKARTA_CDI}.
*
* @see #JAKARTA_CDI
*/
public static final String CDI = "cdi";
/**
* The generated mapper is a Spring bean and can be retrieved via @Autowired
*
*/
public static final String SPRING = "spring";
/**
* The generated mapper is annotated with @Named and @Singleton, and can be retrieved via @Inject.
* The annotations are either from {@code javax.inject} or {@code jakarta.inject}.
* Priority have the {@code javax.inject} annotations.
* In case you want to only use Jakarta then use {@link #JAKARTA}.
*
* @see #JAKARTA
*/
public static final String JSR330 = "jsr330";
/**
* The generated mapper is annotated with @Named and @Singleton, and can be retrieved via @Inject.
* The annotations are from {@code jakarta.inject}.
* In case you want to use {@code javax.inject} then use {@link #JSR330}.
*
* @see #JSR330
*/
public static final String JAKARTA = "jakarta";
/**
* The generated mapper is an application-scoped Jakarta CDI bean and can be retrieved via @Inject.
* @see #CDI
*/
public static final String JAKARTA_CDI = "jakarta-cdi";
}
}

View File

@ -23,8 +23,8 @@ import java.lang.annotation.Target;
* &#64;Mapper
* public interface MyMapper {
* &#64;Mappings({
* &#64;Mapping(source = "first", target = "firstProperty"),
* &#64;Mapping(source = "second", target = "secondProperty")
* &#64;Mapping(target = "firstProperty", source = "first"),
* &#64;Mapping(target = "secondProperty", source = "second")
* })
* HumanDto toHumanDto(Human human);
* }
@ -33,8 +33,8 @@ import java.lang.annotation.Target;
* // Java 8 and later
* &#64;Mapper
* public interface MyMapper {
* &#64;Mapping(source = "first", target = "firstProperty"),
* &#64;Mapping(source = "second", target = "secondProperty")
* &#64;Mapping(target = "firstProperty", source = "first"),
* &#64;Mapping(target = "secondProperty", source = "second")
* HumanDto toHumanDto(Human human);
* }
* </code></pre>

View File

@ -14,7 +14,7 @@ import java.lang.annotation.Target;
* Marks mapping methods with the given qualifier name. Can be used to qualify a single method or all methods of a given
* type by specifying this annotation on the type level.
* <p>
* Will be used to to select the correct mapping methods when mapping a bean property type, element of an iterable type
* Will be used to select the correct mapping methods when mapping a bean property type, element of an iterable type
* or the key/value of a map type.
* <p>
* Example (both methods of {@code Titles} are capable to convert a string, but the ambiguity is resolved by applying

View File

@ -0,0 +1,15 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
/**
* To be used as a default value for enum class annotation elements.
*
* @author Ben Zegveld
* @since 1.6
*/
enum NullEnum {
}

View File

@ -8,7 +8,7 @@ package org.mapstruct;
/**
* Strategy for dealing with null source values.
*
* <b>Note:</b> This strategy is not in effect when the a specific source presence check method is defined
* <b>Note:</b> This strategy is not in effect when a specific source presence check method is defined
* in the service provider interface (SPI).
* <p>
* <b>Note</b>: some types of mappings (collections, maps), in which MapStruct is instructed to use a getter or adder

View File

@ -10,7 +10,7 @@ package org.mapstruct;
* {@link NullValuePropertyMappingStrategy} can be defined on {@link MapperConfig}, {@link Mapper}, {@link BeanMapping}
* and {@link Mapping}.
* Precedence is arranged in the reverse order. So {@link Mapping} will override {@link BeanMapping}, will
* overide {@link Mapper}
* override {@link Mapper}
*
* The enum <b>only applies to update methods</b>: methods that update a pre-existing target (annotated with
* {@code @}{@link MappingTarget}).

View File

@ -21,6 +21,7 @@ import java.lang.annotation.Target;
* <li>{@link IterableMapping#qualifiedBy() }</li>
* <li>{@link MapMapping#keyQualifiedBy() }</li>
* <li>{@link MapMapping#valueQualifiedBy() }</li>
* <li>{@link SubclassMapping#qualifiedBy() }</li>
* </ul>
* <p><strong>Example:</strong></p>
* <pre><code class='java'>

View File

@ -0,0 +1,74 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation marks a method as a <em>check method</em> to check if a source parameter needs to be mapped.
* <p>
* By default, source parameters are checked against {@code null}, unless they are primitives.
* <p>
* Check methods have to return {@code boolean}.
* The following parameters are accepted for the presence check methods:
* <ul>
* <li>The mapping source parameter</li>
* <li>{@code @}{@link Context} parameter</li>
* </ul>
*
* <strong>Note:</strong> The usage of this annotation is <em>mandatory</em>
* for a method to be considered as a source check method.
*
* <pre><code class='java'>
* public class PresenceCheckUtils {
*
* &#64;SourceParameterCondition
* public static boolean isDefined(Car car) {
* return car != null &#38;&#38; car.getId() != null;
* }
* }
*
* &#64;Mapper(uses = PresenceCheckUtils.class)
* public interface CarMapper {
*
* CarDto map(Car car);
* }
* </code></pre>
*
* The following implementation of {@code CarMapper} will be generated:
*
* <pre><code class='java'>
* public class CarMapperImpl implements CarMapper {
*
* &#64;Override
* public CarDto map(Car car) {
* if ( !PresenceCheckUtils.isDefined( car ) ) {
* return null;
* }
*
* CarDto carDto = new CarDto();
*
* carDto.setId( car.getId() );
* // ...
*
* return carDto;
* }
* }
* </code></pre>
*
* @author Filip Hrisafov
* @since 1.6
* @see Condition @Condition
*/
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.CLASS)
@Condition(appliesTo = ConditionStrategy.SOURCE_PARAMETERS)
public @interface SourceParameterCondition {
}

View File

@ -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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation marks a <em>presence check method</em> parameter as a source property name parameter.
* <p>
* This parameter enables conditional filtering based on source property name at run-time.
* Parameter must be of type {@link String} and can be present only in {@link Condition} method.
* </p>
*
* @author Oliver Erhart
* @since 1.6
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.CLASS)
public @interface SourcePropertyName {
}

View File

@ -0,0 +1,28 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
/**
* Strategy for dealing with subclassMapping annotated methods.
*
* @since 1.5
* @author Ben Zegveld
*/
public enum SubclassExhaustiveStrategy {
/**
* If there is no valid constructor or known method to create the return value of a with `@SubclassMapping`
* annotated mapping then a compilation error will be thrown.
*/
COMPILE_ERROR,
/**
* If there is no valid constructor or known method to create the return value of a with `@SubclassMapping`
* annotated mapping then an {@link IllegalArgumentException} will be thrown if a call is made with a type for which
* there is no {@link SubclassMapping} available.
*/
RUNTIME_EXCEPTION;
}

View File

@ -0,0 +1,110 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.mapstruct.util.Experimental;
/**
* Configures the mapping to handle hierarchy of the source type.
* <p>
* The subclass to be mapped is to be specified via {@link #source()}.
* The subclass to map to is to be specified via {@link #target()}.
* </p>
* <p>
* This annotation can be combined with &#64;Mapping annotations.
* </p>
*
* <pre><code class='java'>
* &#64;Mapper
* public interface MyMapper {
* &#64;SubclassMapping (target = TargetSubclass.class, source = SourceSubclass.class)
* TargetParent mapParent(SourceParent parent);
*
* TargetSubclass mapSubclass(SourceSubclass subInstant);
* }
* </code></pre>
* Below follow examples of the implementation for the mapParent method.
* <strong>Example 1:</strong> For parents that cannot be created. (e.g. abstract classes or interfaces)
* <pre><code class='java'>
* // generates
* &#64;Override
* public TargetParent mapParent(SourceParent parent) {
* if (parent instanceof SourceSubclass) {
* return mapSubclass( (SourceSubclass) parent );
* }
* else {
* throw new IllegalArgumentException("Not all subclasses are supported for this mapping. Missing for "
* + parent.getClass());
* }
* }
* </code></pre>
* <strong>Example 2:</strong> For parents that can be created. (e.g. normal classes or interfaces with
* &#64;Mapper( uses = ObjectFactory.class ) )
* <pre><code class='java'>
* // generates
* &#64;Override
* public TargetParent mapParent(SourceParent parent) {
* TargetParent targetParent1;
* if (parent instanceof SourceSubclass) {
* targetParent1 = mapSubclass( (SourceSubclass) parent );
* }
* else {
* targetParent1 = new TargetParent();
* // ...
* }
* }
* </code></pre>
*
* @author Ben Zegveld
* @since 1.5
*/
@Repeatable(value = SubclassMappings.class)
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Experimental
public @interface SubclassMapping {
/**
* @return the source subclass to check for before using the default mapping as fallback.
*/
Class<?> source();
/**
* @return the target subclass to map the source to.
*/
Class<?> target();
/**
* A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple
* mapping methods (hand written or generated) qualify and thus would result in an 'Ambiguous mapping methods found'
* error. A qualifier is a custom annotation and can be placed on a hand written mapper class or a method.
*
* @return the qualifiers
* @see Qualifier
*/
Class<? extends Annotation>[] qualifiedBy() default {};
/**
* String-based form of qualifiers; When looking for a suitable mapping method for a given property, MapStruct will
* only consider those methods carrying directly or indirectly (i.e. on the class-level) a {@link Named} annotation
* for each of the specified qualifier names.
* <p>
* Note that annotation-based qualifiers are generally preferable as they allow more easily to find references and
* are safe for refactorings, but name-based qualifiers can be a less verbose alternative when requiring a large
* number of qualifiers as no custom annotation types are needed.
*
* @return One or more qualifier name(s)
* @see #qualifiedBy()
* @see Named
*/
String[] qualifiedByName() default {};
}

View File

@ -0,0 +1,58 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.mapstruct.util.Experimental;
/**
* Configures the SubclassMappings of several subclasses.
* <p>
* <strong>TIP: When using java 8 or later, you can omit the @SubclassMappings
* Wrapper annotation and directly specify several @SubclassMapping annotations
* on one method.</strong>
* </p>
* <p>These two examples are equal.
* </p>
* <pre><code class='java'>
* // before java 8
* &#64;Mapper
* public interface MyMapper {
* &#64;SubclassMappings({
* &#64;SubclassMapping(source = FirstSub.class, target = FirstTargetSub.class),
* &#64;SubclassMapping(source = SecondSub.class, target = SecondTargetSub.class)
* })
* ParentTarget toParentTarget(Parent parent);
* }
* </code></pre>
* <pre><code class='java'>
* // java 8 and later
* &#64;Mapper
* public interface MyMapper {
* &#64;SubclassMapping(source = First.class, target = FirstTargetSub.class),
* &#64;SubclassMapping(source = SecondSub.class, target = SecondTargetSub.class)
* ParentTarget toParentTarget(Parent parent);
* }
* </code></pre>
*
* @author Ben Zegveld
* @since 1.5
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.CLASS)
@Experimental
public @interface SubclassMappings {
/**
* @return the subclassMappings to apply.
*/
SubclassMapping[] value();
}

View File

@ -0,0 +1,25 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation marks a <em>presence check method</em> parameter as a target property name parameter.
* <p>
* This parameter enables conditional filtering based on target property name at run-time.
* Parameter must be of type {@link String} and can be present only in {@link Condition} method.
* </p>
* @author Nikola Ivačič
* @since 1.6
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.CLASS)
public @interface TargetPropertyName {
}

View File

@ -18,18 +18,16 @@ import java.lang.annotation.Target;
* <ol>
* <li>Enumeration to Enumeration</li>
* </ol>
* <p>
* <B>Example 1:</B>
* <b>Example 1:</b>
*
* <pre>
* <code>
* public enum OrderType { RETAIL, B2B, EXTRA, STANDARD, NORMAL }
* <pre><code>
* public enum OrderType { RETAIL, B2B, C2C, EXTRA, STANDARD, NORMAL }
*
* public enum ExternalOrderType { RETAIL, B2B, SPECIAL, DEFAULT }
*
* &#64;ValueMapping(source = "EXTRA", target = "SPECIAL"),
* &#64;ValueMapping(source = "STANDARD", target = "DEFAULT"),
* &#64;ValueMapping(source = "NORMAL", target = "DEFAULT")
* &#64;ValueMapping(target = "SPECIAL", source = "EXTRA"),
* &#64;ValueMapping(target = "DEFAULT", source = "STANDARD"),
* &#64;ValueMapping(target = "DEFAULT", source = "NORMAL")
* ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
* </code>
* Mapping result:
@ -45,13 +43,9 @@ import java.lang.annotation.Target;
* +---------------------+----------------------------+
* </pre>
*
* MapStruct will <B>WARN</B> on incomplete mappings. However, if for some reason no match is found an
* {@link java.lang.IllegalStateException} will be thrown.
* <p>
* <B>Example 2:</B>
* <b>Example 2:</b>
*
* <pre>
* <code>
* <pre><code>
* &#64;ValueMapping( source = MappingConstants.NULL, target = "DEFAULT" ),
* &#64;ValueMapping( source = "STANDARD", target = MappingConstants.NULL ),
* &#64;ValueMapping( source = MappingConstants.ANY_REMAINING, target = "SPECIAL" )
@ -70,11 +64,27 @@ import java.lang.annotation.Target;
* +---------------------+----------------------------+
* </pre>
*
* <b>Example 3:</b>
*
* MapStruct will <B>WARN</B> on incomplete mappings. However, if for some reason no match is found, an
* {@link java.lang.IllegalStateException} will be thrown. This compile-time error can be avoided by
* using {@link MappingConstants#THROW_EXCEPTION} for {@link ValueMapping#target()}. It will result an
* {@link java.lang.IllegalArgumentException} at runtime.
* <pre><code>
* &#64;ValueMapping( source = "STANDARD", target = "DEFAULT" ),
* &#64;ValueMapping( source = "C2C", target = MappingConstants.THROW_EXCEPTION )
* ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
* </code>
* Mapping result:
* {@link java.lang.IllegalArgumentException} with the error message:
* Unexpected enum constant: C2C
* </pre>
*
* @author Sjaak Derksen
*/
@Repeatable(ValueMappings.class)
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
public @interface ValueMapping {
/**
* The source value constant to use for this mapping.
@ -104,6 +114,7 @@ public @interface ValueMapping {
* <ol>
* <li>enum constant name</li>
* <li>{@link MappingConstants#NULL}</li>
* <li>{@link MappingConstants#THROW_EXCEPTION}</li>
* </ol>
*
* @return The target value.

View File

@ -22,8 +22,8 @@ import java.lang.annotation.Target;
* &#64;Mapper
* public interface GenderMapper {
* &#64;ValueMappings({
* &#64;ValueMapping(source = "MALE", target = "M"),
* &#64;ValueMapping(source = "FEMALE", target = "F")
* &#64;ValueMapping(target = "M", source = "MALE"),
* &#64;ValueMapping(target = "F", source = "FEMALE")
* })
* GenderDto mapToDto(Gender gender);
* }
@ -32,15 +32,15 @@ import java.lang.annotation.Target;
* //Java 8 and later
* &#64;Mapper
* public interface GenderMapper {
* &#64;ValueMapping(source = "MALE", target = "M"),
* &#64;ValueMapping(source = "FEMALE", target = "F")
* &#64;ValueMapping(target = "M", source = "MALE"),
* &#64;ValueMapping(target = "F", source = "FEMALE")
* GenderDto mapToDto(Gender gender);
* }
* </code></pre>
*
* @author Sjaak Derksen
*/
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface ValueMappings {

View File

@ -87,6 +87,8 @@ import java.lang.annotation.Target;
* </code></pre>
*
* @author Sjaak Derksen
*
* @since 1.4
*/
@Retention(RetentionPolicy.CLASS)
@Repeatable(MappingControls.class)
@ -130,7 +132,7 @@ public @interface MappingControl {
* This means if source type and target type are of the same type, MapStruct will not perform
* any mappings anymore and assign the target to the source direct.
* <p>
* An exception are types from the package {@link java}, which will be mapped always directly.
* An exception are types from the package {@code java}, which will be mapped always directly.
*
* @since 1.4
*/

View File

@ -14,6 +14,8 @@ import java.lang.annotation.Target;
* Allows multiple {@link MappingControl} on a class declaration.
*
* @author Sjaak Derksen
*
* @since 1.4
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.ANNOTATION_TYPE)

View File

@ -13,6 +13,6 @@
* This package contains several annotations which allow to configure how mapper interfaces are generated.
* </p>
*
* @see <a href="http://mapstruct.org/">MapStruct reference documentation</a>
* @see <a href="https://mapstruct.org/">MapStruct reference documentation</a>
*/
package org.mapstruct;

View File

@ -7,7 +7,7 @@ package org.mapstruct.factory;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mapstruct.test.model.Foo;
import org.mapstruct.test.model.SomeClass;

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
@ -34,6 +34,18 @@
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<!-- Needed here so references to MapStruct Gem classes can be resolved during JavaDoc generation -->
<dependency>
<groupId>org.mapstruct.tools.gem</groupId>
<artifactId>gem-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
@ -54,7 +66,7 @@
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<outputDirectory>${project.build.directory}/freemarker-unpacked</outputDirectory>
<includes>META-INF/LICENSE.txt,META-INF/NOTICE.txt</includes>
<includes>META-INF/LICENSE</includes>
</artifactItem>
</artifactItems>
</configuration>
@ -88,7 +100,7 @@
<doctitle>MapStruct ${project.version}</doctitle>
<windowtitle>MapStruct ${project.version}</windowtitle>
<bottom>
<![CDATA[Copyright &copy; ${project.inceptionYear}-{currentYear} <a href="http://mapstruct.org/">MapStruct Authors</a>; All rights reserved. Released under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache Software License 2.0</a>.]]>
<![CDATA[Copyright &copy; ${project.inceptionYear}-{currentYear} <a href="https://mapstruct.org/">MapStruct Authors</a>; All rights reserved. Released under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache Software License 2.0</a>.]]>
</bottom>
<groups>
@ -96,18 +108,16 @@
<title>MapStruct API</title>
<packages>org.mapstruct*</packages>
</group>
<group>
<title>MapStruct Processor SPI</title>
<packages>org.mapstruct.ap.spi*</packages>
</group>
<group>
<title>MapStruct Processor</title>
<packages>org.mapstruct.ap*</packages>
</group>
</groups>
<doclet>org.jboss.apiviz.APIviz</doclet>
<docletArtifact>
<groupId>org.jboss.apiviz</groupId>
<artifactId>apiviz</artifactId>
<version>1.3.2.GA</version>
</docletArtifact>
<useStandardDocletOptions>true</useStandardDocletOptions>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
@ -116,6 +126,23 @@
<version>true</version>
<author>true</author>
<keywords>true</keywords>
<!--
There is a bug in JDK 11 (https://bugs.openjdk.java.net/browse/JDK-8215291) which doesn't work correctly when searching and adds undefined.
It has been fixed since JDK 12, but not yet backported to JDK 11 (https://bugs.openjdk.java.net/browse/JDK-8244171).
One workaround is https://stackoverflow.com/a/57284322/1115491.
-->
<bottom>
<![CDATA[
<script>
if (typeof useModuleDirectories !== 'undefined') {
useModuleDirectories = false;
}
</script>
]]>
</bottom>
<additionalJOption>--allow-script-in-comments</additionalJOption>
</configuration>
<executions>
<execution>
@ -135,7 +162,7 @@
<descriptor>${basedir}/src/main/assembly/dist.xml</descriptor>
</descriptors>
<finalName>mapstruct-${project.version}</finalName>
<tarLongFileMode>gnu</tarLongFileMode>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
@ -156,4 +183,21 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>jdk-11-or-newer</id>
<activation>
<jdk>[11</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

View File

@ -42,11 +42,7 @@
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>target/freemarker-unpacked/META-INF/NOTICE.txt</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>target/freemarker-unpacked/META-INF/LICENSE.txt</source>
<source>target/freemarker-unpacked/META-INF/LICENSE</source>
<outputDirectory>etc/freemarker</outputDirectory>
</file>
<file>

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
@ -21,9 +21,9 @@
<name>MapStruct Documentation</name>
<properties>
<asciidoctorj.pdf.version>1.5.3</asciidoctorj.pdf.version>
<asciidoctorj.version>1.6.2</asciidoctorj.version>
<jruby.version>9.2.6.0</jruby.version>
<asciidoctorj.pdf.version>1.6.0</asciidoctorj.pdf.version>
<asciidoctorj.version>2.5.1</asciidoctorj.version>
<jruby.version>9.2.17.0</jruby.version>
</properties>
<dependencies>
</dependencies>
@ -33,7 +33,7 @@
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.6.0</version>
<version>2.1.0</version>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
@ -52,7 +52,6 @@
</dependency>
</dependencies>
<configuration>
<sourceHighlighter>coderay</sourceHighlighter>
<sourceDocumentName>mapstruct-reference-guide.asciidoc</sourceDocumentName>
<attributes>
<mapstructVersion>${project.version}</mapstructVersion>

View File

@ -88,7 +88,7 @@ Default expressions are a combination of default values and expressions. They wi
The same warnings and restrictions apply to default expressions that apply to expressions. Only Java is supported, and MapStruct will not validate the expression at generation-time.
The example below demonstrates how two source properties can be mapped to one target:
The example below demonstrates how a default expression can be used to set a value when the source attribute is not present (e.g. is `null`):
.Mapping method using a default expression
====
@ -110,6 +110,51 @@ public interface SourceTargetMapper {
The example demonstrates how to use defaultExpression to set an `ID` field if the source field is null, this could be used to take the existing `sourceId` from the source object if it is set, or create a new `Id` if it isn't. Please note that the fully qualified package name is specified because MapStruct does not take care of the import of the `UUID` class (unless its used otherwise explicitly in the `SourceTargetMapper`). This can be resolved by defining imports on the @Mapper annotation (see <<expressions>>).
[[sub-class-mappings]]
=== Subclass Mapping
When both input and result types have an inheritance relation, you would want the correct specialization be mapped to the matching specialization.
Suppose an `Apple` and a `Banana`, which are both specializations of `Fruit`.
.Specifying the sub class mappings of a fruit mapping
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface FruitMapper {
@SubclassMapping( source = AppleDto.class, target = Apple.class )
@SubclassMapping( source = BananaDto.class, target = Banana.class )
Fruit map( FruitDto source );
}
----
====
If you would just use a normal mapping both the `AppleDto` and the `BananaDto` would be made into a `Fruit` object, instead of an `Apple` and a `Banana` object.
By using the subclass mapping an `AppleDtoToApple` mapping will be used for `AppleDto` objects, and an `BananaDtoToBanana` mapping will be used for `BananaDto` objects.
If you try to map a `GrapeDto` it would still turn it into a `Fruit`.
In the case that the `Fruit` is an abstract class or an interface, you would get a compile error.
To allow mappings for abstract classes or interfaces you need to set the `subclassExhaustiveStrategy` to `RUNTIME_EXCEPTION`, you can do this at the `@MapperConfig`, `@Mapper` or `@BeanMapping` annotations. If you then pass a `GrapeDto` an `IllegalArgumentException` will be thrown because it is unknown how to map a `GrapeDto`.
Adding the missing (`@SubclassMapping`) for it will fix that.
<<selection-based-on-qualifiers>> can be used to further control which methods may be chosen to map a specific subclass. For that, you will need to use one of `SubclassMapping#qualifiedByName` or `SubclassMapping#qualifiedBy`.
[TIP]
====
If the mapping method for the subclasses does not exist it will be created and any other annotations on the fruit mapping method will be inherited by the newly generated mappings.
====
[NOTE]
====
Combining `@SubclassMapping` with update methods is not supported.
If you try to use subclass mappings there will be a compile error.
The same issue exists for the `@Context` and `@TargetType` parameters.
====
[[determining-result-type]]
=== Determining the result type
@ -144,7 +189,7 @@ public class FruitFactory {
----
====
So, which `Fruit` must be factorized in the mapping method `Fruit map(FruitDto source);`? A `Banana` or an `Apple`? Here's were the `@BeanMapping#resultType` comes in handy. It controls the factory method to select, or in absence of a factory method, the return type to create.
So, which `Fruit` must be factorized in the mapping method `Fruit map(FruitDto source);`? A `Banana` or an `Apple`? Here's where the `@BeanMapping#resultType` comes in handy. It controls the factory method to select, or in absence of a factory method, the return type to create.
[TIP]
====
@ -161,13 +206,36 @@ The mechanism is also present on iterable mapping and map mapping. `@IterableMap
MapStruct offers control over the object to create when the source argument of the mapping method equals `null`. By default `null` will be returned.
However, by specifying `nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT` on `@BeanMapping`, `@IterableMapping`, `@MapMapping`, or globally on `@Mapper` or `@MappingConfig`, the mapping result can be altered to return empty *default* values. This means for:
However, by specifying `nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT` on `@BeanMapping`, `@IterableMapping`, `@MapMapping`, or globally on `@Mapper` or `@MapperConfig`, the mapping result can be altered to return empty *default* values. This means for:
* *Bean mappings*: an 'empty' target bean will be returned, with the exception of constants and expressions, they will be populated when present.
* *Iterables / Arrays*: an empty iterable will be returned.
* *Maps*: an empty map will be returned.
The strategy works in a hierarchical fashion. Setting `nullValueMappingStrategy` on mapping method level will override `@Mapper#nullValueMappingStrategy`, and `@Mapper#nullValueMappingStrategy` will override `@MappingConfig#nullValueMappingStrategy`.
The strategy works in a hierarchical fashion. Setting `nullValueMappingStrategy` on mapping method level will override `@Mapper#nullValueMappingStrategy`, and `@Mapper#nullValueMappingStrategy` will override `@MapperConfig#nullValueMappingStrategy`.
[[mapping-result-for-null-collection-or-map-arguments]]
=== Controlling mapping result for 'null' collection or map arguments
With <<mapping-result-for-null-arguments>> it is possible to control how the return type should be constructed when the source argument of the mapping method is `null`.
That is applied for all mapping methods (bean, iterable or map mapping methods).
However, MapStruct also offers a more dedicated way to control how collections / maps should be mapped.
e.g. return default (empty) collections / maps, but return `null` for beans.
For collections (iterables) this can be controlled through:
* `MapperConfig#nullValueIterableMappingStrategy`
* `Mapper#nullValueIterableMappingStrategy`
* `IterableMapping#nullValueMappingStrategy`
For maps this can be controlled through:
* `MapperConfig#nullValueMapMappingStrategy`
* `Mapper#nullValueMapMappingStrategy`
* `MapMapping#nullValueMappingStrategy`
How the value of the `NullValueMappingStrategy` is applied is the same as in <<mapping-result-for-null-arguments>>
[[mapping-result-for-null-properties]]
@ -179,13 +247,13 @@ By default the target property will be set to null.
However:
1. By specifying `nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT` on `@Mapping`, `@BeanMapping`, `@Mapper` or `@MappingConfig`, the mapping result can be altered to return *default* values.
For `List` MapStruct generates an `ArrayList`, for `Map` a `HashMap`, for arrays an empty array, for `String` `""` and for primitive / boxed types a representation of `false` or `0`.
1. By specifying `nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT` on `@Mapping`, `@BeanMapping`, `@Mapper` or `@MapperConfig`, the mapping result can be altered to return *default* values.
For `List` MapStruct generates an `ArrayList`, for `Map` a `LinkedHashMap`, for arrays an empty array, for `String` `""` and for primitive / boxed types a representation of `false` or `0`.
For all other objects an new instance is created. Please note that a default constructor is required. If not available, use the `@Mapping#defaultValue`.
2. By specifying `nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE` on `@Mapping`, `@BeanMapping`, `@Mapper` or `@MappingConfig`, the mapping result will be equal to the original value of the `@MappingTarget` annotated target.
2. By specifying `nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE` on `@Mapping`, `@BeanMapping`, `@Mapper` or `@MapperConfig`, the mapping result will be equal to the original value of the `@MappingTarget` annotated target.
The strategy works in a hierarchical fashion. Setting `Mapping#nullValuePropertyMappingStrategy` on mapping level will override `nullValuePropertyMappingStrategy` on mapping method level will override `@Mapper#nullValuePropertyMappingStrategy`, and `@Mapper#nullValuePropertyMappingStrategy` will override `@MappingConfig#nullValuePropertyMappingStrategy`.
The strategy works in a hierarchical fashion. Setting `nullValuePropertyMappingStrategy` on mapping method level will override `@Mapper#nullValuePropertyMappingStrategy`, and `@Mapper#nullValuePropertyMappingStrategy` will override `@MapperConfig#nullValuePropertyMappingStrategy`.
[NOTE]
====
@ -213,7 +281,7 @@ First calling a mapping method on the source property is not protected by a null
The option `nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS` will always include a null check when source is non primitive, unless a source presence checker is defined on the source bean.
The strategy works in a hierarchical fashion. `@Mapping#nullValueCheckStrategy` will override `@BeanMapping#nullValueCheckStrategy`, `@BeanMapping#nullValueCheckStrategy` will override `@Mapper#nullValueCheckStrategy` and `@Mapper#nullValueCheckStrategy` will override `@MappingConfig#nullValueCheckStrategy`.
The strategy works in a hierarchical fashion. `@Mapping#nullValueCheckStrategy` will override `@BeanMapping#nullValueCheckStrategy`, `@BeanMapping#nullValueCheckStrategy` will override `@Mapper#nullValueCheckStrategy` and `@Mapper#nullValueCheckStrategy` will override `@MapperConfig#nullValueCheckStrategy`.
[[source-presence-check]]
=== Source presence checking
@ -226,9 +294,247 @@ The source presence checker name can be changed in the MapStruct service provide
[NOTE]
====
Some types of mappings (collections, maps), in which MapStruct is instructed to use a getter or adder as target accessor see `CollectionMappingStrategy`, MapStruct will always generate a source property
Some types of mappings (collections, maps), in which MapStruct is instructed to use a getter or adder as target accessor (see `CollectionMappingStrategy`), MapStruct will always generate a source property
null check, regardless the value of the `NullValueCheckStrategy` to avoid addition of `null` to the target collection or map.
====
[[conditional-mapping]]
=== Conditional Mapping
Conditional Mapping is a type of <<source-presence-check>>.
The difference is that it allows users to write custom condition methods that will be invoked to check if a property needs to be mapped or not.
Conditional mapping can also be used to check if a source parameter should be mapped or not.
A custom condition method for properties is a method that is annotated with `org.mapstruct.Condition` and returns `boolean`.
A custom condition method for source parameters is annotated with `org.mapstruct.SourceParameterCondition`, `org.mapstruct.Condition(appliesTo = org.mapstruct.ConditionStrategy#SOURCE_PARAMETERS)` or meta-annotated with `Condition(appliesTo = ConditionStrategy#SOURCE_PARAMETERS)`
e.g. if you only want to map a String property when it is not `null` and not empty then you can do something like:
.Mapper using custom condition check method
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface CarMapper {
CarDto carToCarDto(Car car);
@Condition
default boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
}
----
====
The generated mapper will look like:
.Custom condition check in generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car) {
if ( car == null ) {
return null;
}
CarDto carDto = new CarDto();
if ( isNotEmpty( car.getOwner() ) ) {
carDto.setOwner( car.getOwner() );
}
// Mapping of other properties
return carDto;
}
}
----
====
When using this in combination with an update mapping method it will replace the `null-check` there, for example:
.Update mapper using custom condition check method
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface CarMapper {
CarDto carToCarDto(Car car, @MappingTarget CarDto carDto);
@Condition
default boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
}
----
====
The generated update mapper will look like:
.Custom condition check in generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car, CarDto carDto) {
if ( car == null ) {
return carDto;
}
if ( isNotEmpty( car.getOwner() ) ) {
carDto.setOwner( car.getOwner() );
} else {
carDto.setOwner( null );
}
// Mapping of other properties
return carDto;
}
}
----
====
Additionally `@TargetPropertyName` or `@SourcePropertyName` of type `java.lang.String` can be used in custom condition check method:
.Mapper using custom condition check method with `@TargetPropertyName` and `@SourcePropertyName`
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface CarMapper {
@Mapping(target = "owner", source = "ownerName")
CarDto carToCarDto(Car car, @MappingTarget CarDto carDto);
@Condition
default boolean isNotEmpty(
String value,
@TargetPropertyName String targetPropertyName,
@SourcePropertyName String sourcePropertyName
) {
if ( targetPropertyName.equals( "owner" )
&& sourcePropertyName.equals( "ownerName" ) ) {
return value != null
&& !value.isEmpty()
&& !value.equals( value.toLowerCase() );
}
return value != null && !value.isEmpty();
}
}
----
====
The generated mapper with `@TargetPropertyName` and `@SourcePropertyName` will look like:
.Custom condition check in generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car, CarDto carDto) {
if ( car == null ) {
return carDto;
}
if ( isNotEmpty( car.getOwner(), "owner", "ownerName" ) ) {
carDto.setOwner( car.getOwner() );
} else {
carDto.setOwner( null );
}
// Mapping of other properties
return carDto;
}
}
----
====
[IMPORTANT]
====
If there is a custom `@Condition` method applicable for the property it will have a precedence over a presence check method in the bean itself.
====
[NOTE]
====
Methods annotated with `@Condition` in addition to the value of the source property can also have the source parameter as an input.
`@TargetPropertyName` and `@SourcePropertyName` parameters can only be used in `@Condition` methods.
====
<<selection-based-on-qualifiers>> is also valid for `@Condition` methods.
In order to use a more specific condition method you will need to use one of `Mapping#conditionQualifiedByName` or `Mapping#conditionQualifiedBy`.
If we want to only map cars that have an id provided then we can do something like:
.Mapper using custom condition source parameter check method
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface CarMapper {
CarDto carToCarDto(Car car);
@SourceParameterCondition
default boolean hasCar(Car car) {
return car != null && car.getId() != null;
}
}
----
====
The generated mapper will look like:
.Custom condition source parameter check generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car) {
if ( !hasCar( car ) ) {
return null;
}
CarDto carDto = new CarDto();
carDto.setOwner( car.getOwner() );
// Mapping of other properties
return carDto;
}
}
----
====
[[exceptions]]
=== Exceptions

View File

@ -46,6 +46,8 @@ In case of bi-directional mappings, e.g. from entity to DTO and from DTO to enti
Use the annotation `@InheritInverseConfiguration` to indicate that a method shall inherit the inverse configuration of the corresponding reverse method.
In the example below, there is no need to write the inverse mapping manually. Think of a case where there are several mappings, so writing the inverse ones can be cumbersome and error prone.
.Inverse mapping method inheriting its configuration and ignoring some of them
====
[source, java, linenums]
@ -54,7 +56,7 @@ Use the annotation `@InheritInverseConfiguration` to indicate that a method shal
@Mapper
public interface CarMapper {
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(target = "seatCount", source = "numberOfSeats")
CarDto carToDto(Car car);
@InheritInverseConfiguration

View File

@ -248,9 +248,8 @@ All before/after-mapping methods that *can* be applied to a mapping method *will
The order of the method invocation is determined primarily by their variant:
1. `@BeforeMapping` methods without an `@MappingTarget` parameter are called before any null-checks on source
parameters and constructing a new target bean.
2. `@BeforeMapping` methods with an `@MappingTarget` parameter are called after constructing a new target bean.
1. `@BeforeMapping` methods without parameters, a `@MappingTarget` parameter or a `@TargetType` parameter are called before any null-checks on source parameters and constructing a new target bean.
2. `@BeforeMapping` methods with a `@MappingTarget` parameter are called after constructing a new target bean.
3. `@AfterMapping` methods are called at the end of the mapping method before the last `return` statement.
Within those groups, the method invocations are ordered by their location of definition:
@ -262,4 +261,11 @@ Within those groups, the method invocations are ordered by their location of def
*Important:* the order of methods declared within one type can not be guaranteed, as it depends on the compiler and the processing environment implementation.
*Important:* when using a builder, the `@AfterMapping` annotated method must have the builder as `@MappingTarget` annotated parameter so that the method is able to modify the object going to be build. The `build` method is called when the `@AfterMapping` annotated method scope finishes. MapStruct will not call the `@AfterMapping` annotated method if the real target is used as `@MappingTarget` annotated parameter.
[NOTE]
====
Before/After-mapping methods can also be used with builders:
* `@BeforeMapping` methods with a `@MappingTarget` parameter of the real target will not be invoked because it is only available after the mapping was already performed.
* To be able to modify the object that is going to be built, the `@AfterMapping` annotated method must have the builder as `@MappingTarget` annotated parameter. The `build` method is called when the `@AfterMapping` annotated method scope finishes.
* The `@AfterMapping` annotated method can also have the real target as `@TargetType` or `@MappingTarget`. It will be invoked after the real target was built (first the methods annotated with `@TargetType`, then the methods annotated with `@MappingTarget`)
====

View File

@ -1,8 +1,19 @@
[[using-spi]]
== Using the MapStruct SPI
To use a custom SPI implementation, it must be located in a separate JAR file together with a file named after the SPI (e.g. `org.mapstruct.ap.spi.AccessorNamingStrategy`) in `META-INF/services/` with the fully qualified name of your custom implementation as content (e.g. `org.mapstruct.example.CustomAccessorNamingStrategy`). This JAR file needs to be added to the annotation processor classpath (i.e. add it next to the place where you added the mapstruct-processor jar).
[NOTE]
====
It might also be necessary to add the jar to your IDE's annotation processor factory path. Otherwise you might get an error stating that it cannot be found, while a run using your build tool does succeed.
====
=== Custom Accessor Naming Strategy
MapStruct offers the possibility to override the `AccessorNamingStrategy` via the Service Provide Interface (SPI). A nice example is the use of the fluent API on the source object `GolfPlayer` and `GolfPlayerDto` below.
SPI name: `org.mapstruct.ap.spi.AccessorNamingStrategy`
MapStruct offers the possibility to override the `AccessorNamingStrategy` via the Service Provider Interface (SPI). A nice example is the use of the fluent API on the source object `GolfPlayer` and `GolfPlayerDto` below.
.Source object GolfPlayer with fluent API.
====
@ -60,7 +71,7 @@ public class GolfPlayerDto {
public GolfPlayerDto withName(String name) {
this.name = name;
return this
return this;
}
}
----
@ -121,14 +132,14 @@ public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy
====
The `CustomAccessorNamingStrategy` makes use of the `DefaultAccessorNamingStrategy` (also available in mapstruct-processor) and relies on that class to leave most of the default behaviour unchanged.
To use a custom SPI implementation, it must be located in a separate JAR file together with the file `META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy` with the fully qualified name of your custom implementation as content (e.g. `org.mapstruct.example.CustomAccessorNamingStrategy`). This JAR file needs to be added to the annotation processor classpath (i.e. add it next to the place where you added the mapstruct-processor jar).
[TIP]
Fore more details: The example above is present in our examples repository (https://github.com/mapstruct/mapstruct-examples).
[mapping-exclusion-provider]
[[mapping-exclusion-provider]]
=== Mapping Exclusion Provider
SPI name: `org.mapstruct.ap.spi.MappingExclusionProvider`
MapStruct offers the possibility to override the `MappingExclusionProvider` via the Service Provider Interface (SPI).
A nice example is to not allow MapStruct to create an automatic sub-mapping for a certain type,
i.e. MapStruct will not try to generate an automatic sub-mapping method for an excluded type.
@ -177,16 +188,12 @@ include::{processor-ap-test}/nestedbeans/exclusions/custom/CustomMappingExclusio
----
====
To use a custom SPI implementation, it must be located in a separate JAR file
together with the file `META-INF/services/org.mapstruct.ap.spi.MappingExclusionProvider` with the fully qualified name of your custom implementation as content
(e.g. `org.mapstruct.example.CustomMappingExclusionProvider`).
This JAR file needs to be added to the annotation processor classpath
(i.e. add it next to the place where you added the mapstruct-processor jar).
[[custom-builder-provider]]
=== Custom Builder Provider
SPI name: org.mapstruct.ap.spi.BuilderProvider
MapStruct offers the possibility to override the `DefaultProvider` via the Service Provider Interface (SPI).
A nice example is to provide support for a custom builder strategy.
@ -202,7 +209,9 @@ include::{processor-ap-main}/spi/NoOpBuilderProvider.java[tag=documentation]
[[custom-enum-naming-strategy]]
=== Custom Enum Naming Strategy
MapStruct offers the possibility to override the `EnumNamingStrategy` via the Service Provider Interface (SPI).
SPI name: `org.mapstruct.ap.spi.EnumMappingStrategy`
MapStruct offers the possibility to override the `EnumMappingStrategy` via the Service Provider Interface (SPI).
This can be used when you have certain enums that follow some conventions within your organization.
For example all enums which implement an interface named `CustomEnumMarker` are prefixed with `CUSTOM_`
and the default value for them when mapping from `null` is `UNSPECIFIED`
@ -250,15 +259,15 @@ public interface CheeseTypeMapper {
----
====
This can be achieved with implementing the SPI `org.mapstruct.ap.spi.EnumNamingStrategy` as in the following example.
Heres an implemented `org.mapstruct.ap.spi.EnumNamingStrategy`:
This can be achieved with implementing the SPI `org.mapstruct.ap.spi.EnumMappingStrategy` as in the following example.
Heres an implemented `org.mapstruct.ap.spi.EnumMappingStrategy`:
.Custom enum naming strategy
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
public class CustomEnumNamingStrategy extends DefaultEnumNamingStrategy {
public class CustomEnumMappingStrategy extends DefaultEnumMappingStrategy {
@Override
public String getDefaultNullEnumConstant(TypeElement enumType) {
@ -349,6 +358,8 @@ public class CheeseTypeMapperImpl implements CheeseTypeMapper {
[[custom-enum-transformation-strategy]]
=== Custom Enum Transformation Strategy
SPI name: `org.mapstruct.ap.spi.EnumTransformationStrategy`
MapStruct offers the possibility to other transformations strategies by implementing `EnumTransformationStrategy` via the Service Provider Interface (SPI).
A nice example is to provide support for a custom transformation strategy.
@ -360,3 +371,54 @@ A nice example is to provide support for a custom transformation strategy.
include::{processor-ap-test}/value/nametransformation/CustomEnumTransformationStrategy.java[tag=documentation]
----
====
[[additional-supported-options-provider]]
=== Additional Supported Options Provider
SPI name: `org.mapstruct.ap.spi.AdditionalSupportedOptionsProvider`
MapStruct offers the ability to pass through declared compiler args (or "options") provided to the MappingProcessor
to the individual SPIs, by implementing `AdditionalSupportedOptionsProvider` via the Service Provider Interface (SPI).
.Custom Additional Supported Options Provider that declares `myorg.custom.defaultNullEnumConstant` as an option to pass through
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
include::{processor-ap-test}/additionalsupportedoptions/CustomAdditionalSupportedOptionsProvider.java[tag=documentation]
----
====
The value of this option is provided by including an `arg` to the `compilerArgs` tag when defining your custom SPI
implementation.
.Example maven configuration with additional options
====
[source, maven, linenums]
[subs="verbatim,attributes"]
----
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.myorg</groupId>
<artifactId>custom-spi-impl</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amyorg.custom.defaultNullEnumConstant=MISSING</arg>
</compilerArgs>
</configuration>
----
====
Your custom SPI implementations can then access this configured value via `MapStructProcessingEnvironment#getOptions()`.
.Accessing your custom options
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
include::{processor-ap-test}/additionalsupportedoptions/UnknownEnumMappingStrategy.java[tag=documentation]
----
====

View File

@ -0,0 +1,192 @@
[[third-party-api-integration]]
== Third-party API integration
[[non-shipped-annotations]]
=== Non-shipped annotations
There are various use-cases you must resolve ambiguity for MapStruct to use a correct piece of code.
However, the primary goal of MapStruct is to focus on bean mapping without polluting the entity code.
For that reason, MapStruct is flexible enough to interact with already defined annotations from third-party libraries.
The requirement to enable this behavior is to match the _name_ of such annotation.
Hence, we say that annotation can be _from any package_.
The annotations _named_ `@ConstructorProperties` and `@Default` are currently examples of this kind of annotation.
[WARNING]
====
If such named third-party annotation exists, it does not guarantee its `@Target` matches with the intended placement.
Be aware of placing a third-party annotation just for sake of mapping is not recommended as long as it might lead to unwanted side effects caused by that library.
====
A very common case is that no third-party dependency imported to your project provides such annotation or is inappropriate for use as already described.
In such cases create your own annotation, for example:
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
package foo.support.mapstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.CLASS)
public @interface Default {
}
----
====
[[lombok]]
=== Lombok
MapStruct works together with https://projectlombok.org/[Project Lombok] as of MapStruct 1.2.0.Beta1 and Lombok 1.16.14.
MapStruct takes advantage of generated getters, setters, and constructors and uses them to generate the mapper implementations.
Be reminded that the generated code by Lombok might not always be compatible with the expectations from the individual mappings.
In such a case, either Mapstruct mapping must be changed or Lombok must be configured accordingly using https://projectlombok.org/features/configuration[`lombok.config`] for mutual synergy.
[WARNING]
====
Lombok 1.18.16 introduces a breaking change (https://projectlombok.org/changelog[changelog]).
The additional annotation processor `lombok-mapstruct-binding` (https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding[Maven]) must be added otherwise MapStruct stops working with Lombok.
This resolves the compilation issues of Lombok and MapStruct modules.
[source, xml]
----
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
----
====
==== Set up
The set up using Maven or Gradle does not differ from what is described in <<setup>>. Additionally, you need to provide Lombok dependencies.
.Maven configuration
====
[source, xml, linenums]
[subs="verbatim,attributes"]
----
<properties>
<org.mapstruct.version>{mapstructVersion}</org.mapstruct.version>
<org.projectlombok.version>1.18.16</org.projectlombok.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!-- lombok dependency should not end up on classpath -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<!-- additional annotation processor required as of Lombok 1.18.16 -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
----
====
.Gradle configuration (3.4 and later)
====
[source, groovy, linenums]
[subs="verbatim,attributes"]
----
dependencies {
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
compileOnly "org.projectlombok:lombok:1.18.16"
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
annotationProcessor "org.projectlombok:lombok:1.18.16"
}
----
====
The usage combines what you already know from <<defining-mapper>> and Lombok.
.Usage of MapStruct with Lombok
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Data
public class Source {
private String test;
}
public class Target {
private Long testing;
public Long getTesting() {
return testing;
}
public void setTesting( Long testing ) {
this.testing = testing;
}
}
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
@Mapping( source = "test", target = "testing" )
Target toTarget( Source s );
}
----
====
A working example can be found on the GitHub project https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-lombok[mapstruct-lombok].

View File

@ -76,7 +76,7 @@ It will not work with older versions.
Add the following to your Gradle build file in order to enable MapStruct:
.Gradle configuration (3.4 and later)
.Gradle configuration
====
[source, groovy, linenums]
[subs="verbatim,attributes"]
@ -84,14 +84,9 @@ Add the following to your Gradle build file in order to enable MapStruct:
...
plugins {
...
id 'net.ltgt.apt' version '0.20'
id "com.diffplug.eclipse.apt" version "3.26.0" // Only for Eclipse
}
// You can integrate with your IDEs.
// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides
apply plugin: 'net.ltgt.apt-idea'
apply plugin: 'net.ltgt.apt-eclipse'
dependencies {
...
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
@ -103,33 +98,6 @@ dependencies {
...
----
====
.Gradle (3.3 and older)
====
[source, groovy, linenums]
[subs="verbatim,attributes"]
----
...
plugins {
...
id 'net.ltgt.apt' version '0.20'
}
// You can integrate with your IDEs.
// See more details: https://github.com/tbroyer/gradle-apt-plugin#usage-with-ides
apply plugin: 'net.ltgt.apt-idea'
apply plugin: 'net.ltgt.apt-eclipse'
dependencies {
...
compile "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
// If you are using mapstruct in test code
testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
}
...
----
====
You can find a complete example in the https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-on-gradle[mapstruct-examples] project on GitHub.
@ -162,7 +130,7 @@ You can find a complete example in the https://github.com/mapstruct/mapstruct-ex
The MapStruct code generator can be configured using _annotation processor options_.
When invoking javac directly, these options are passed to the compiler in the form _-Akey=value_. When using MapStruct via Maven, any processor options can be passed using an `options` element within the configuration of the Maven processor plug-in like this:
When invoking javac directly, these options are passed to the compiler in the form _-Akey=value_. When using MapStruct via Maven, any processor options can be passed using `compilerArgs` within the configuration of the Maven processor plug-in like this:
.Maven configuration
====
@ -187,15 +155,15 @@ When invoking javac directly, these options are passed to the compiler in the fo
<!-- due to problem in maven-compiler-plugin, for verbose mode add showWarnings -->
<showWarnings>true</showWarnings>
<compilerArgs>
<compilerArg>
<arg>
-Amapstruct.suppressGeneratorTimestamp=true
</compilerArg>
<compilerArg>
</arg>
<arg>
-Amapstruct.suppressGeneratorVersionInfoComment=true
</compilerArg>
<compilerArg>
</arg>
<arg>
-Amapstruct.verbose=true
</compilerArg>
</arg>
</compilerArgs>
</configuration>
</plugin>
@ -247,9 +215,11 @@ suppressGeneratorVersionInfoComment`
Supported values are:
* `default`: the mapper uses no component model, instances are typically retrieved via `Mappers#getMapper(Class)`
* `cdi`: the generated mapper is an application-scoped CDI bean and can be retrieved via `@Inject`
* `cdi`: the generated mapper is an application-scoped (from javax.enterprise.context or jakarta.enterprise.context, depending on which one is available with javax.inject having priority) CDI bean and can be retrieved via `@Inject`
* `spring`: the generated mapper is a singleton-scoped Spring bean and can be retrieved via `@Autowired`
* `jsr330`: the generated mapper is annotated with {@code @Named} and can be retrieved via `@Inject`, e.g. using Spring
* `jsr330`: the generated mapper is annotated with {@code @Named} and can be retrieved via `@Inject` (from javax.inject or jakarta.inject, depending which one is available with javax.inject having priority), e.g. using Spring
* `jakarta`: the generated mapper is annotated with {@code @Named} and can be retrieved via `@Inject` (from jakarta.inject), e.g. using Spring
* `jakarta-cdi`: the generated mapper is an application-scoped (from jakarta.enterprise.context) CDI bean and can be retrieved via `@Inject`
If a component model is given for a specific mapper via `@Mapper#componentModel()`, the value from the annotation takes precedence.
|`default`
@ -277,23 +247,79 @@ Supported values are:
* `IGNORE`: unmapped target properties are ignored
If a policy is given for a specific mapper via `@Mapper#unmappedTargetPolicy()`, the value from the annotation takes precedence.
If a policy is given for a specific bean mapping via `@BeanMapping#unmappedTargetPolicy()`, it takes precedence over both `@Mapper#unmappedTargetPolicy()` and the option.
|`WARN`
|`mapstruct.unmappedSourcePolicy`
|The default reporting policy to be applied in case an attribute of the source object of a mapping method is not populated with a target value.
Supported values are:
* `ERROR`: any unmapped source property will cause the mapping code generation to fail
* `WARN`: any unmapped source property will cause a warning at build time
* `IGNORE`: unmapped source properties are ignored
If a policy is given for a specific mapper via `@Mapper#unmappedSourcePolicy()`, the value from the annotation takes precedence.
If a policy is given for a specific bean mapping via `@BeanMapping#ignoreUnmappedSourceProperties()`, it takes precedence over both `@Mapper#unmappedSourcePolicy()` and the option.
|`IGNORE`
|`mapstruct.
disableBuilders`
|If set to `true`, then MapStruct will not use builder patterns when doing the mapping. This is equivalent to doing `@Mapper( builder = @Builder( disableBuilder = true ) )` for all of your mappers.
|`false`
|`mapstruct.nullValueIterableMappingStrategy`
|The strategy to be applied when `null` is passed as a source value to an iterable mapping.
Supported values are:
* `RETURN_NULL`: if `null` is passed as a source value, then `null` will be returned
* `RETURN_DEFAULT`: if `null` is passed then a default value (empty collection) will be returned
If a strategy is given for a specific mapper via `@Mapper#nullValueIterableMappingStrategy()`, the value from the annotation takes precedence.
If a strategy is given for a specific iterable mapping via `@IterableMapping#nullValueMappingStrategy()`, it takes precedence over both `@Mapper#nullValueIterableMappingStrategy()` and the option.
|`RETURN_NULL`
|`mapstruct.nullValueMapMappingStrategy`
|The strategy to be applied when `null` is passed as a source value to a map mapping.
Supported values are:
* `RETURN_NULL`: if `null` is passed as a source value, then `null` will be returned
* `RETURN_DEFAULT`: if `null` is passed then a default value (empty map) will be returned
If a strategy is given for a specific mapper via `@Mapper#nullValueMapMappingStrategy()`, the value from the annotation takes precedence.
If a strategy is given for a specific map mapping via `@MapMapping#nullValueMappingStrategy()`, it takes precedence over both `@Mapper#nullValueMapMappingStrategy()` and the option.
|`RETURN_NULL`
|===
=== Using MapStruct on Java 9
=== Using MapStruct with the Java Module System
MapStruct can be used with Java 9 (JPMS), support for it is experimental.
MapStruct can be used with Java 9 and higher versions.
A core theme of Java 9 is the modularization of the JDK. One effect of this is that a specific module needs to be enabled for a project in order to use the `javax.annotation.Generated` annotation. `@Generated` is added by MapStruct to generated mapper classes to tag them as generated code, stating the date of generation, the generator version etc.
To allow usage of the `@Generated` annotation `java.annotation.processing.Generated` (part of the `java.compiler` module) can be enabled.
To allow usage of the `@Generated` annotation the module _java.xml.ws.annotation_ must be enabled. When using Maven, this can be done like this:
=== IDE Integration
export MAVEN_OPTS="--add-modules java.xml.ws.annotation"
There are optional MapStruct plugins for IntelliJ and Eclipse that allow you to have additional completion support (and more) in the annotations.
If the `@Generated` annotation is not available, MapStruct will detect this situation and not add it to generated mappers.
==== IntelliJ
[NOTE]
=====
In Java 9 `java.annotation.processing.Generated` was added (part of the `java.compiler` module),
if this annotation is available then it will be used.
=====
The https://plugins.jetbrains.com/plugin/10036-mapstruct-support[MapStruct IntelliJ] plugin offers assistance in projects that use MapStruct.
Some features include:
* Code completion in `target`, `source`, `expression`
* Go To Declaration for properties in `target` and `source`
* Find Usages of properties in `target` and `source`
* Refactoring support
* Errors and Quick Fixes
==== Eclipse
The https://marketplace.eclipse.org/content/mapstruct-eclipse-plugin[MapStruct Eclipse] Plugin offers assistance in projects that use MapStruct.
Some features include:
* Code completion in `target` and `source`
* Quick Fixes

View File

@ -16,11 +16,11 @@ To create a mapper simply define a Java interface with the required mapping meth
@Mapper
public interface CarMapper {
@Mapping(source = "make", target = "manufacturer")
@Mapping(source = "numberOfSeats", target = "seatCount")
@Mapping(target = "manufacturer", source = "make")
@Mapping(target = "seatCount", source = "numberOfSeats")
CarDto carToCarDto(Car car);
@Mapping(source = "name", target = "fullName")
@Mapping(target = "fullName", source = "name")
PersonDto personToPersonDto(Person person);
}
----
@ -39,7 +39,8 @@ The property name as defined in the http://www.oracle.com/technetwork/java/javas
====
[TIP]
====
By means of the `@BeanMapping(ignoreByDefault = true)` the default behavior will be *explicit mapping*, meaning that all mappings have to be specified by means of the `@Mapping` and no warnings will be issued on missing target properties.
By means of the `@BeanMapping(ignoreByDefault = true)` the default behavior will be *explicit mapping*, meaning that all mappings (including nested ones) have to be specified by means of the `@Mapping` and no warnings will be issued on missing target properties.
This allows to ignore all fields, except the ones that are explicitly defined through `@Mapping`.
====
[TIP]
====
@ -125,7 +126,7 @@ Collection-typed attributes with the same element type will be copied by creatin
MapStruct takes all public properties of the source and target types into account. This includes properties declared on super-types.
[[mapping-composition]]
=== Mapping Composition (experimental)
=== Mapping Composition
MapStruct supports the use of meta annotations. The `@Mapping` annotation supports now `@Target` with `ElementType#ANNOTATION_TYPE` in addition to `ElementType#METHOD`. This allows `@Mapping` to be used on other (user defined) annotations for re-use purposes. For example:
====
@ -163,7 +164,7 @@ public interface StorageMapper {
Still, they do have some properties in common. The `@ToEntity` assumes both target beans `ShelveEntity` and `BoxEntity` have properties: `"id"`, `"creationDate"` and `"name"`. It furthermore assumes that the source beans `ShelveDto` and `BoxDto` always have a property `"groupName"`. This concept is also known as "duck-typing". In other words, if it quacks like duck, walks like a duck its probably a duck.
This feature is still experimental. Error messages are not mature yet: the method on which the problem occurs is displayed, as well as the concerned values in the `@Mapping` annotation. However, the composition aspect is not visible. The messages are "as if" the `@Mapping` would be present on the concerned method directly.
Error messages are not mature yet: the method on which the problem occurs is displayed, as well as the concerned values in the `@Mapping` annotation. However, the composition aspect is not visible. The messages are "as if" the `@Mapping` would be present on the concerned method directly.
Therefore, the user should use this feature with care, especially when uncertain when a property is always present.
A more typesafe (but also more verbose) way would be to define base classes / interfaces on the target bean and the source bean and use `@InheritConfiguration` to achieve the same result (see <<mapping-configuration-inheritance>>).
@ -236,8 +237,8 @@ MapStruct also supports mapping methods with several source parameters. This is
@Mapper
public interface AddressMapper {
@Mapping(source = "person.description", target = "description")
@Mapping(source = "address.houseNo", target = "houseNumber")
@Mapping(target = "description", source = "person.description")
@Mapping(target = "houseNumber", source = "address.houseNo")
DeliveryAddressDto personAndAddressToDeliveryAddressDto(Person person, Address address);
}
----
@ -267,8 +268,8 @@ MapStruct also offers the possibility to directly refer to a source parameter.
@Mapper
public interface AddressMapper {
@Mapping(source = "person.description", target = "description")
@Mapping(source = "hn", target = "houseNumber")
@Mapping(target = "description", source = "person.description")
@Mapping(target = "houseNumber", source = "hn")
DeliveryAddressDto personAndAddressToDeliveryAddressDto(Person person, Integer hn);
}
----
@ -301,7 +302,7 @@ If you don't want explicitly name all properties from nested source bean, you ca
The generated code will map every property from `CustomerDto.record` to `Customer` directly, without need to manually name any of them.
The same goes for `Customer.account`.
When there are conflicts, these can be resolved by explicitely defining the mapping. For instance in the example above. `name` occurs in `CustomerDto.record` and in `CustomerDto.account`. The mapping `@Mapping( target = "name", source = "record.name" )` resolves this conflict.
When there are conflicts, these can be resolved by explicitly defining the mapping. For instance in the example above. `name` occurs in `CustomerDto.record` and in `CustomerDto.account`. The mapping `@Mapping( target = "name", source = "record.name" )` resolves this conflict.
This "target this" notation can be very useful when mapping hierarchical objects to flat objects and vice versa (`@InheritInverseConfiguration`).
@ -368,7 +369,7 @@ public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );
@Mapping(source = "customerName", target = "name")
@Mapping(target = "name", source = "customerName")
Customer toCustomer(CustomerDto customerDto);
@InheritInverseConfiguration
@ -420,8 +421,11 @@ If a Builder exists for a certain type, then that builder will be used for the m
The default implementation of the `BuilderProvider` assumes the following:
* The type has a parameterless public static builder creation method that returns a builder.
So for example `Person` has a public static method that returns `PersonBuilder`.
* The type has either
** A parameterless public static builder creation method that returns a builder.
e.g. `Person` has a public static method that returns `PersonBuilder`.
** A public static inner class with the name having the suffix "Builder", and a public no-args constructor
e.g. `Person` has an inner class `PersonBuilder` with a public no-args constructor.
* The builder type has a parameterless public method (build method) that returns the type being built.
In our example `PersonBuilder` has a method returning `Person`.
* In case there are multiple build methods, MapStruct will look for a method called `build`, if such method exists
@ -445,6 +449,11 @@ The <<object-factories>> are also considered for the builder type.
E.g. If an object factory exists for our `PersonBuilder` then this factory would be used instead of the builder creation method.
======
[NOTE]
======
Detected builders influence `@BeforeMapping` and `@AfterMapping` behavior. See <<customizing-mappings-with-before-and-after>> for more information.
======
.Person with Builder example
====
[source, java, linenums]
@ -516,7 +525,8 @@ public class PersonMapperImpl implements PersonMapper {
Supported builder frameworks:
* https://projectlombok.org/[Lombok] - requires having the Lombok classes in a separate module. See for more information https://github.com/rzwitserloot/lombok/issues/1538[rzwitserloot/lombok#1538]
* https://projectlombok.org/[Lombok] - It is required to have the Lombok classes in a separate module.
See for more information at https://github.com/rzwitserloot/lombok/issues/1538[rzwitserloot/lombok#1538] and to set up Lombok with MapStruct, refer to <<lombok>>.
* https://github.com/google/auto/blob/master/value/userguide/index.md[AutoValue]
* https://immutables.github.io/[Immutables] - When Immutables are present on the annotation processor path then the `ImmutablesAccessorNamingStrategy` and `ImmutablesBuilderProvider` would be used by default
* https://github.com/google/FreeBuilder[FreeBuilder] - When FreeBuilder is present on the annotation processor path then the `FreeBuilderAccessorNamingStrategy` would be used by default.
@ -526,7 +536,7 @@ Otherwise, you would need to write a custom `BuilderProvider`
[TIP]
====
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.
In case you want to disable using builders then you can pass the MapStruct processor option `mapstruct.disableBuilders` to the compiler. e.g. `-Amapstruct.disableBuilders=true`.
====
[[mapping-with-constructors]]
@ -537,10 +547,10 @@ When doing a mapping MapStruct checks if there is a builder for the type being m
If there is no builder, then MapStruct looks for a single accessible constructor.
When there are multiple constructors then the following is done to pick the one which should be used:
* If a constructor is annotated with an annotation named `@Default` (from any package) it will be used.
* If a constructor is annotated with an annotation _named_ `@Default` (from any package, see <<non-shipped-annotations>>) it will be used.
* If a single public constructor exists then it will be used to construct the object, and the other non public constructors will be ignored.
* If a parameterless constructor exists then it will be used to construct the object, and the other constructors will be ignored.
* If there are multiple eligible constructors then there will be a compilation error due to ambigious constructors. In order to break the ambiquity an annotation named `@Default` (from any package) can used.
* If there are multiple eligible constructors then there will be a compilation error due to ambiguous constructors. In order to break the ambiguity an annotation _named_ `@Default` (from any package, see <<non-shipped-annotations>>) can used.
.Deciding which constructor to use
====
@ -585,7 +595,7 @@ public class Van {
====
When using a constructor then the names of the parameters of the constructor will be used and matched to the target properties.
When the constructor has an annotation named `@ConstructorProperties` (from any package) then this annotation will be used to get the names of the parameters.
When the constructor has an annotation _named_ `@ConstructorProperties` (from any package, see <<non-shipped-annotations>>) then this annotation will be used to get the names of the parameters.
[NOTE]
====
@ -649,3 +659,208 @@ public class PersonMapperImpl implements PersonMapper {
}
----
====
[[mapping-map-to-bean]]
=== Mapping Map to Bean
There are situations when a mapping from a `Map<String, ???>` into a specific bean is needed.
MapStruct offers a transparent way of doing such a mapping by using the target bean properties (or defined through `Mapping#source`) to extract the values from the map.
Such a mapping looks like:
.Example classes for mapping map to bean
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
public class Customer {
private Long id;
private String name;
//getters and setter omitted for brevity
}
@Mapper
public interface CustomerMapper {
@Mapping(target = "name", source = "customerName")
Customer toCustomer(Map<String, String> map);
}
----
====
.Generated mapper for mapping map to bean
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CustomerMapperImpl implements CustomerMapper {
@Override
public Customer toCustomer(Map<String, String> map) {
// ...
if ( map.containsKey( "id" ) ) {
customer.setId( Integer.parseInt( map.get( "id" ) ) );
}
if ( map.containsKey( "customerName" ) ) {
customer.setName( map.get( "customerName" ) );
}
// ...
}
}
----
====
[NOTE]
====
All existing rules about mapping between different types and using other mappers defined with `Mapper#uses` or custom methods in the mappers are applied.
i.e. You can map from `Map<String, Integer>` where for each property a conversion from `Integer` into the respective property will be needed.
====
[WARNING]
====
When a raw map or a map that does not have a String as a key is used, then a warning will be generated.
The warning is not generated if the map itself is mapped into some other target property directly as is.
====
[[adding-annotations]]
=== Adding annotations
Other frameworks sometimes requires you to add annotations to certain classes so that they can easily detect the mappers.
Using the `@AnnotateWith` annotation you can generate an annotation at the specified location.
For example Apache Camel has a `@Converter` annotation which you can apply to generated mappers using the `@AnnotateWith` annotation.
.AnnotateWith source example
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
@AnnotateWith(
value = Converter.class,
elements = @AnnotateWith.Element( name = "generateBulkLoader", booleans = true )
)
public interface MyConverter {
@AnnotateWith( Converter.class )
DomainObject map( DtoObject dto );
}
----
====
.AnnotateWith generated mapper
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Converter( generateBulkLoader = true )
public class MyConverterImpl implements MyConverter {
@Converter
public DomainObject map( DtoObject dto ) {
// default mapping behaviour
}
}
----
====
[[javadoc]]
=== Adding Javadoc comments
MapStruct provides support for defining Javadoc comments in the generated mapper implementation using the
`org.mapstruct.Javadoc` annotation.
This functionality could be relevant especially in situations where certain Javadoc standards need to be met or
to deal with Javadoc validation constraints.
The `@Javadoc` annotation defines attributes for the different Javadoc elements.
Consider the following example:
.Javadoc annotation example
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
@Javadoc(
value = "This is the description",
authors = { "author1", "author2" },
deprecated = "Use {@link OtherMapper} instead",
since = "0.1"
)
public interface MyAnnotatedWithJavadocMapper {
//...
}
----
====
.Javadoc annotated generated mapper
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
/**
* This is the description
*
* @author author1
* @author author2
*
* @deprecated Use {@link OtherMapper} instead
* @since 0.1
*/
public class MyAnnotatedWithJavadocMapperImpl implements MyAnnotatedWithJavadocMapper {
//...
}
----
====
The entire Javadoc comment block can be provided directly as well.
.Javadoc annotation example with the entire Javadoc comment block provided directly
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
@Javadoc(
"This is the description\n"
+ "\n"
+ "@author author1\n"
+ "@author author2\n"
+ "\n"
+ "@deprecated Use {@link OtherMapper} instead\n"
+ "@since 0.1\n"
)
public interface MyAnnotatedWithJavadocMapper {
//...
}
----
====
Or using Text blocks:
.Javadoc annotation example with the entire Javadoc comment block provided directly using Text blocks
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
@Javadoc(
"""
This is the description
@author author1
@author author2
@deprecated Use {@link OtherMapper} instead
@since 0.1
"""
)
public interface MyAnnotatedWithJavadocMapper {
//...
}
----
====

View File

@ -69,14 +69,14 @@ Note that mappers generated by MapStruct are stateless and thread-safe and thus
If you're working with a dependency injection framework such as http://jcp.org/en/jsr/detail?id=346[CDI] (Contexts and Dependency Injection for Java^TM^ EE) or the http://www.springsource.org/spring-framework[Spring Framework], it is recommended to obtain mapper objects via dependency injection and *not* via the `Mappers` class as described above. For that purpose you can specify the component model which generated mapper classes should be based on either via `@Mapper#componentModel` or using a processor option as described in <<configuration-options>>.
Currently there is support for CDI and Spring (the latter either via its custom annotations or using the JSR 330 annotations). See <<configuration-options>> for the allowed values of the `componentModel` attribute which are the same as for the `mapstruct.defaultComponentModel` processor option. In both cases the required annotations will be added to the generated mapper implementations classes in order to make the same subject to dependency injection. The following shows an example using CDI:
Currently there is support for CDI and Spring (the latter either via its custom annotations or using the JSR 330 annotations). See <<configuration-options>> for the allowed values of the `componentModel` attribute which are the same as for the `mapstruct.defaultComponentModel` processor option and constants are defined in a class `MappingConstants.ComponentModel`. In both cases the required annotations will be added to the generated mapper implementations classes in order to make the same subject to dependency injection. The following shows an example using CDI:
.A mapper using the CDI component model
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper(componentModel = "cdi")
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
public interface CarMapper {
CarDto carToCarDto(Car car);
@ -102,7 +102,7 @@ A mapper which uses other mapper classes (see <<invoking-other-mappers>>) will o
[[injection-strategy]]
=== Injection strategy
When using <<using-dependency-injection,dependency injection>>, you can choose between field and constructor injection.
When using <<using-dependency-injection,dependency injection>>, you can choose between constructor, field, or setter injection.
This can be done by either providing the injection strategy via `@Mapper` or `@MapperConfig` annotation.
.Using constructor injection
@ -110,19 +110,23 @@ This can be done by either providing the injection strategy via `@Mapper` or `@M
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper(componentModel = "cdi", uses = EngineMapper.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = EngineMapper.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface CarMapper {
CarDto carToCarDto(Car car);
}
----
====
The generated mapper will inject all classes defined in the **uses** attribute.
The generated mapper will inject classes defined in the **uses** attribute if MapStruct has detected that it needs to use an instance of it for a mapping.
When `InjectionStrategy#CONSTRUCTOR` is used, the constructor will have the appropriate annotation and the fields won't.
When `InjectionStrategy#FIELD` is used, the annotation is on the field itself.
When `InjectionStrategy#SETTER` is used the annotation is on a generated setter method.
For now, the default injection strategy is field injection, but it can be configured with <<configuration-options>>.
It is recommended to use constructor injection to simplify testing.
When you define mappers in Spring with circular dependencies compilation may fail.
In that case utilize the `InjectionStrategy#SETTER` strategy.
[TIP]
====
For abstract classes or decorators setter injection should be used.

View File

@ -20,7 +20,7 @@ Currently the following conversions are applied automatically:
[WARNING]
====
Converting from larger data types to smaller ones (e.g. from `long` to `int`) can cause a value or precision loss. The `Mapper` and `MapperConfig` annotations have a method `typeConversionPolicy` to control warnings / errors. Due to backward compatibility reasons the default value is 'ReportingPolicy.IGNORE`.
Converting from larger data types to smaller ones (e.g. from `long` to `int`) can cause a value or precision loss. The `Mapper` and `MapperConfig` annotations have a method `typeConversionPolicy` to control warnings / errors. Due to backward compatibility reasons the default value is `ReportingPolicy.IGNORE`.
====
* Between all Java primitive types (including their wrappers) and `String`, e.g. between `int` and `String` or `Boolean` and `String`. A format string as understood by `java.text.DecimalFormat` can be specified.
@ -43,6 +43,9 @@ public interface CarMapper {
====
* Between `enum` types and `String`.
* Between `enum` types and `Integer`, according to `enum.ordinal()`.
** When converting from an `Integer`, the value needs to be less than the number of values of the enum, otherwise an `ArrayOutOfBoundsException` is thrown.
* Between big number types (`java.math.BigInteger`, `java.math.BigDecimal`) and Java primitive types (including their wrappers) as well as String. A format string as understood by `java.text.DecimalFormat` can be specified.
.Conversion from BigDecimal to String
@ -104,6 +107,8 @@ public interface CarMapper {
* Between `java.time.Instant` from Java 8 Date-Time package and `java.util.Date`.
* Between `java.time.LocalDateTime` from Java 8 Date-Time package and `java.time.LocalDate` from the same package.
* Between `java.time.ZonedDateTime` from Java 8 Date-Time package and `java.util.Calendar`.
* Between `java.sql.Date` and `java.util.Date`
@ -115,7 +120,18 @@ public interface CarMapper {
* When converting from a `String`, omitting `Mapping#dateFormat`, it leads to usage of the default pattern and date format symbols for the default locale. An exception to this rule is `XmlGregorianCalendar` which results in parsing the `String` according to http://www.w3.org/TR/xmlschema-2/#dateTime[XML Schema 1.0 Part 2, Section 3.2.7-14.1, Lexical Representation].
* Between `java.util.Currency` and `String`.
** When converting from a `String`, the value needs to be a valid https://en.wikipedia.org/wiki/ISO_4217[ISO-4217] alphabetic code otherwise an `IllegalArgumentException` is thrown
** When converting from a `String`, the value needs to be a valid https://en.wikipedia.org/wiki/ISO_4217[ISO-4217] alphabetic code otherwise an `IllegalArgumentException` is thrown.
* Between `java.util.UUID` and `String`.
** When converting from a `String`, the value needs to be a valid https://en.wikipedia.org/wiki/Universally_unique_identifier[UUID] otherwise an `IllegalArgumentException` is thrown.
* Between `String` and `StringBuilder`
* Between `java.net.URL` and `String`.
** When converting from a `String`, the value needs to be a valid https://en.wikipedia.org/wiki/URL[URL] otherwise a `MalformedURLException` is thrown.
* Between `java.util.Locale` and `String`.
** When converting from a `Locale`, the resulting `String` will be a well-formed IETF BCP 47 language tag representing the locale. When converting from a `String`, the locale that best represents the language tag will be returned. See https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#forLanguageTag-java.lang.String-[Locale.forLanguageTag()] and https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#toLanguageTag--[Locale.toLanguageTag()] for more information.
[[mapping-object-references]]
=== Mapping object references
@ -155,7 +171,7 @@ When generating the implementation of a mapping method, MapStruct will apply the
. If no such method was found MapStruct will try to generate an automatic sub-mapping method that will do the mapping between the source and target attributes.
. If MapStruct could not create a name based mapping method an error will be raised at build time, indicating the non-mappable attribute and its path.
A mapping control (`MappingControl`) can be defined on all levels (`@MappingConfig`, `@Mapper`, `@BeanMapping`, `@Mapping`), the latter taking precedence over the former. For example: `@Mapper( mappingControl = NoComplexMapping.class )` takes precedence over `@MapperConfig( mappingControl = DeepClone.class )`. `@IterableMapping` and `@MapMapping` work similar as `@Mapping`. MappingControl is experimental from MapStruct 1.4.
A mapping control (`MappingControl`) can be defined on all levels (`@MapperConfig`, `@Mapper`, `@BeanMapping`, `@Mapping`), the latter taking precedence over the former. For example: `@Mapper( mappingControl = NoComplexMapping.class )` takes precedence over `@MapperConfig( mappingControl = DeepClone.class )`. `@IterableMapping` and `@MapMapping` work similar as `@Mapping`. MappingControl is experimental from MapStruct 1.4.
`MappingControl` has an enum that corresponds to the first 4 options above: `MappingControl.Use#DIRECT`, `MappingControl.Use#MAPPING_METHOD`, `MappingControl.Use#BUILT_IN_CONVERSION` and `MappingControl.Use#COMPLEX_MAPPING` the presence of which allows the user to switch *on* a option. The absence of an enum switches *off* a mapping option. Default they are all present enabling all mapping options.
[NOTE]
@ -264,6 +280,13 @@ This puts the configuration of the nested mapping into one place (method) where
instead of re-configuring the same things on all of those upper methods.
====
[TIP]
====
When ignoring multiple properties instead of defining multiple `@Mapping` annotations, you can use the `@Ignored` annotation to group them together.
e.g. for the `FishTankMapperWithDocument` example above, you could write:
`@Ignored(targets = { "plant", "ornament", "material" })`
====
[NOTE]
====
In some cases the `ReportingPolicy` that is going to be used for the generated nested method would be `IGNORE`.
@ -385,6 +408,39 @@ When having a custom mapper hooked into the generated mapper with `@Mapper#uses(
For instance, the `CarDto` could have a property `owner` of type `Reference` that contains the primary key of a `Person` entity. You could now create a generic custom mapper that resolves any `Reference` objects to their corresponding managed JPA entity instances.
e.g.
.Example classes for the passing target type example
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
public class Car {
private Person owner;
// ...
}
public class Person extends BaseEntity {
// ...
}
public class Reference {
private String pk;
// ...
}
public class CarDto {
private Reference owner;
// ...
}
----
====
.Mapping method expecting mapping target type as parameter
====
[source, java, linenums]
@ -405,7 +461,7 @@ public class ReferenceMapper {
}
}
@Mapper(componentModel = "cdi", uses = ReferenceMapper.class )
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = ReferenceMapper.class )
public interface CarMapper {
Car carDtoToCar(CarDto carDto);
@ -695,3 +751,73 @@ public interface MovieMapper {
====
Although the used mechanism is the same, the user has to be a bit more careful. Refactoring the name of a defined qualifier in an IDE will neatly refactor all other occurrences as well. This is obviously not the case for changing a name.
====
=== Combining qualifiers with defaults
Please note that the `Mapping#defaultValue` is in essence a `String`, which needs to be converted to the `Mapping#target`. Providing a `Mapping#qualifiedByName` or `Mapping#qualifiedBy` will force MapStruct to use that method. If you want different behavior for the `Mapping#defaultValue`, then please provide an appropriate mapping method. This mapping method needs to transforms a `String` into the desired type of `Mapping#target` and also be annotated so that it can be found by the `Mapping#qualifiedByName` or `Mapping#qualifiedBy`.
.Mapper using defaultValue
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface MovieMapper {
@Mapping( target = "category", qualifiedByName = "CategoryToString", defaultValue = "DEFAULT" )
GermanRelease toGerman( OriginalRelease movies );
@Named("CategoryToString")
default String defaultValueForQualifier(Category cat) {
// some mapping logic
}
}
----
====
In the above example in case that category is null, the method `CategoryToString( Enum.valueOf( Category.class, "DEFAULT" ) )` will be called and the result will be set to the category field.
.Mapper using defaultValue and default method.
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface MovieMapper {
@Mapping( target = "category", qualifiedByName = "CategoryToString", defaultValue = "Unknown" )
GermanRelease toGerman( OriginalRelease movies );
@Named("CategoryToString")
default String defaultValueForQualifier(Category cat) {
// some mapping logic
}
@Named("CategoryToString")
default String defaultValueForQualifier(String value) {
return value;
}
}
----
====
In the above example in case that category is null, the method `defaultValueForQualifier( "Unknown" )` will be called and the result will be set to the category field.
If the above mentioned methods do not work there is the option to use `defaultExpression` to set the default value.
.Mapper using defaultExpression
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface MovieMapper {
@Mapping( target = "category", qualifiedByName = "CategoryToString", defaultExpression = "java(\"Unknown\")" )
GermanRelease toGerman( OriginalRelease movies );
@Named("CategoryToString")
default String defaultValueForQualifier(Category cat) {
// some mapping logic
}
}
----
====

View File

@ -36,7 +36,7 @@ public Set<String> integerSetToStringSet(Set<Integer> integers) {
return null;
}
Set<String> set = new HashSet<String>();
Set<String> set = new LinkedHashSet<String>();
for ( Integer integer : integers ) {
set.add( String.valueOf( integer ) );
@ -90,7 +90,7 @@ carDto.getPassengers().addAll( personsToPersonDtos( car.getPassengers() ) );
[WARNING]
====
It is not allowed to declare mapping methods with an iterable source and a non-iterable target or the other way around. An error will be raised when detecting this situation.
It is not allowed to declare mapping methods with an iterable source (from a java package) and a non-iterable target or the other way around. An error will be raised when detecting this situation.
====
[[mapping-maps]]
@ -125,7 +125,7 @@ public Map<Long, Date> stringStringMapToLongDateMap(Map<String, String> source)
return null;
}
Map<Long, Date> map = new HashMap<Long, Date>();
Map<Long, Date> map = new LinkedHashMap<Long, Date>();
for ( Map.Entry<String, String> entry : source.entrySet() ) {
@ -151,7 +151,7 @@ public Map<Long, Date> stringStringMapToLongDateMap(Map<String, String> source)
MapStruct has a `CollectionMappingStrategy`, with the possible values: `ACCESSOR_ONLY`, `SETTER_PREFERRED`, `ADDER_PREFERRED` and `TARGET_IMMUTABLE`.
In the table below, the dash `-` indicates a property name. Next, the trailing `s` indicates the plural form. The table explains the options and how they are applied to the presence/absense of a `set-s`, `add-` and / or `get-s` method on the target object:
In the table below, the dash `-` indicates a property name. Next, the trailing `s` indicates the plural form. The table explains the options and how they are applied to the presence/absence of a `set-s`, `add-` and / or `get-s` method on the target object:
.Collection mapping strategy options
|===
@ -192,7 +192,7 @@ The option `DEFAULT` should not be used explicitly. It is used to distinguish be
[TIP]
====
When working with an `adder` method and JPA entities, Mapstruct assumes that the target collections are initialized with a collection implementation (e.g. an `ArrayList`). You can use factories to create a new target entity with intialized collections instead of Mapstruct creating the target entity by its constructor.
When working with an `adder` method and JPA entities, Mapstruct assumes that the target collections are initialized with a collection implementation (e.g. an `ArrayList`). You can use factories to create a new target entity with initialized collections instead of Mapstruct creating the target entity by its constructor.
====
[[implementation-types-for-collection-mappings]]
@ -210,13 +210,17 @@ When an iterable or map mapping method declares an interface type as return type
|`List`|`ArrayList`
|`Set`|`HashSet`
|`Set`|`LinkedHashSet`
|`SequencedSet`|`LinkedHashSet`
|`SortedSet`|`TreeSet`
|`NavigableSet`|`TreeSet`
|`Map`|`HashMap`
|`Map`|`LinkedHashMap`
|`SequencedMap`|`LinkedHashMap`
|`SortedMap`|`TreeMap`

View File

@ -42,7 +42,7 @@ public Set<String> integerStreamToStringSet(Stream<Integer> integers) {
}
return integers.map( integer -> String.valueOf( integer ) )
.collect( Collectors.toCollection( HashSet<String>::new ) );
.collect( Collectors.toCollection( LinkedHashSet<String>::new ) );
}
@Override

View File

@ -20,9 +20,9 @@ public interface OrderMapper {
OrderMapper INSTANCE = Mappers.getMapper( OrderMapper.class );
@ValueMappings({
@ValueMapping(source = "EXTRA", target = "SPECIAL"),
@ValueMapping(source = "STANDARD", target = "DEFAULT"),
@ValueMapping(source = "NORMAL", target = "DEFAULT")
@ValueMapping(target = "SPECIAL", source = "EXTRA"),
@ValueMapping(target = "DEFAULT", source = "STANDARD"),
@ValueMapping(target = "DEFAULT", source = "NORMAL")
})
ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
}
@ -65,7 +65,7 @@ public class OrderMapperImpl implements OrderMapper {
----
====
By default an error will be raised by MapStruct in case a constant of the source enum type does not have a corresponding constant with the same name in the target type and also is not mapped to another constant via `@ValueMapping`. This ensures that all constants are mapped in a safe and predictable manner. The generated
mapping method will throw an IllegalStateException if for some reason an unrecognized source value occurs.
mapping method will throw an `IllegalStateException` if for some reason an unrecognized source value occurs.
MapStruct also has a mechanism for mapping any remaining (unspecified) mappings to a default. This can be used only once in a set of value mappings and only applies to the source. It comes in two flavors: `<ANY_REMAINING>` and `<ANY_UNMAPPED>`. They cannot be used at the same time.
@ -75,14 +75,18 @@ MapStruct will *not* attempt such name based mapping for `<ANY_UNMAPPED>` and di
MapStruct is able to handle `null` sources and `null` targets by means of the `<NULL>` keyword.
In addition, the constant value `<THROW_EXCEPTION>` can be used for throwing an exception for particular value mappings. This value is only applicable to `ValueMapping#target()` and not `ValueMapping#source()` since MapStruct can't map from exceptions.
[TIP]
====
Constants for `<ANY_REMAINING>`, `<ANY_UNMAPPED>` and `<NULL>` are available in the `MappingConstants` class.
Constants for `<ANY_REMAINING>`, `<ANY_UNMAPPED>`, `<THROW_EXCEPTION>` and `<NULL>` are available in the `MappingConstants` class.
====
Finally `@InheritInverseConfiguration` and `@InheritConfiguration` can be used in combination with `@ValueMappings`. `<ANY_REMAINING>` and `<ANY_UNMAPPED>` will be ignored in that case.
.Enum mapping method, <NULL> and <ANY_REMAINING>
The following code snippets exemplify the use of the aforementioned constants.
.Enum mapping method, `<NULL>` and `<ANY_REMAINING>`
====
[source, java, linenums]
[subs="verbatim,attributes"]
@ -102,7 +106,7 @@ public interface SpecialOrderMapper {
----
====
.Enum mapping method result, <NULL> and <ANY_REMAINING>
.Enum mapping method result, `<NULL>` and `<ANY_REMAINING>`
====
[source, java, linenums]
[subs="verbatim,attributes"]
@ -137,9 +141,52 @@ public class SpecialOrderMapperImpl implements SpecialOrderMapper {
*Note:* MapStruct would have refrained from mapping the `RETAIL` and `B2B` when `<ANY_UNMAPPED>` was used instead of `<ANY_REMAINING>`.
[WARNING]
.Enum mapping method with `<THROW_EXCEPTION>`
====
The mapping of enum to enum via the `@Mapping` annotation is *DEPRECATED*. It will be removed from future versions of MapStruct. Please adapt existing enum mapping methods to make use of `@ValueMapping` instead.
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface SpecialOrderMapper {
SpecialOrderMapper INSTANCE = Mappers.getMapper( SpecialOrderMapper.class );
@ValueMappings({
@ValueMapping( source = "STANDARD", target = "DEFAULT" ),
@ValueMapping( source = "C2C", target = MappingConstants.THROW_EXCEPTION )
})
ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
}
----
====
.Enum mapping method with `<THROW_EXCEPTION>` result
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class SpecialOrderMapperImpl implements SpecialOrderMapper {
@Override
public ExternalOrderType orderTypeToExternalOrderType(OrderType orderType) {
if ( orderType == null ) {
return null;
}
ExternalOrderType externalOrderType;
switch ( orderType ) {
case STANDARD: externalOrderType = ExternalOrderType.DEFAULT;
break;
case C2C: throw new IllegalArgumentException( "Unexpected enum constant: " + orderType );
default: throw new IllegalArgumentException( "Unexpected enum constant: " + orderType );
}
return externalOrderType;
}
}
----
====
=== Mapping enum-to-String or String-to-enum
@ -152,13 +199,15 @@ MapStruct supports enum to a String mapping along the same lines as is described
2. Similarity: `<ANY_UNMAPPED`> stops after handling defined mapping and proceeds to the switch/default clause value.
3. Difference: `<ANY_REMAINING>` will result in an error. It acts on the premise that there is name similarity between enum constants in source and target which does not make sense for a String type.
4. Difference: Given 1. and 3. there will never be unmapped values.
5. Similarity: `<THROW_EXCEPTION>` can be used for throwing an exception for particular enum values.
*`String` to enum*
1. Similarity: All not explicit defined mappings will result in the target enum constant mapped from the `String` value when that maches the target enum constant name.
1. Similarity: All not explicit defined mappings will result in the target enum constant mapped from the `String` value when that matches the target enum constant name.
2. Similarity: `<ANY_UNMAPPED`> stops after handling defined mapping and proceeds to the switch/default clause value.
3. Similarity: `<ANY_REMAINING>` will create a mapping for each target enum constant and proceed to the switch/default clause value.
4. Difference: A switch/default value needs to be provided to have a determined outcome (enum has a limited set of values, `String` has unlimited options). Failing to specify `<ANY_REMAINING>` or `<ANY_UNMAPPED`> will result in a warning.
5. Similarity: `<THROW_EXCEPTION>` can be used for throwing an exception for any arbitrary `String` value.
=== Custom name transformation
@ -259,6 +308,49 @@ MapStruct provides the following out of the box enum name transformation strateg
* _stripSuffix_ - Strips a suffix from the source enum
* _prefix_ - Applies a prefix on the source enum
* _stripPrefix_ - Strips a prefix from the source enum
* _case_ - Applies case transformation to the source enum. Supported _case_ transformations are:
** _upper_ - Performs upper case transformation to the source enum
** _lower_ - Performs lower case transformation to the source enum
** _capital_ - Performs capitalisation of the first character of every word in the source enum and everything else to lowercase. A word is split by "_"
It is also possible to register custom strategies.
For more information on how to do that have a look at <<custom-enum-transformation-strategy>>
[[value-mapping-composition]]
=== ValueMapping Composition
The `@ValueMapping` annotation supports now `@Target` with `ElementType#ANNOTATION_TYPE` in addition to `ElementType#METHOD`.
This allows `@ValueMapping` to be used on other (user defined) annotations for re-use purposes.
For example:
.Custom value mapping annotations
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Retention( RetentionPolicy.CLASS )
@ValueMapping(source = "EXTRA", target = "SPECIAL")
@ValueMapping(source = MappingConstants.ANY_REMAINING, target = "DEFAULT")
public @interface CustomValueAnnotation {
}
----
====
It can be used to describe some common value mapping relationships to avoid duplicate declarations, as in the following example:
.Using custom combination annotations
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface ValueMappingCompositionMapper {
@CustomValueAnnotation
ExternalOrderType orderTypeToExternalOrderType(OrderType orderType);
@CustomValueAnnotation
@ValueMapping(source = "STANDARD", target = "SPECIAL")
ExternalOrderType duplicateAnnotation(OrderType orderType);
}
----
====

View File

@ -2,6 +2,7 @@
:revdate: {docdate}
:toc: right
:sectanchors:
:source-highlighter: coderay
:Author: Gunnar Morling, Andreas Gudian, Sjaak Derksen, Filip Hrisafov and the MapStruct community
:processor-dir: ../../../../processor
:processor-ap-test: {processor-dir}/src/test/java/org/mapstruct/ap/test
@ -11,7 +12,7 @@
[[Preface]]
== Preface
This is the reference documentation of MapStruct, an annotation processor for generating type-safe, performant and dependency-free bean mapping code.
This guide covers all the functionality provided by MapStruct. In case this guide doesn't answer all your questions just join the MapStruct https://groups.google.com/forum/?fromgroups#!forum/mapstruct-users[Google group] to get help.
This guide covers all the functionality provided by MapStruct. In case this guide doesn't answer all your questions just join the MapStruct https://github.com/mapstruct/mapstruct/discussions[GitHub Discussions] to get help.
You found a typo or other error in this guide? Please let us know by opening an issue in the https://github.com/mapstruct/mapstruct[MapStruct GitHub repository],
or, better yet, help the community and send a pull request for fixing it!
@ -45,3 +46,5 @@ include::chapter-11-reusing-mapping-configurations.asciidoc[]
include::chapter-12-customizing-mapping.asciidoc[]
include::chapter-13-using-mapstruct-spi.asciidoc[]
include::chapter-14-third-party-api-integration.asciidoc[]

View File

@ -12,7 +12,7 @@
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-parent</artifactId>
<version>1.4.0.Beta3</version>
<version>1.7.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
@ -28,7 +28,7 @@
<repositories>
<repository>
<id>gradle</id>
<url>https://repo.gradle.org/gradle/libs-releases</url>
<url>https://repo.gradle.org/artifactory/libs-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
@ -65,7 +65,6 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
<dependency>
@ -134,8 +133,8 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</profile>

View File

@ -26,16 +26,36 @@ public final class FullFeatureCompilationExclusionCliEnhancer implements Process
// SPI not working correctly here.. (not picked up)
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1596/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1801/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_3089/*.java" );
switch ( currentJreVersion ) {
case JAVA_8:
additionalExcludes.add( "org/mapstruct/ap/test/**/spring/**/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/injectionstrategy/cdi/**/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/injectionstrategy/jakarta_cdi/**/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/annotatewith/deprecated/jdk11/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/**/jdk21/*.java" );
if ( processorType == ProcessorTest.ProcessorType.ECLIPSE_JDT ) {
additionalExcludes.add(
"org/mapstruct/ap/test/selection/methodgenerics/wildcards/LifecycleIntersectionMapper.java" );
}
break;
case JAVA_9:
// TODO find out why this fails:
additionalExcludes.add( "org/mapstruct/ap/test/collection/wildcard/BeanMapper.java" );
additionalExcludes.add( "org/mapstruct/ap/test/**/jdk21/*.java" );
break;
case JAVA_11:
additionalExcludes.add( "org/mapstruct/ap/test/**/spring/**/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/**/jdk21/*.java" );
break;
case JAVA_17:
additionalExcludes.add( "org/mapstruct/ap/test/**/jdk21/*.java" );
break;
default:
}
Collection<String> result = new ArrayList<String>( additionalExcludes.size() );
Collection<String> result = new ArrayList<>(additionalExcludes.size());
for ( int i = 0; i < additionalExcludes.size(); i++ ) {
result.add( "-DadditionalExclude" + i + "=" + additionalExcludes.get( i ) );
}

View File

@ -62,7 +62,7 @@ public class GradleIncrementalCompilationTest {
}
private GradleRunner getRunner(String... additionalArguments) {
List<String> fullArguments = new ArrayList<String>( compileArgs );
List<String> fullArguments = new ArrayList<>(compileArgs);
fullArguments.addAll( Arrays.asList( additionalArguments ) );
return runner.withArguments( fullArguments );
}

View File

@ -5,6 +5,7 @@
*/
package org.mapstruct.itest.tests;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.parallel.Execution;
@ -75,16 +76,32 @@ public class MavenIntegrationTest {
void jaxbTest() {
}
@ProcessorTest(baseDir = "jakartaJaxbTest")
void jakartaJaxbTest() {
}
@ProcessorTest(baseDir = "jsr330Test")
@EnabledForJreRange(min = JRE.JAVA_17)
@DisabledOnJre(JRE.OTHER)
void jsr330Test() {
}
@ProcessorTest(baseDir = "lombokBuilderTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@DisabledOnJre(JRE.OTHER)
void lombokBuilderTest() {
}
@ProcessorTest(baseDir = "lombokModuleTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC,
ProcessorTest.ProcessorType.JAVAC_WITH_PATHS
})
@EnabledForJreRange(min = JRE.JAVA_11)
@DisabledOnJre(JRE.OTHER)
void lombokModuleTest() {
}
@ProcessorTest(baseDir = "namingStrategyTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@ -100,6 +117,11 @@ public class MavenIntegrationTest {
void protobufBuilderTest() {
}
@ProcessorTest(baseDir = "sealedSubclassTest")
@EnabledForJreRange(min = JRE.JAVA_17)
void sealedSubclassTest() {
}
@ProcessorTest(baseDir = "recordsTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@ -107,11 +129,33 @@ public class MavenIntegrationTest {
void recordsTest() {
}
@ProcessorTest(baseDir = "recordsCrossModuleTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@EnabledForJreRange(min = JRE.JAVA_17)
void recordsCrossModuleTest() {
}
@ProcessorTest(baseDir = "recordsCrossModuleInterfaceTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@EnabledForJreRange(min = JRE.JAVA_17)
void recordsCrossModuleInterfaceTest() {
}
@ProcessorTest(baseDir = "expressionTextBlocksTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
})
@EnabledForJreRange(min = JRE.JAVA_17)
void expressionTextBlocksTest() {
}
@ProcessorTest(baseDir = "kotlinDataTest", processorTypes = {
ProcessorTest.ProcessorType.JAVAC
}, forkJvm = true)
// We have to fork the jvm because there is an NPE in com.intellij.openapi.util.SystemInfo.getRtVersion
// and the kotlin-maven-plugin uses that. See also https://youtrack.jetbrains.com/issue/IDEA-238907
@DisabledOnJre(JRE.OTHER)
void kotlinDataTest() {
}
@ -119,7 +163,14 @@ public class MavenIntegrationTest {
void simpleTest() {
}
// for issue #2593
@ProcessorTest(baseDir = "defaultPackage")
void defaultPackageTest() {
}
@ProcessorTest(baseDir = "springTest")
@EnabledForJreRange(min = JRE.JAVA_17)
@DisabledOnJre(JRE.OTHER)
void springTest() {
}

View File

@ -134,10 +134,20 @@ public class ProcessorInvocationInterceptor implements InvocationInterceptor {
}
private void configureProcessor(Verifier verifier) {
String compilerId = processorTestContext.getProcessor().getCompilerId();
ProcessorTest.ProcessorType processor = processorTestContext.getProcessor();
String compilerId = processor.getCompilerId();
if ( compilerId != null ) {
verifier.addCliOption( "-Pgenerate-via-compiler-plugin" );
String profile = processor.getProfile();
if ( profile == null ) {
profile = "generate-via-compiler-plugin";
}
verifier.addCliOption( "-P" + profile );
verifier.addCliOption( "-Dcompiler-id=" + compilerId );
if ( processor == ProcessorTest.ProcessorType.JAVAC ) {
if ( CURRENT_VERSION.ordinal() >= JRE.JAVA_21.ordinal() ) {
verifier.addCliOption( "-Dmaven.compiler.proc=full" );
}
}
}
else {
verifier.addCliOption( "-Pgenerate-via-processor-plugin" );

View File

@ -44,6 +44,7 @@ public @interface ProcessorTest {
enum ProcessorType {
JAVAC( "javac" ),
JAVAC_WITH_PATHS( "javac", JRE.OTHER, "generate-via-compiler-plugin-with-annotation-processor-paths" ),
ECLIPSE_JDT( "jdt", JRE.JAVA_8 ),
@ -51,14 +52,20 @@ public @interface ProcessorTest {
private final String compilerId;
private final JRE max;
private final String profile;
ProcessorType(String compilerId) {
this( compilerId, JRE.OTHER );
}
ProcessorType(String compilerId, JRE max) {
this( compilerId, max, null );
}
ProcessorType(String compilerId, JRE max, String profile) {
this.compilerId = compilerId;
this.max = max;
this.profile = profile;
}
public String getCompilerId() {
@ -68,6 +75,10 @@ public @interface ProcessorTest {
public JRE maxJre() {
return max;
}
public String getProfile() {
return profile;
}
}
/**
@ -98,6 +109,7 @@ public @interface ProcessorTest {
*/
ProcessorType[] processorTypes() default {
ProcessorType.JAVAC,
ProcessorType.JAVAC_WITH_PATHS,
ProcessorType.ECLIPSE_JDT,
ProcessorType.PROCESSOR_PLUGIN
};

View File

@ -56,7 +56,7 @@
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<artifactId>weld-core-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -6,10 +6,11 @@
package org.mapstruct.itest.cdi;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.DecoratedWith;
import org.mapstruct.itest.cdi.other.DateMapper;
@Mapper( componentModel = "cdi", uses = DateMapper.class )
@Mapper( componentModel = MappingConstants.ComponentModel.CDI, uses = DateMapper.class )
public interface DecoratedSourceTargetMapper {
Target sourceToTarget(Source source);

View File

@ -6,9 +6,10 @@
package org.mapstruct.itest.cdi;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.itest.cdi.other.DateMapper;
@Mapper(componentModel = "cdi", uses = DateMapper.class)
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, uses = DateMapper.class)
public interface SourceTargetMapper {
Target sourceToTarget(Source source);

View File

@ -38,7 +38,9 @@ public class CdiBasedMapperTest {
.addPackage( SourceTargetMapper.class.getPackage() )
.addPackage( DateMapper.class.getPackage() )
.addAsManifestResource(
new StringAsset("<decorators><class>org.mapstruct.itest.cdi.SourceTargetMapperDecorator</class></decorators>"),
new StringAsset("<beans version=\"1.1\">" +
"<decorators><class>org.mapstruct.itest.cdi.SourceTargetMapperDecorator</class></decorators>" +
"</beans>"),
"beans.xml" );
}

View File

@ -0,0 +1,97 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
public class DefaultPackageObject {
public enum CarType {
SEDAN, CAMPER, X4, TRUCK;
}
static public class Car {
private String make;
private int numberOfSeats;
private CarType type;
public Car(String string, int numberOfSeats, CarType sedan) {
this.make = string;
this.numberOfSeats = numberOfSeats;
this.type = sedan;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getNumberOfSeats() {
return numberOfSeats;
}
public void setNumberOfSeats(int numberOfSeats) {
this.numberOfSeats = numberOfSeats;
}
public CarType getType() {
return type;
}
public void setType(CarType type) {
this.type = type;
}
}
static public class CarDto {
private String make;
private int seatCount;
private String type;
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
@Mapping(source = "numberOfSeats", target = "seatCount")
CarDto carToCarDto(Car car);
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright MapStruct Authors.
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-it-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>defaultPackage</artifactId>
<packaging>jar</packaging>
</project>

View File

@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
public class DefaultPackageTest {
@Test
public void shouldWorkCorrectlyInDefaultPackage() {
DefaultPackageObject.CarDto carDto = DefaultPackageObject.CarMapper.INSTANCE.carToCarDto(
new DefaultPackageObject.Car(
"Morris",
5,
DefaultPackageObject.CarType.SEDAN
) );
assertThat( carDto ).isNotNull();
assertThat( carDto.getMake() ).isEqualTo( "Morris" );
assertThat( carDto.getSeatCount() ).isEqualTo( 5 );
assertThat( carDto.getType() ).isEqualTo( "SEDAN" );
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright MapStruct Authors.
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-it-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>expressionTextBlocksTest</artifactId>
<packaging>jar</packaging>
</project>

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.textBlocks;
/**
* @author Filip Hrisafov
*/
public class Car {
private WheelPosition wheelPosition;
public WheelPosition getWheelPosition() {
return wheelPosition;
}
public void setWheelPosition(WheelPosition wheelPosition) {
this.wheelPosition = wheelPosition;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.textBlocks;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
public interface CarAndWheelMapper {
CarAndWheelMapper INSTANCE = Mappers.getMapper( CarAndWheelMapper.class );
@Mapping(target = "wheelPosition",
expression =
"""
java(
source.getWheelPosition() == null ?
null :
source.getWheelPosition().getPosition()
)
""")
CarDto carDtoFromCar(Car source);
@Mapping(target = "wheelPosition",
expression = """
java(
source.wheelPosition() == null ?
null :
new WheelPosition(source.wheelPosition())
)
""")
Car carFromCarDto(CarDto source);
}

View File

@ -0,0 +1,15 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.textBlocks;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public record CarDto(String wheelPosition) {
}

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.textBlocks;
/**
* @author Filip Hrisafov
*/
public class WheelPosition {
private final String position;
public WheelPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
}

View File

@ -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.itest.textBlocks;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TextBlocksTest {
@Test
public void textBlockExpressionShouldWork() {
Car car = new Car();
car.setWheelPosition( new WheelPosition( "left" ) );
CarDto carDto = CarAndWheelMapper.INSTANCE.carDtoFromCar(car);
assertThat( carDto ).isNotNull();
assertThat( carDto.wheelPosition() )
.isEqualTo( "left" );
}
}

View File

@ -23,7 +23,6 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -25,6 +25,13 @@
<additionalExclude2>x</additionalExclude2>
<additionalExclude3>x</additionalExclude3>
<additionalExclude4>x</additionalExclude4>
<additionalExclude5>x</additionalExclude5>
<additionalExclude6>x</additionalExclude6>
<additionalExclude7>x</additionalExclude7>
<additionalExclude8>x</additionalExclude8>
<additionalExclude9>x</additionalExclude9>
<additionalExclude10>x</additionalExclude10>
<additionalExclude11>x</additionalExclude11>
</properties>
<build>
@ -45,6 +52,15 @@
<exclude>${additionalExclude2}</exclude>
<exclude>${additionalExclude3}</exclude>
<exclude>${additionalExclude4}</exclude>
<exclude>${additionalExclude5}</exclude>
<exclude>${additionalExclude6}</exclude>
<exclude>${additionalExclude7}</exclude>
<exclude>${additionalExclude8}</exclude>
<exclude>${additionalExclude9}</exclude>
<exclude>${additionalExclude10}</exclude>
<exclude>${additionalExclude11}</exclude>
<exclude>${additionalExclude12}</exclude>
<exclude>${additionalExclude13}</exclude>
</excludes>
</configuration>
</plugin>
@ -60,6 +76,14 @@
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
<!-- Spring -->
<dependency>
@ -75,6 +99,13 @@
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<profiles>
@ -85,14 +116,16 @@
</activation>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
</profile>

View File

@ -14,6 +14,6 @@ import org.mapstruct.itest.gradle.model.Source;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface TestMapper {
@Mapping(source = "value", target = "field")
@Mapping(target = "field", source = "value")
public Target toTarget(Source source);
}

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright MapStruct Authors.
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-it-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>jakartaJaxbTest</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>xjc</id>
<phase>initialize</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<xjbSources>
<xjbSource>${project.build.resources[0].directory}/binding</xjbSource>
</xjbSources>
<sources>
<source>${project.build.resources[0].directory}/schema</source>
</sources>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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.itest.jakarta.jaxb;
import java.util.List;
/**
* @author Sjaak Derksen
*/
public class OrderDetailsDto {
private String name;
private List<String> description;
private OrderStatusDto status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getDescription() {
return description;
}
public void setDescription(List<String> description) {
this.description = description;
}
public OrderStatusDto getStatus() {
return status;
}
public void setStatus(OrderStatusDto status) {
this.status = status;
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.jakarta.jaxb;
import java.util.Date;
/**
* @author Sjaak Derksen
*/
public class OrderDto {
private Long orderNumber;
private Date orderDate;
private OrderDetailsDto orderDetails;
private ShippingAddressDto shippingAddress;
public Long getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(Long orderNumber) {
this.orderNumber = orderNumber;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public OrderDetailsDto getOrderDetails() {
return orderDetails;
}
public void setOrderDetails(OrderDetailsDto orderDetails) {
this.orderDetails = orderDetails;
}
public ShippingAddressDto getShippingAddress() {
return shippingAddress;
}
public void setShippingAddress(ShippingAddressDto shippingAddress) {
this.shippingAddress = shippingAddress;
}
}

View File

@ -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.itest.jakarta.jaxb;
/**
* @author Sjaak Derksen
*/
public enum OrderStatusDto {
ORDERED( "small" ),
PROCESSED( "medium" ),
DELIVERED( "large" );
private final String value;
OrderStatusDto(String v) {
value = v;
}
public String value() {
return value;
}
public static OrderStatusDto fromValue(String v) {
for ( OrderStatusDto c : OrderStatusDto.values() ) {
if ( c.value.equals( v ) ) {
return c;
}
}
throw new IllegalArgumentException( v );
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.jakarta.jaxb;
/**
* @author Sjaak Derksen
*/
public class ShippingAddressDto {
private String street;
private String houseNumber;
private String city;
private String country;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}

Some files were not shown because too many files have changed in this diff Show More