Community
Participate
Working Groups
JDT provides unused code detection for private methods and fields. However, AFAIK there no build in detection for unused code that is protected or private. Having detection for dead/unused code would be really beneficial, for instance, Eclipse Platform UI has dozens of unused internal classes (e.g. Bug 564144). UCDetector [1] is an Eclipse plugin that provides such kind of analysis already, though its maintenance seems low. It heavily depends on functionality provided by Eclipse. Though, some integration could be better, such as able to distinguish between exported and not-exported packages. [1] http://www.ucdetector.org/
We have a dead code detector built into our flow analysis, check the code below: public class X { protected void foo(int i) { if (true) { return ; } if (i > 0) { // Dead code [warning] return ; } } } Could you give a specific code snippet example for your requirement?
I know the detection for dead code within a class. But there is no detection of, for example, *public* classes, methods or fields which have no references. Surely it is valid to have such classes/methods/fields for API that is provided. For non-API this is a smell that the code is not used (disclaimers aside for reflection, special cases of text references). Read the description of the UCDetector project. Also IntelliJ provides similar functionality [1]. Surely some configuration is required here, that can filter out common false positives, and result in confusion if enabled always [2]. But, for example, for most OSGi bundles it should be quite well defined what is API and what not. [1] https://stackoverflow.com/questions/6587729/how-to-use-intellij-idea-to-find-all-unused-code [2] https://stackoverflow.com/questions/6166334/disable-not-used-warning-for-public-methods-of-a-class
I just learned that the Code Minings already provide functionality to count the number of references to types/fields/methods, among others. Everything without a reference is potentially unused code.
This comes under the purview of jdt/ui - moving to jdt/ui
*** Bug 563661 has been marked as a duplicate of this bug. ***
org.eclipse.core.tools contains code for finding unused code. Also we could contact the developers of UCDetector if they are interested in contributing this to JDT.
The eclipse.platform.runtime repo has the org.eclipse.core.tools project which contains a implementation to find unused code.