Home > Back-end >  Does IntelliJ have feature for adding (or removing) 'this.' prefix in front of appropriate
Does IntelliJ have feature for adding (or removing) 'this.' prefix in front of appropriate

Time:10-26

Some people like to omit the this. prefix to calls, such as:

addColor( Color.PURPLE ) ;

… while other folks prefer to include explicitly the this. prefix, such as:

this.addColor( Color.PURPLE ) ;

Does IntelliJ offer any feature for automatically adding and/or removing the this. prefix within the code shown in the editor?

CodePudding user response:

IntelliJ has the following inspections to add a this qualifier to method calls and field references:

Java | Code style issues | Instance field access not qualified with 'this'
Java | Code style issues | Instance method call not qualified with 'this'

And the following inspection to remove unnecessary this qualifiers:

Java | Code style issues | Unnecessary 'this' qualifier

All three inspections have a quick fix, and code can be fixed in batch using Code | Inspect Code and applying the fix on the result or Code | Cleanup to immediately clean up the code.

The Save Actions plugin just calls these built-in inspections.

CodePudding user response:

Save Actions plugin

The plugin, Save Actions, offers this functionality.

When installed (and activated) you can find these options under Other Settings -> Save Actions:

  • Add this to field access
  • Add this to method access
  • Remove unnecessary this to field and method

The plugin can be configured to execute these actions on save / idling / window deactivation.

Built into IntelliJ

InteliJ IDEA itself offers these checkstyle functionalities:

Ctrl Alt S -> Editor -> Inspections -> Code style issues:

  • Unnecessary 'this' qualifier
  • Instance field access not qualified with 'this'
  • Instance method call not qualified with 'this'

To inspect the code and apply the fix: Code -> Inspect

To auto apply fixes: Code -> Cleanup

  • Related