Batch refactoring to make Java method arguments final

I am looking for a way to do batch refactoring in a full Java application. In this case, make make the final arguments, where this is not the case.

Does anyone here know about such an instrument? Or something that parses a Java source and can be extended with such changes.

+4
source share
3 answers

You can make a bulk change in IntelliJ to change each field, local variable, or parameter, which can be the final final.

Do the code analysis with the parameter and Apply Correction globally, make sure that it still compiles, since it does not always get 100% rights in odd cases.

+7
source

According to Peter Laurie, IntelliJ does this.

Analysis -> Check Code -> Custom Profile

In the section "Problems with the code style" you can:

The 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 

A local variable or parameter may 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. 

This will only simplify the final version, which may be safe, but those that you are trying to detect will remain invalid. However, this is a way to detect them.

+4
source

I do not know such refactoring in Eclipse or NetBeans. But replacing normal expression would replace the trick. To ensure that you do not accidentally perform it in places where this should not occur, you may need to confirm each replacement manually. This may not be realistic if you have hundreds of classes. In this case, using a replacement everywhere and then checking diff with an older version might be useful.

If any argument is not final, as it will be overwritten, it will become clear at compile time.

0
source

All Articles