Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch refactoring to make Java method arguments final

I'm looking around for a way to perform batch refactoring on a complete Java application. In this namely make method arguments final where it's not the case yet.

Does somebody in here knows about such a tool ? Or something that parses Java source and that can be extended with such changes.

like image 426
Jan Goyvaerts Avatar asked Dec 17 '22 08:12

Jan Goyvaerts


2 Answers

You can do a bulk change in IntelliJ to change every field, local variable or parameter which can be final to be final.

Do a Code Analysis with the option, and "Apply Fix" globally, make sure it still compiles as it doesn't always get it 100% right in odd cases.

like image 96
Peter Lawrey Avatar answered Dec 26 '22 18:12

Peter Lawrey


As Peter Lawrey suggests, IntelliJ does that.

Analyze -> Inspect code -> custom profile

There, in the "Code style issues" section, you have:

Field may be final

This inspection reports any fields which may safely be made final. A static field may 
be final if it is initialized in its declaration or in one static class initializer, but 
not both. A non-static field may be final if it is initialized in its declaration or in 
one non-static class initializer or in all constructors.
Powered by InspectionGadgets

Local variable or parameter can be final

This inspection reports parameters or local variables, found in the specified inspection
scope, that may have a final modifier added.

Use check boxes in the inspection options below, to define whether parameters or local
variables (or both) are to be reported.

That will propably only make final the variable that can safely be so but the ones that you're trying to spot will remain non-final. Still, it's a way to spot them.

like image 39
Eric Darchis Avatar answered Dec 26 '22 17:12

Eric Darchis