Is there a tool in eclipse, a plugin for eclipse, or an external program that can automatically limit access modifiers?

人盡茶涼 提交于 2019-12-07 06:00:52

问题


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.


回答1:


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




回答2:


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)




回答3:


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.



来源:https://stackoverflow.com/questions/16816405/is-there-a-tool-in-eclipse-a-plugin-for-eclipse-or-an-external-program-that-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!