tags:

views:

131

answers:

3

In the Java code for our application I'm pretty sure there are lots of classes and methods which have public access but probably only need package level access.

What I'd like to do is to tidy up our code on a package by package level, making only things that really need to visible outside each package public as this should simplify other refactoring that I want to do.

I was wondering if there was a tool available that could help me with this. Ideally it would analyse all the code and produce a report saying which classes and method had public access but are only called from within the same package.

The Find Usages option in Netbeans can help me with this, but running it by hand for every class and method and analysing the output line by line will take forever.

A: 

The netbeans SQE plugin may help.

It integrates FindBugs, PMD, CheckStyle and Lint4j into netbeans. This should provide warnings that public methods can be made package private and the like.

Ally Sutherland
FindBugs, PMD, CheckStyle and Lint4j seem to work on a class-by-class basis rather than looking at the application as a whole so I don't think they will help. They all look useful but none seem to have a check for the particular problem I'm looking for.The SQE plugin also includes Dependency Finder which looks promising, but again will show me dependencies but doesn't seem to have an exception report for too great an Access Level.
Dave Webb
+2  A: 

I've used ProGuard, which is byte code manipulation tool, to find dead code in the past. There's an example in the ProGuard manual on how to use it. Note the tool does a lot of other stuff as well and it's not really a typical use of it, but it's the only thing I know of that can scan an entire application for dead code.

Edit: So I misread the question. Perhaps UCDetector is what you want. It claims to be able to detect "code where the visibility could be changed to protected, default or private. It is tied to Eclipse though. I just tried UCDetector on my code base and it seems to do the job. Certainly detected some methods and classes that could have their visibility reduced.

safetydan
The code I'm looking for isn't dead; just having the wrong access level. I'm looking for methods which are "public void foo()" which could be "void foo()" not methods which can me removed entirely.
Dave Webb
Ah, sorry, misread your question. Perhaps http://www.ucdetector.org/ is what you want. It claims to be able to detect "code where the visibility could be changed to protected, default or private". Note that I haven't used it and it's tied to Eclipse.
safetydan
I just tried UCDetector on my code base and it seems to do the job. Certainly detected some methods and classes that could have their visibility reduced.
safetydan
+1, UCDetector works exactly for this purpose.
waxwing
+1  A: 

The cheap (and quite probably faster than installing a tool for a single pass) is to search and replace public class to class and similarly for abstract, final and interface. Recompile. Fix what breaks until it does compile.

Tom Hawtin - tackline
This could work for classes but not for methods. I may have a public method on a public class which I could make a package method. Changing all the public methods on every class in a package would be pretty time-consuming.
Dave Webb
If you want to do it on methods, then you have much bigger problems. Default access on members and constructors is useful as a poor man's friend, but IMO it's best to have as few friends as possible (IFYSWIM).
Tom Hawtin - tackline