I have a legacy code base that I want to refactor to reduce the visibility of methods to the minimum possible (private, protected, default) such that the code still works. Many of the methods in the codebase are unnecessarily public, and I'd like to change that to reduce the interface burden and simplify documentation as the code evolves in the future. Is there a tool that will analyze the codebase and generate a list of suggested methods whose visibility can be reduced? I can specify all the entry points into the code (just the main methods), and the codebase doesn't use reflection.
An Eclipse plugin will be even better.
Java has four levels of visibility: public, protected, (default), private. The meaning of these is as follows: public - makes your methods accessible to any other class. protected - makes your methods accessible to any class in the same package OR any subclass of your class.
1) Any variable declared in a method is only visible in that method. (method-local). The programmer has no choice in that. 2) Any variable declared with the modifier private is visible only from within instances of the class it is delared in.
Interfaces behave like classes within packages. An interface can be declared public to make it visible outside of its package. Under the default visibility, an interface is visible only inside of its package. There can be only one public interface declared in a compilation unit.
A public class is accessible to all and most visible, try to keep public only key interfaces, never let the implementation go public until you believe it's complete and mature. The private type, on the other hand, is less visible, and in Java, only the nested class or interface can be private.
Although there are tools that you can use to report the occurrence of methods which visibility can be reduced, I am not aware of something that allows you to transform the code to solve those issues.
However, you may find interesting taking a look to JTransformer and Ekeko. Both allows you to query and accomplish custom code transformations based on logic programming techniques. JTransformer may be a bit more mature, but Ekeko also looks quite interesting. To the best of my knowledge they both are open source and include an Eclipse plugin.
Take a look at UCDetector (Unnecessary Code Detector - pronounced "You See Detector") http://www.ucdetector.org/ It is an eclipse plugin and it helps to reduce visibility. I have used it for long time and it works very well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With