Finding all Unused Public Methods in Java

Recently I had to do some unpleasant code cleanup work.

I’m currently working on a legacy database using Hibernate. Along the way, some of the developers decided to generate the mappings for some of the tables instead of writing the mappings only for the columns that they need. The result is that as we reach the release date, we have a bunch of mapped fields that are not used. There were over 800 mapped fields in the code base, and my job was to manually search for references on the those fields and see which ones could be deleted.

I had no desire to spend some number of days tracing the usage of over 800 values in a codebase, so I did some googling on whether a tool is out there that can help me. In particular, I wanted something the would scan the code base, and find usages on all public methods in a code base.

I found a bunch of forums where people debated whether such a tool should exist. I found several tools that will point out unused private methods. There are even tools that generate dependency graphs, which, at some point probably know what methods are getting called and which ones aren’t. However, I did not find anything that would just list unused public methods.

My next idea was that I could write an eclipse plugin. After all, eclipse has the logic built in for finding usages on one method, so it seemed like I should just be able to ask it to give me a syntax tree of some kind that I can traverse, calling that function on all the methods. On the plus side, it looks like eclipse has all the parts that I need and shouldn’t take a lot of time to write. On the downside, I’ve never written an eclipse plugin, and the effort looked nontrivial as a first time effort.

So, I put my head down and turned myself into a usage finding machine for 2 days. It sucked.

If you know of a tool that does this let me know, otherwise, I may take a stab at writing an eclipse plugin that does it.

Tags: , ,

7 Responses to “Finding all Unused Public Methods in Java”

  1. murphee Says:

    Script your task - either checkout EclipseMonkey, or use the interactive EclipseShell:
    http://eclipse-shell.sourceforge.net/
    (which gives you a choice of JRuby/Beanshell/Rhino).

    Basically: I guess what you could do is use the functionality of “Find all References”, and simply do that for all methods (you just get the CompilationUnit for the class, and that allows you to get classes and their members).

  2. Kris Kemper Says:

    Thanks dude, that looks like a good tool for what I want.

  3. Jim LoVerde Says:

    Depending on the comprehensiveness of your unit/integration/functional tests, you could run your tests over your codebase using a coverage tool like Emma or Cobertura and then use the reports to indentify getters/setters that were never invoked.

    It’s true that this might give some false positives if your tests aren’t complete. But that’s also true of a static code analyzer like Eclipse’s if you have anything in your application that might be calling those getters/setters via reflection.

  4. XM Says:

    Hey Kris, I’m facing a similar cleanup pain…
    did you manage to create the script for finding unused methods?

  5. Kris Kemper Says:

    Sorry man, I never got back to this again. I’m sure that if I ever have to do it again, I’ll attempt the Eclipse Shell approach.

  6. Joerg Spieler Says:

    Here is a Eclipse plugin to find unused public methods:
    http://www.ucdetector.org/

  7. Kris Kemper Says:

    Thanks Joerg Spieler! I’ll try it out. I’m glad to find out that a tool like exists.

Leave a Reply