#73 Implementing Immutable Constructors - #847
Conversation
|
(Message from the pull-request-builder): Admins, please verify this patch for it to build in the pull-request-builder. |
|
Hi Guys, I have started work on #73. This PR is far from being ready to merge but I wanted to check if so see if I am on the right track and ask a few questions because I have gotten stuck. Basic Thought Process:
Questions: Ciaran |
| unprocessedSourceParameters.add( sourceParameter ); | ||
| } | ||
|
|
||
| if ( method.getResultType().getConstructor() != null ) { |
There was a problem hiding this comment.
Add the extra mapping targets
|
@cliedeman That's an awesome contribution, thanks! Still recovering from vacation, but going to look into this soon. This one has been on the wish list for a very long time! |
| private String getConstructorConstantExpression(Parameter parameter) { | ||
| if ( parameter.getType().isPrimitive() ) { | ||
| TypeKind kind = parameter.getType().getTypeMirror().getKind(); | ||
| if ( kind == TypeKind.BOOLEAN ) { |
There was a problem hiding this comment.
Hmm. I just did a search on 0.0. Seems that we have similar code already in Type and IterableMappingMethod.. Perhaps already a refactoring point 😄
|
Hey @cliedeman, great that you're taking this up! One thing that directly caught my eye was the introduction of My idea would be to try to map the properties to constructor args by type, and if that doesn't work without ambiguities, match those ambiguous properties by constructor argument name (or their Regarding your questions:
Hmm, I would have guessed that in the model it's part of the
Good catch!
Introducing local vars sounds reasonable here. Don't know if that works with the existing wrapper, though. |
The reason is that these bean classes might be generated by a framework. I would not like to change generated code. |
|
Hi Guys, Thanks for the feedback. @agudian, @sjaakd I am not too worried how we end up sourcing the bean constructor to use. I feel like there are multiple approaches but this is not the hard part of the implementation. I am happy to change it later or use an alternate method. For instance my particular use case would most likely be with jackson annotated constructor fields. In this case I would like to retrieve the jackson annotations and somehow create the constructor mapping from them. @agudian I think enhancing the bean factory method to accept parameters might be a better approach in the long run and will experiment with this. Ciaran |
|
I reckon we have the following situations: 1a. The mapping requires a constructor with a set of arguments, each of them of a unique type. There's only one matching constructor, with exactly those arguments of the required type. -> Make match. 1b. The mapping requires a constructor with a set of arguments, each of them of a unique type. There's are more matching constructors, with exactly those arguments of the required type. -> 2a. The mapping requires a constructor with a set of arguments, but some of the required arguments have the same type. -> MapStruct could use constructor parameter names to make a match. I know that IDE's match the name of the constructor with the properties of the bean when you tell it to generate a constructor (at least Netbeans does). However, this is not in anyway conform java bean standard and we don't know how frameworks generate their constructors. 2b. The mapping requires a constructor with a set of arguments, but some of the required arguments have the same type, names do not match up. -> We could rely on the 3a. The mapping requires a constructor with a set of arguments, each of them of a unique type. However there's a constructor that has a superset of these arguments. -> 4a. The mapping requires a constructor with a set of arguments, each of them of a unique type. However there's a constructor that has a subset of these arguments. -> the remainder could be completed by the regular target accessors (setter, adder, getter-for-collections) 5a. The user wants to select deliberately an other constructor. -> We need (the) factory mechanism + qualifiers to make a decision. In general: when do we resort to constructor matching? If there's no empty constructor? |
|
@cliedeman, regarding
Could you give an example of how that'd look like? |
|
So my current inclination would be towards the following:
This above would be the default implementation of an SPI for constructor selection. Users could plug in their own selection mechanism via that API (Ciaran's Jackson example would be one potential usage). |
|
@gunnarmorling Jackson Example: whoops accidentally hit close.... |
|
(Message from the pull-request-builder): Admins, please verify this patch for it to build in the pull-request-builder. |
|
Great to see you're working on this! Could you please sonsider that the constructor for the immutable object could be private access? This will be often the case if externally a Builder is used to fill the objects attributes (like generated by https://immutables.github.io/). |
|
@mvonrenteln we have that on the radar with the separate issue #782. PRs are welcome! |
|
Any news on this PR? |
No description provided.