Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java tool to reduce the visibility of methods

Tags:

java

eclipse

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.

like image 842
user1001630 Avatar asked Apr 09 '14 22:04

user1001630


People also ask

What's the default visibility of methods and fields in a Java class?

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.

How is the visibility of a variable controlled in Java?

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.

What is class visibility Java?

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.

What will be the visibility of a public class?

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.


2 Answers

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.

like image 140
Sergio Avatar answered Nov 09 '22 13:11

Sergio


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.

like image 25
Angelo Gargantini Avatar answered Nov 09 '22 13:11

Angelo Gargantini