11

I'm migrating some pre Java 10 code and I'm wondering if IntelliJ offers a way to automatically refactor the code to replace the variable declarations that uses the actual type with var wherever it's possible.

The code is full of stuff like:

String status = "empty";
BigDecimal interest = BigDecimal.ZERO;
List<Future<Boolean>> results = es.invokeAll(tasks);
LocalDate start = LocalDate.of(2020, 1, 1);

And I would prefer:

var status = "empty";
var interest = BigDecimal.ZERO;
var results = es.invokeAll(tasks);
var start = LocalDate.of(2020, 1, 1);

I already looked in IntelliJ's settings (Code Style/Inspections) and couldn't find anything.

6
  • 5
    Why are you trying to do that? My take on "var" is that it's meant to make new code easier to write but what do you expect from refactoring existing code to use it?
    – Thomas
    Oct 22, 2020 at 12:20
  • 3
    The new parts use var and I prefer it. I want to keep a consistent style throughout the project.
    – Fábio
    Oct 22, 2020 at 12:29
  • 1
    Do the new parts use var everywhere, though? I think it often depends whether you should use it, e.g. MyComplexType bla = create() gives me more information than var bla = create(). The thing about old code is that you read it much more often and don't want to guess the types in such cases. So there is no good way to know whether to use var or not, and no way to automate these choices.
    – sfiss
    Oct 22, 2020 at 12:39
  • 2
    You may use inspection Local variable type can be omitted (Java | Java language level migration aids | Java 10 | Local variable type can be omitted). It provides the "fix all" fix that will help to change the types to var in the file. Oct 23, 2020 at 13:46
  • Olga Klisho, that's exactly what I was looking for. Can you put your comment in an answer so I can accept it?
    – Fábio
    Oct 23, 2020 at 15:16

3 Answers 3

9
  1. Go to File | Settings, there select Editor | Inspections, then under Java | Java language level migration aids | Java 10.

  2. Right click on Local variable type can be omitted and select Weak Warning or similar.

  3. Move Your cursor onto any of those warnings in Your code (highlighted grey), open quick fix context help (alt+enter), at Replace explicit type with 'var' move to the right and select Fix all 'Local variable type can be omitted' problems in file

Thanks for @olga-klisho for the idea (in comments)

3

I'm using IntelliJ IDEA 2021.3.2, but don't think the setting is new.

I've been struggling with this one myself.

It seems that the first time to install IntelliJ locally, by default, it will fall back to using traditional defining of variables (i.e. String s = new String();)

How I managed to change it into using var is after I declared something, for example new String(), either I pressed ⌥ Alt/Option+Enter to declare a variable for that declaration or by using ⌘ Command+⇧ Shift+V shortcut (I'm using Mac and classic Intellij key mapping, so YMMV) which activates declaration of a variable, this would show as follows:

change traditional declaration of variables with var

As you see, it suggest to hit that key combination shortcut or clicking on the settings button that would open a pop up like this one:

pop up from clicking the settings button

Make sure you have Declare var type selected and you should be good to go.

-3

Use the IntelliJ Edit -> Find -> Replace... option.

Or Ctrl + R

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.