Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Massively refactor - how to add final keyword to Java method argument

This might be a simple question: How can I massively refactor my Java code to make most of the method argument as "final"? This is to follow one of our "checkstyle" rule. We have thousands of Java files, so manually edit all of them seems not an acceptable solution to us.

I didn't find any such refactor option in IntelliJ. Anybody knows any tool that can help? Or any smart approach to achieve that?

like image 539
John Zhang Avatar asked Feb 20 '17 13:02

John Zhang


People also ask

Should we use final in method parameter?

The final keyword on a method parameter means absolutely nothing to the caller. It also means absolutely nothing to the running program, since its presence or absence doesn't change the bytecode. It only ensures that the compiler will complain if the parameter variable is reassigned within the method. That's all.

When should we use final keyword in Java?

The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".

Which of the following keywords is used to declare a parameter with final value?

Final Methods When a method is declared with final keyword, it is called a final method. A final method cannot be overridden.

What is a final argument in Java?

The final keyword when used for parameters/variables in Java marks the reference as final. In case of passing an object to another method, the system creates a copy of the reference variable and passes it to the method. By marking the new references final, you protect them from reassignment.


2 Answers

You can use IntelliJ's inspections mechanism for this:

  1. Navigate to Analyze->Run Inspection by Name
  2. Search for the "Local variable or parameter can be final" warning
  3. Make sure that "Report method parameters" is the only option checked.
  4. Select the root of the tree (it should read Local variable or parameter can be final"
  5. Click ALT+ENTER and select "make final". This should add the final modifier in all the missing places.

Once you've done this, you may want to enable this inspection in your IDE so it warns you about making further mistakes:

  1. Navigate to File->Settings->Editor->Inspections
  2. Search for the "Local variable or parameter can be final" warning
  3. Make sure that "Report method parameters" is the only option checked.
like image 186
Mureinik Avatar answered Oct 14 '22 13:10

Mureinik


In IntelliJ you can check the "Make generated local variables final" and "Make generated parameters final" options enabled.

In IntelliJ 2017.3 it's in Preferences -> Editor -> Code Style -> Java -> Code Generation tab.

enter image description here

like image 23
Christian Vielma Avatar answered Oct 14 '22 14:10

Christian Vielma