Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce "this" access

Tags:

java

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?

like image 478
Pass Avatar asked Apr 13 '11 15:04

Pass


4 Answers

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.

like image 76
Jon Skeet Avatar answered Nov 16 '22 22:11

Jon Skeet


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.

like image 9
Amir Raminfar Avatar answered Nov 16 '22 23:11

Amir Raminfar


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

like image 7
Jim Blackler Avatar answered Nov 16 '22 22:11

Jim Blackler


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.

like image 2
Peter Lawrey Avatar answered Nov 16 '22 22:11

Peter Lawrey