I used to be incredibly bad at limiting access of my variables/methods/classes
, I tended to use public a hell of a lot when I shouldn't.
I was just wondering if there was any tool - plugin, external or otherwise - that can search your source code, find what calls your variables/methods/classes
and changes the visibility if it's too high.
So for example, if I had a public variable and nothing outside that class called it, then the tool would reduce its access to private.
Mainly I need this for some of my older projects that have to many public variables. It would take far to long for me to sift through all of them and would be extremely annoying/inefficient to leave them public, when I come back to these projects to work on them again.
Take a look at the UCDetector: Unnecessary Code Detector Eclipse plugin. It will create markers for the following problems (which appear in the Eclipse problem view):
- Unnecessary (dead) code
- Code where the visibility could be changed to protected, default or
private
- Methods of fields, which can be final
It also has support for Eclipse QuickFixes to automatically adjust member visibility
I've never heard or read about something like what you are asking for, but maybe PMD can help you: it won't fix your problems, but it will point at them. Next time use private for all your attributes and create getters and setters (in eclipse, press Atl+Shift+R and then 's' to do it automatically)
There are many code quality tools designed to pick up on overly-broad access like this: Sonar, PMD, FindBugs...however, they will not modify the original source for you. The focus should be to improve your habits, and use the tools to remind you of places where you have faltered mistakenly, not use some plugin as a crutch for bad habits.