Mass refactoring - how to add final keyword to Java method argument

This may be a simple question: how can I massage refactoring Java code to make most of the method arguments "final"? This must follow one of our checkstyle rules. We have thousands of Java files, so manually editing all of them seems like an unacceptable solution for us.

In IntelliJ, I did not find such a refactoring option. Does anyone know any tool that can help? Or any smart approach to achieve this?

+7
java intellij-idea refactoring checkstyle
source share
2 answers

To do this, you can use the IntelliJ inspection mechanism:

  • Go to Analyze-> Run Inspection by Name
  • Search for the warning "Local variable or parameter may be final"
  • Make sure that the "Report Method Parameters" option is checked only one.
  • Select the root of the tree (it should read Local variable or the parameter may be final "
  • Press ALT + ENTER and select "make final". This should add the final modifier in all missing places.

Once you do this, you can enable this check in your IDE so that it warns you of further errors:

  • Go to File-> Settings-> Editor-> Inspections
  • Search for the warning "Local variable or parameter may be final"
  • Make sure that the "Report Method Parameters" option is checked only one.
+17
source share

Disclaimer: I do not want to start the IDE war, and I do not use intelliJ, so there may be a way to do it there. But since you asked about a tool to do this, and manual effort could justify using another IDE for it, here you go into eclipse.

In eclipse, you can configure the clean up task to add finals where possible. The cleaning task can be launched in separate editor windows, as well as in the whole project from the "source" context menu.

+2
source share

All Articles