-
-
Notifications
You must be signed in to change notification settings - Fork 1k
#1454 Support for lifecycle methods on type being built with builders #3267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
filiphr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good @thunderhook. I've left some comments in line. Regarding your remarks:
There is one edge-case i stumbled accross. With supporting
@AfterMappingon the targets builder and finalized object, the following combination can be done by using methods both returning the result:
I think that it is fine to have it as is now. We have clearly documented how things work when something is returned from a lifecycle method, so it is completely valid to use it like that if someone wants to do it. Keep in mind that afterWithBuilderTargetReturningTarget in our example always returns an instance, but in an actual implementation you might return an instance based on certain logic in your own method. Adapting the test for us is fine.
processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java
Outdated
Show resolved
Hide resolved
processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java
Show resolved
Hide resolved
processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java
Outdated
Show resolved
Hide resolved
|
Checkstyle failed because of the 2000 line limit in |
filiphr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checkstyle failed because of the 2000 line limit in
BeanMappingMethod
merging some the lines looks OK. Perhaps we need to refactor some things there (or increase this limit of 2000).
I don't like the wrappings in forEach and Optional.ofNullable though. It creates extra classes. Let's increase the line limit (I am also adding some new things to that class now for something else, so it would fail anyways)
processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java
Outdated
Show resolved
Hide resolved
Yeah, you're right. I will revert the changes to make life easier when you rebase |
|
I increased the |
filiphr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good @thunderhook.
Two last things that I just remembered about. One I commented inline and the other on is the documetation. In the Mapping customization with before-mapping and after-mapping methods we have a not mentioning the fact that @AfterMapping / @BeforeMapping methods are not being invoked.
| } | ||
|
|
||
| private void removeMappingReferencesWithoutSourceParameters(List<LifecycleCallbackMethodReference> references) { | ||
| references.removeIf( r -> r.getSourceParameters().isEmpty() && r.getReturnType().isVoid() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment why we are doing this. The reason why this is done is because lifecycle methods without any parameters are going to be already invoked. Mostly for future us, when we scratch our head why this is being done 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for explaining why I did this. I already already forgot the reason 2 weeks after implementing it... 😆 I added a comment.
I'm not sure if I understood you correctly. I tried to tackle the documentation part and replaced the Important: with a NOTE-block going into detail with builders. Have a look at the MR. Documenting and formulating is definitely not my forte, I tried my best. 😅 |
filiphr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs are perfect. Thanks a lot. I'll merge it once the builds are done
Hi,
this is my first PR for mapstruct. Disclaimer: Because of the IntelliJ Community Edition I do not have any highlighting or formatting for
.ftlfiles, so bear with me. 😅I tried to be as consistent as possible with the naming of the lifecycle method reference collections (
beforeMappingReferencesWithFinalizedReturnType&afterMappingReferencesWithFinalizedReturnType) but if you have better suggestions, please tell me.There is one edge-case i stumbled accross. With supporting
@AfterMappingon the targets builder and finalized object, the following combination can be done by using methods both returning the result:Which results in the following mapper:
This makes sense but might be confusing. I was wondering if MapStruct should throw an error in that case?
For me, I had to adapt the test, that
context#afterWithBuilderTargetReturningTargetreturns null so that he other@AfterMappingsget called.Fixes #1454