Preserve generic type parameters in AutowiredFieldIntoConstructorParameterVisitor#1037
Merged
Merged
Conversation
…onstructor parameters `AutowiredFieldIntoConstructorParameterVisitor` rendered the generated constructor parameter with `FullyQualified.getClassName()`, which strips type arguments, so `List<String>` became a raw `List`. Render the field's source-level type instead (`TypeTree` text for the new-constructor path, `TypeUtils.toString(..)` plus `ImportService` shortening for the existing-constructor path) and let array-typed fields through the guard. Co-authored-by: dim-kod <278075964+dim-kod@users.noreply.github.com>
3 tasks
timtebeek
approved these changes
Jun 15, 2026
timtebeek
left a comment
Member
There was a problem hiding this comment.
Nice to see this minimal fix, thanks!
|
Hello @MBoegers, I am satisfied with the result. It is always better to have simpler fixes! Thank you once again, closing the other PR. |
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.
Closes AutowiredFieldIntoConstructorParameterVisitor loses generic type parameters #1005.
Big thanks to @dim-kod for the upfront investigation and the clear reproduction in AutowiredFieldIntoConstructorParameterVisitor loses generic type parameters #1005 / Fix generic type loss in AutowiredFieldIntoConstructorParameterVisitor (fixes #1005) #1006 — it pinned down the root cause and surfaced all the edge cases (nested generics, wildcards, arrays) that this needed to handle. This is a smaller take on the same fix, leaning on existing utilities.
What's changed?
AutowiredFieldIntoConstructorParameterVisitorrendered the generated constructor parameter type withTypeUtils.asFullyQualified(type).getClassName(), which drops type arguments — so an@Autowired List<String>field became a rawListparameter (and array fields were skipped entirely).AddConstructorVisitor): render the field'sTypeTreesource text directly in the template. Because the template iscontextSensitive()and the field stays in the class, its imports are already present — no import registration needed.AddConstructorParameterAndAssignment): thereplaceParameters()template is parsed without the file's imports, so the parameter is rendered fully qualified viaTypeUtils.toString(..)and then simplified withservice(ImportService.class).shortenFullyQualifiedTypeReferencesIn(..)— the same idiomChangeMethodParameteralready uses in this repo. This also fixes array fields into an existing constructor, which previously threw.null/primitive types instead of everything without aFullyQualified, letting arrays and type variables through.Why this shape
The fix itself is just rendering the type from the LST instead of
getClassName(). No recursive FQN-collection helper is needed: imports are already in scope on the new-constructor path, andImportServicehandles them on the existing-constructor path. Net production change is ~23 lines.Tests
Covers generics into new/existing/parameterized constructors, nested generics (incl. into an existing constructor, under full type validation), bounded wildcards, user-defined generic types across packages, and arrays. Two tests relax
TypeValidationto document currentJavaTemplatelimitations (the template parser can't resolve sibling in-run sources; array assignments are mis-attributed upstream) — each annotated with the precise reason.