3

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, version 2.2.0 of Save Actions plugin crashes with the launch of IntelliJ version 2021.3 EAP. This plugin would seem to be a good solution, once the crashing is sorted in future versions.
2

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.

1 Comment

Cool, I hope you don't mind I updated my answer, I figured IntelliJ had to offer these checkstyles but I didn't see them last night.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.