-
Notifications
You must be signed in to change notification settings - Fork 210
Quick fix action to refactor field injection to constructor injection #522
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
This will take about 3-5 weeks to implement. |
I was hoping that this could be implemented inside of the Spring Boot language server, as the LSP offers something like quick fixes and things like that. Any thoughts around that approach? |
Hmm... I forgot that we already parse java sources and keep them in the cache to provide live hovers... I'll have another look at this. |
@martinlippert do we want to put a "problem" marker in the code (i.e. hint)? Code Action/Quick Fix would be available via the marker in this case... Not so great in VSCode, but i suspect there is some sort of a bug in the client: it does not show the range correctly - only underlines the first symbol: |
@BoykoAlex In general, both approaches seem to be fine with me. I would aim at some consistency with other quick fixes and refactorings when coding in Java in VSCode and Eclipse. What is the choice for most of the quick Java refactorings in VSCode? If that goes with the lightbulb, then we should follow that example. If this doesn't look great in Eclipse and doesn't align with the overall Java coding experience in Eclipse, we can spend some additional work on the LSP4E side to improve that experience. We integrated the code completions in a special way to make them work well in the Java editor as well, so that should be possible. But again, I would like to take a look at the existing refactorings in the same area and see how they work for Java in VSCode - and take that as a guiding example. |
This is available in the latest versions of the Spring Tools across Eclipse and VSCode extensions. |
@martinlippert which PRs are related to this issue? I wanted to try out the new Eclipse but could not find the Quick Fix. So I wanted to look at the code... |
@MahatmaFatalError This belongs to a broader effort that we are working on at the moment to integrate refactorings and code changes using https://github.com/openrewrite, so the code change part of this quick fix is actually implemented in https://github.com/openrewrite/rewrite-spring. Since this is an early stage and not yet meant for mainstream consumption, you need to enable this in the preferences via
When you put the cursor on But keep in mind, this is a very early, experimental feature and not yet optimized or meant for mainstream consumption. It is early work towards enabling a broader set of new Spring-related refactorings, validations, and automated code changes. Hope this helps. Feedback always welcome!!! |
Thanks for providing this background information.
Version: 4.16.0.RELEASE |
@MahatmaFatalError Rogue |
One comment around the
|
@BoykoAlex I can't reproduce it either with a real-world class. So maybe it was just a anomaly in the matrix ;)
@martinlippert really? OK official docs https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.spring-beans-and-dependency-injection say it is needed when there are multiple constructors. I personally would prefer always having the annotation, but if the best practice is different, OK for me. Another thing I noticed is that @Autowired(required = false) does not have any effect. I think it would make sense to separate mandatory beans in a required-args-constructor (only required = true) from an all-args-constructor (all beans). |
@MahatmaFatalError A parameter is to be added to a constructor if the constructor meets the following criteria:
Therefore, this refactoring doesn't add |
In general, field injection comes with some downsides http://olivergierke.de/2013/11/why-field-injection-is-evil/
Unfortunately, refactoring of old, grown code bases towards constructor injection is tedious. So I was looking for a refactoring automation (and apparently I was not the first one with this issue https://stackoverflow.com/questions/44008985/convert-spring-field-injection-to-constructor-injection-intellij-idea)
So in essence, a quick fix action on
@Inject
or@Autowired
fields to create a new or extend an existing constructor (depending on (required = false) flag) would be really nice.The text was updated successfully, but these errors were encountered: