Adding "this." qualifier.

Hi,

I've just switched to intellij idea from eclipse. And have a question regarding to Code Formatting.

In eclipse I was able to check "Use 'this.' qualifier for field accesses" and "Use 'this.' qualifier for method accesses" and on each code cleanup 'this' qualifiers are added to methods and fields accesses.
Does IDEA have such capabilities?

5 comments
Comment actions Permalink

There is an "Instance field access not qualified by 'this.' inspection, that will do what you want for fields.  No such functionality exists for method calls, as far as I know.

--Dave Griffith

0
Comment actions Permalink

a bit off topic, still related to your question (I am just interested):
Why do you want to enforce the  'this.'  qualifier? In my view it makes it in terms of java readability even more verbose. With good syntax highlighting (you can even tweak it very well in idea) I think enforcing 'this' does not improve the code readability. So apart from cases where 'this' is semantically necessary (e.g. in setters when param has the same name) I completely skip it.

0
Comment actions Permalink

I can see where adding 'this.' qualifiers to field accesses might be useful [1]. Qualifying method calls with 'this.' is less useful in my opinion. To make it easier to detect the difference between a call to an instance method or a static method, it takes less effort to qualify the (less common) static method calls with the name of their containing class [2].
Am I missing something? Given a good use case I might implement an additional inspection to help qualify method calls with 'this.'

Bas

[1] field accesses and local variable or parameter accesses are easier to distinguish
[2] see the "unqualified static usage" inspection

0
Comment actions Permalink
[1] field accesses and local variable or parameter accesses are easier to distinguish

for that i did:  static -> pink, instance -> violet, parameters -> bold-blue, local-vars -> non-bold blue

[2] see the "unqualified static usage" inspection

Unqualified static usages happen very rarely to me, because I use idea auto-complete nearly all the time. In the end a lot also gets automatically done by using static imports. The static method-calls I see with the italic highlighting.

0
Comment actions Permalink

Sergey, there is a plugin call this-inserter that does what you want. It works for member variables and methods. The plugin is available through the plugin settings dialog.

Please note it doesn't currently work for IntelliJ 9.x, only 8.x. Once I get my 9.0 license I will update the plugin to work with 9.0.

The plugin does not add the "InnerClass.this.member" qualifier for outer class members accessed in the inner class (most people don't like that anyway). Maybe I will make that optional in the future.

0

Please sign in to leave a comment.