Question: Is there a way to replace all var
declaration with actual types in IntelliJ IDEA?
Context: I implemented a large feature using vars but turns out all developers in the team are against using vars and I have to rework it back to actual types.
From the main menu, select Edit | Find | Find in Files Ctrl+Shift+F . In the search field, type your search string. Alternatively, in the editor, highlight the string you want to find and press Ctrl+Shift+F . IntelliJ IDEA places the highlighted string into the search field.
From the main menu, select Edit | Find | Search Structurally. In the Structural Search dialog, under the Java | Class-based node, select All Fields of a Class which will look for all field declarations and click Find. As a result, in the Find tool window, IntelliJ IDEA shows all the fields declared in our Java code.
From the main menu, select Navigate | Search Everywhere or press Shift twice to open the search window. By default, IntelliJ IDEA displays the list of recent files.
There is an inspection specifically for this:
You should carefully review the suggested fixes, as the inspection may well use the most specific type, e.g. ArrayList<Foo>
, whereas it may well be more appropriate to use List<Foo>
(there is no way for a tool to know that in all cases).
There is a complementary inspection called "Local variable type can be omitted", to replace explicit types with var
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With