#2932 Use builder for generic target types - #4133
Open
seonwooj0810 wants to merge 1 commit into
Open
seonwooj0810 wants to merge 1 commit into
seonwooj0810 wants to merge 1 commit into
Conversation
DefaultBuilderProvider#isBuildMethod checked the build method's return type (e.g. Record<T> with the builder's T) for assignability against the type element's declared type (Record<T> with the class's own T). The two type variables are distinct, so the check failed for every generic type and MapStruct fell back to the (often private) constructor. Compare erasures instead, and resolve the builder's type parameters against the concrete target type (RecordBuilder<String> for Record<String>) in BuilderType.
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 #2932
Root cause
DefaultBuilderProvider#isBuildMethodchecks that the builder's build method returns the type being built viatypeUtils.isAssignable( buildMethodReturnType, typeElement.asType() ). For a generic type such as Lombok's@BuilderonRecord<T>, the build method returnsRecord<T>whereTis the builder's type variable, whiletypeElement.asType()isRecord<T>with the class's ownT. These are distinct type variables, so the check fails for every generic type, no builder is found, and MapStruct falls back to the (private) all-args constructor — exactly the behaviour reported in the issue.Change
DefaultBuilderProvider#isBuildMethod: compare the erasures of the build method's return type and the type being built.BuilderType#create: resolve the builder's type parameters against the concrete target type, so that forRecord<String>the generated code usesRecordBuilder<String>(not a raw/unresolvedRecordBuilder<T>). If the parameters can't be resolved, the builder type is left unchanged.Tests
Added
processor/src/test/java/org/mapstruct/ap/test/bugs/_2932/covering both builder shapes:shouldUseBuilderOfGenericTarget—Record<T>with a staticbuilder()factory (mirrors what Lombok generates for@Builderon a generic class).shouldUseConstructorCreatedBuilderOfGenericTarget—Pair<K, V>whose builder is created through its public constructor.Both fail on current
main(4/4 failures across the JDK and Eclipse compilers) and pass with the fix.Verification done:
gh pr list --search, issue has no linked PRs)..javaonly).main(211b2be) by running the new test against the pre-fix code — all 4 iterations fail; they pass after the fix../mvnw -pl processor -am clean install: all 3559 processor tests pass (0 failures, 0 errors) and checkstyle passes. (Locally on JDK 25 theanimal-sniffercheck reportsTypeKind.MODULEin the untouchedType.java; this is unrelated to this change.)