Repository navigation
#4145 Use the adder for expression and constant mappings - #4151
Open
matheuslealpa wants to merge 1 commit into
Open
matheuslealpa wants to merge 1 commit into
matheuslealpa wants to merge 1 commit into
Conversation
With an adder as the target write accessor, expression and constant mappings fell back to the getter handling and generated code that called the adder without arguments, which does not compile. An expression is now assigned to a local Iterable variable and the adder is called for each of its elements. A constant is added as a single element.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4145
With
CollectionMappingStrategy.ADDER_PREFERRED(or any target whose write accessor is an adder),@Mapping(target = "list", expression = ...)and@Mapping(target = "list", constant = ...)generated code that does not compile:Cause
JavaExpressionMappingBuilderandConstantMappingBuilderonly distinguish a setter (or field) from everything else and treat everything else as a getter, wrapping the assignment inGetterWrapperForCollectionsAndMaps. With an adder, the write accessor takes one argument andtargetTypeis the element type, hence the adder call without arguments and theStringlocal variable.Changes
JavaExpressionMappingBuilder: when the write accessor is an adder, the expression is assigned to a localIterable<T>variable and wrapped inAdderWrapper, so the adder is called for each element, as it already is for source properties.ConstantMappingBuilder: when the write accessor is an adder, the constant is added as a single element.TypeFactory.iterableTypeOf(Type): new helper that buildsIterable<T>, next to the existingclassTypeOf.Issue4145Test: covers both cases. Without the change it fails with the compilation errors from the issue.Generated code now:
Known limitation
The type of an expression is unknown to the processor, so it is assumed to be an
Iterableof the adder's element type, in the same way the getter handling assumes the target collection type. An expression that returns an array or aStreamdoes not compile against theIterable<T>local variable. It has to be converted in the expression itself, e.g.Arrays.asList( ... )or.collect( Collectors.toList() ).Arrays could be supported by iterating over the expression directly (
for ( T item : <expression> )), but that would drop the null check, and aStreamwould needforEach, which does not work for arrays. I kept the null check and the single form. I'm happy to change this if you prefer a different trade-off.Constant as a single element
A constant is one value, so it results in one adder call (
target.addList( "c" )). This was proposed in the issue and matches what the reporter expects.The full
processortest suite passes locally on Java 21.