Pass
Pass

Reputation: 1501

Enforce "this" access

Due to some weird requirements (post processing of source code) I need to use "this." syntax instead of just the member variable name. Is there a way I can enforce static checks at compile time to ensure that I am using "this." instead of the shorter equivalent?

Upvotes: 11

Views: 1600

Answers (6)

Jim Blackler
Jim Blackler

Reputation: 23179

Eclipse offers the ability to configure the Clean Up tool to do this for you.enter image description here

Upvotes: 7

Peter Lawrey
Peter Lawrey

Reputation: 533780

IntelliJ has a code style check for Instance field access not qualified with 'this'

It also has the option correct this for a field, class or modules. This sort of thing could safe you having to touch many files yourself.

Upvotes: 2

Aravindan R
Aravindan R

Reputation: 3084

You should be able to use a static code analyzer like CheckStyle to acomplish this in compile time.

http://checkstyle.sourceforge.net/config_coding.html#RequireThis

Upvotes: 1

Thomas
Thomas

Reputation: 88747

You could use checkstyle to impose such requirements.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503070

In Eclipse you could go into Java / Compiler / Errors and warnings / Code Style and change "unqualified access to instance field" to Error instead of the default Ignore.

EDIT: Using a mixture of this change to the IDE (if you're using Eclipse, of course) and checkstyle as part of your build rules should suffice.

Upvotes: 19

Amir Raminfar
Amir Raminfar

Reputation: 34179

Have you ever used checkstyle? You can define your code style rule in xml and run it at compile time. There is also a maven plugin if you care.

Upvotes: 9

Related Questions