Fix generic type loss in AutowiredFieldIntoConstructorParameterVisitor (fixes #1005)#1006
Open
dim-kod wants to merge 3 commits intoopenrewrite:mainfrom
Open
Fix generic type loss in AutowiredFieldIntoConstructorParameterVisitor (fixes #1005)#1006dim-kod wants to merge 3 commits intoopenrewrite:mainfrom
dim-kod wants to merge 3 commits intoopenrewrite:mainfrom
Conversation
…r parameters Both AddConstructorVisitor and AddConstructorParameterAndAssignment used TypeUtils.asFullyQualified(type).getClassName() to render the field type in the generated constructor parameter, which dropped generic type parameters (e.g. List<String> became raw List). Use TypeTree.toString() instead, which leverages JavaPrinter to produce the full source-level type string including generics, wildcards, and nested type parameters. Also add addImportsForType/collectFullyQualifiedNames helpers to register type parameter FQNs with the JavaTemplate builder.
The type guard used TypeUtils.asFullyQualified() which returns null for JavaType.Array, preventing constructor generation for array-typed fields. Replace with a null + primitive check so arrays (and other non-primitive types) are accepted. Also add tests for @Autowired(required = false) and fields with multiple annotations.
…-type-loss # Conflicts: # src/main/java/org/openrewrite/java/spring/AutowiredFieldIntoConstructorParameterVisitor.java
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 issue #1005
What's changed?
AutowiredFieldIntoConstructorParameterVisitornow preserves generic type parameters when converting@Autowiredfields to constructor parameters.Both
AddConstructorVisitorandAddConstructorParameterAndAssignmentwere usingTypeUtils.asFullyQualified(type).getClassName()to render the field type in the generated constructor, which strips generics — soList<String>ends up as rawList.The fix switches to
TypeTree.toString(), which goes throughJavaPrinterand gives you the full source-level type string. I also addedaddImportsForType/collectFullyQualifiedNameshelpers to register type parameter FQNs with theJavaTemplatebuilder.A second commit broadens the type guard from
TypeUtils.asFullyQualified() == nulltonull || Primitive— this fixes a separate issue where array-typed fields likeString[] awere silently skipped during constructor generation.What's your motivation?
Any
@Autowiredfield with a parameterized type ends up with a raw-type constructor parameter after the recipe runs, which produces compiler warnings and loses type safety.Anything in particular you'd like reviewers to focus on?
TypeTree.toString()approach — it usesJavaPrinter.printTrimmed()under the hood and already handles all type forms (parameterized, wildcards, bounded wildcards, nested generics, arrays).Primitivecheck instead ofasFullyQualified == null) — this intentionally letsJavaType.Arraythrough.Have you considered any alternatives or workarounds?
renderTypeSimple(JavaType)that recursively builds the simple-name type string fromJavaType— works, but duplicates whatTypeTree.toString()already does.TypeUtils.toString(JavaType)— produces fully qualified names (e.g.java.util.List<java.lang.String>), not source-level simple names, so it doesn't work for template strings.Any additional context
Test coverage went from 11 to 22 tests. New tests cover:
List<String>)Map<String, List<Integer>>)List<?>), upper-bounded (List<? extends Number>), lower-bounded (List<? super Integer>)MyService<MyConfig>)String[])@Autowired(required = false)@Autowired @Qualifier("myBean"))Checklist