Bug 172595 - [search] Feature proposal: Identify all unused classes, methods etc within project/package
Summary: [search] Feature proposal: Identify all unused classes, methods etc within pr...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
: 480620 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-02-01 21:06 EST by Andreas Klein CLA
Modified: 2018-10-20 13:59 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Klein CLA 2007-02-01 21:06:36 EST
While the existing References search feature is without question very handy, at times it would be excellent to have an overview of all classes, interfaces, methods and fields not used, or more commonly no longer in use, within a package or entire project thus helping identifying obsolete code.

While there exists an expensive commercial solution for, amongst others, this request called RefactorIT, with features such as besaid FindReferencesInProjectAction currently implemented, I thought it might not be an overly far step to running a search on all classes, methods etc. in a package or project and, as opposed to the References Search, list those that have no references.

RefactorIt further supports a series of helpful search criteria, such as the exclusion of getter and setter accessor methods, overriding methods or JUnit test cases. Naturally this exclusion option could make the search both faster and give better results as to the goal of cleaning up the code.
Comment 1 Martin Aeschlimann CLA 2007-02-13 08:44:39 EST
No plans to add this to JDT. This would be a nice idea for an external plugin.
Comment 2 Denis Roy CLA 2009-08-30 02:16:21 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.
Comment 3 Dani Megert CLA 2015-10-26 07:07:00 EDT
*** Bug 480620 has been marked as a duplicate of this bug. ***
Comment 4 Lars Vogel CLA 2015-10-26 07:15:31 EDT
(In reply to Denis Roy from comment #2)
> As of now 'LATER' and 'REMIND' resolutions are no longer supported.
> Please reopen this bug if it is still valid for you.

We have such an action in platform.runtime and could contribute it. Having the option to search for unused methods in the current workspace would be valuable to several customers I know of. 

(Also this bug is marked as helpwanted).
Comment 5 Dani Megert CLA 2015-10-26 07:31:16 EDT
(In reply to Lars Vogel from comment #4)
> (In reply to Denis Roy from comment #2)
> > As of now 'LATER' and 'REMIND' resolutions are no longer supported.
> > Please reopen this bug if it is still valid for you.
> 
> We have such an action in platform.runtime and could contribute it.

If we add such a feature, it has to be done like all other Java element searches in JDT: implemented in JDT Core, exposed as API, and offered in the UI
- as Search > Unreferenced > [Workspace|Project|Package|Hierarchy|Working Set...]
- on the Java Search page

Is that what you are willing to contribute? If not, the better place is in an external tools plug-in as outlined in comment 1.
Comment 6 Lars Vogel CLA 2015-10-26 07:44:53 EDT
(In reply to Dani Megert from comment #5)
> If we add such a feature, it has to be done like all other Java element
> searches in JDT: implemented in JDT Core, exposed as API, and offered in the
> UI
> - as Search > Unreferenced > [Workspace|Project|Package|Hierarchy|Working
> Set...]
> - on the Java Search page

I don't think new API in JDT core is need, the existing one can be used. As for the integration into the search page, I think that makes sense. Do you also want to have it in the context menu?

> Is that what you are willing to contribute? 

Sounds good to me, except that I do not think that new core API is needed.
Comment 7 Dani Megert CLA 2015-10-26 09:18:33 EDT
(In reply to Lars Vogel from comment #6)
> (In reply to Dani Megert from comment #5)
> > If we add such a feature, it has to be done like all other Java element
> > searches in JDT: implemented in JDT Core, exposed as API, and offered in the
> > UI
> > - as Search > Unreferenced > [Workspace|Project|Package|Hierarchy|Working
> > Set...]
> > - on the Java Search page
> 
> I don't think new API in JDT core is need, the existing one can be used. As
> for the integration into the search page, I think that makes sense. Do you
> also want to have it in the context menu?

Yes, it should appear where all other searches like e.g. Declarations or References searches appear.


> > Is that what you are willing to contribute? 
> 
> Sounds good to me, except that I do not think that new core API is needed.

It needs to fit into our normal search architecture, hence it is needed, or won't be accepted. Sorry. Your call whether you want to work on it or not.
Comment 8 Lars Vogel CLA 2015-10-26 10:18:18 EDT
(In reply to Dani Megert from comment #7)
> It needs to fit into our normal search architecture, hence it is needed, or
> won't be accepted. Sorry. Your call whether you want to work on it or not.

No worries, I try to have a look during 4.6. I first need to learn about the JDT search architecture. I suggest to leave that bug open and unassigned in case someone else want to help.
Comment 9 Stephan Herrmann CLA 2015-10-26 17:31:17 EDT
Some time back I wrote a little hack on top of the compiler that would perform the necessary call-graph analysis during a full build, to check if a method was unreachable from a given set of entry points. The implementation would faithfully recognize unreachable code even in the presence of a cyclic call structure (isolated islands).

I guess the main reason I never proposed to include this in JDT lies in the difficulties to identify the full set of entry points. If you have an application it's just "main", but if you write a library / framework that would be at least all public API, but for classes that can be extended by clients, even all protected methods would be possible entry points etc.  - Ergo: it is comparably easy to implement the core of such analysis, but not obvious how users would tell the tool from which entry points to start.

I understand the search-based strategy takes a different approach. It will probably not detect isolated islands, and instead of starting from a set of entry points, it will probably filter from the search result all methods that have a good excuse for not being referenced? Am I describing it correctly?
Comment 10 Timo Kinnunen CLA 2015-10-29 22:49:25 EDT
(In reply to comment #9)
> Some time back I wrote a little hack on top of the compiler that would perform
> the necessary call-graph analysis during a full build, to check if a method was
> unreachable from a given set of entry points. The implementation would
> faithfully recognize unreachable code even in the presence of a cyclic call
> structure (isolated islands).

Oh, this would have been so useful for so many times!

> I guess the main reason I never proposed to include this in JDT lies in the
> difficulties to identify the full set of entry points. If you have an
> application it's just "main", but if you write a library / framework that would
> be at least all public API, but for classes that can be extended by clients,
> even all protected methods would be possible entry points etc.  - Ergo: it is
> comparably easy to implement the core of such analysis, but not obvious how
> users would tell the tool from which entry points to start.

This feature should also be considered from a Java 9 module world perspective. If I understand correctly, with modules it becomes possible to create module-private APIs that are public internally only for crossing over a package boundary. Consuming what will be specifying this could be a starting point. 

Apart from that, looking for any @Documented annotations to indicate a public API member would already provide a consumer-extensible mechanism to mark public or reflection-accessible APIs. @SuppressWarnings("unused") would also fit nicely into this to provide a lighter-weight alternative under a well-known name.