5

I am concerned that some of the classes in my app have methods that are defined but not called anywhere in the app.

In Eclipse is there a way to find these methods?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Daniel K.
  • 105
  • 2
  • 5
  • 1
    It's not elegant, but you could do a search for them...if they appear more than once, they've been called. Obviously, this would be incredibly time consuming for any more than a couple of methods... – Chris Thompson Nov 03 '10 at 19:53
  • Use Intellij instead. (Just kidding. (Not Really.)) – Jeremy Nov 03 '10 at 20:30

3 Answers3

10

You can use Findbugs for this, but there's also a plugin dedicated to this: UCDetector.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    +1, and thanks for the link to UCDetector. Of course, this isn't likely able to analyze the runtime behaviour of your code to know certain circumstances (e.g. if a certain override of a certain method is ever going to be called--for instance, any particular `toString()` override). – Mark Peters Nov 03 '10 at 19:56
  • 1
    @Mark: UCDetector homepage also disclaims that it would only generate suggestions since the code might be referenced directly or indirectly by reflection (and frameworks using reflection under the covers like EL, JPA, javabean tools, etc). Not entirely useful plugin for enterprise applications imo, but may be useful for simple client Java applications. – BalusC Nov 03 '10 at 19:58
0

Here's a related question and answers and a few other helpful links;

Find unused classes in a Java Eclipse project How to find unused/dead code in java projects http://www.velocityreviews.com/forums/t389551-identify-unused-methods.html

http://pmd.sourceforge.net/eclipse/

Hope that helps.

Community
  • 1
  • 1
Damien Roche
  • 13,189
  • 18
  • 68
  • 96
0

The quickest way to find usages of a single method would be to highlight the method, right click and go to "References", from there you can choose search location like workspace, project etc.

Obviously this wouldn't be a good way to find ALL the methods in the project that aren't being used, but it might be worth using if you just looking at one odd looking method that you can't figure out why you need.

Andy
  • 8,841
  • 8
  • 45
  • 68