Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused public members in Eclipse

Tags:

java

eclipse

I have a class which has 1600 public static members. I must found and delete the unused ones. Is there a way to do it in Eclipse.

like image 469
rdonuk Avatar asked May 16 '14 12:05

rdonuk


People also ask

How do I find unused classes in Eclipse?

UCDetector (Unecessary Code Detector) is a Open Source eclipse PlugIn Tool to find unecessary (dead) public java code. It also tries to make code final, protected or private. Caveat: Cid mentions in the comments: UCDetector shall not work if there are interface implementations which will be known only at runtime.

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

Is there a way to see unused code in Eclipse?

Eclipse can show/highlight code that can't be reached. JUnit can show you code coverage, but you'd need some tests and have to decide if the relevant test is missing or the code is really unused. Show activity on this post.

How to find unused code in Java?

In theory, you can't deterministically find unused code. Theres a mathematical proof of this (well, this is a special case of a more general theorem). If you're curious, look up the Halting Problem. This can manifest itself in Java code in many ways:

How to find unused code in IntelliJ?

IntelliJ has code analysis tools for detecting code which is unused. You should try making as many fields/methods/classes as non-public as possible and that will show up more unused methods/fields/classes I would also try to locate duplicate code as a way of reducing code volume.

How to find unused classes in a list?

For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.


Video Answer


2 Answers

There exists a Eclipse plugin Unused Code Detector (http://www.ucdetector.org/). It can process a specific file or the entire project, searching for unused members/methods.

like image 54
VincentDS Avatar answered Oct 23 '22 02:10

VincentDS


Ucdetector: You can install a plugin from this link. It works pretty well we have been using it in all our projects. It generates a html report as well it can mark warnings in eclipse. It helps you find unused classes, methods and variables. Only thing it dosen't do well is finding if a class is used via reflection only or where instance is created via spring. In these cases it still shows that class is unused.

http://ucdetector.sourceforge.net/update

Another thing you can do to clean up other types of unsed code is GoTo Window>Preferences>Java>Compiler>Error/Warnings

enter image description here Now look for uncessary code section and tweak the settings as you desire to cleanup further.

I hope it helps :)

like image 8
vkg Avatar answered Oct 23 '22 03:10

vkg