3

I am trying to create an index of unused Java methods in the form of a json file.

There are a couple different ways in which the methods can be referenced. I have already checked for all the other ways and have a relatively small list of possibly unused java methods.

The final way in which a method can be used is in other java files. They would be called with a basic class.method(args,args2,etc...) syntax somewhere in the java source code.

My question is, is there an easy way to just check my list of possible unused methods to see if any of them are not used in the java code. It would be ideal if this could be done at runtime, but it would also work if I could create a file that I could then read in at runtime.

I have tried using pre-built software like UCDetector, but the source code is huge, and running UCDetector takes hours and often doesn't even finish. It also checks all methods to see if they are used which is a waste of time since I have narrowed it down to a small number of possible methods to check.

  • 1
    your IDE should tell you if a method is not used at all. – StackFlowed Aug 31 '15 at 15:40
  • 2
    What are trying to solve? –  Aug 31 '15 at 15:42
  • 1
    It does, and that's my backup... But I would really like to have a defined list of all completely unused methods instead of a possible list which I then need to go check to see which is actually used. This script will likely be run quite often and it would great if it could be completely autonomous @StackFlowed – pythonHelpRequired Aug 31 '15 at 15:43
  • 1
    @StackFlowed unless those methods are accessed through reflection for instance. The IDE will then detect them as unused, but... – xlecoustillier Aug 31 '15 at 15:43
  • @X.L.Ant No need to worry about reflection currently – pythonHelpRequired Aug 31 '15 at 15:44
  • @X.L.Ant im not sure if refections should be used in production code it violates the OOP principles. – StackFlowed Aug 31 '15 at 15:44
  • @pythonHelpRequired You can set you IDE to give you a complier error and not just warning on those so you can fix them ! – StackFlowed Aug 31 '15 at 15:45
  • @StackFlowed Yea that would be nice. The problem is the code can be referenced in a lot of different ways which the IDE won't catch, so It will throw compiler errors for half the methods. – pythonHelpRequired Aug 31 '15 at 15:48
  • 1
    @StackFlowed Dependency injection (or inversion of control in general) often use reflection, and are widely used in prod. Anyway, was just saying. – xlecoustillier Aug 31 '15 at 15:49
  • @DraganBozanovic It doesn't need to be runtime. It's just the rest of the script that finds the possible list of unused methods has to be run at runtime, so it would be nice if it was just a one step process – pythonHelpRequired Aug 31 '15 at 15:50
  • @StackFlowed at least all DI and ORM libraries/frameworks use reflection and they are widely used in production – Alex Salauyou Aug 31 '15 at 16:15
  • 1
    Miscellaneous bean classes (accessed by reflection, like JPA, EL, DAOs) can probably be excluded. **FindBugs** should find unused methods too. You could check only that point to scan for. – Joop Eggen Aug 31 '15 at 16:26
  • Related, maybe even duplicate: http://stackoverflow.com/q/162551/2974766 – DoubleDouble Aug 31 '15 at 16:46
  • @SashaSalauyou, not all DI frameworks. http://google.github.io/dagger/ is fully compile-time. – Louis Wasserman Aug 31 '15 at 17:22
  • @LouisWasserman my bad English... "at least" <- "almost", sorry. – Alex Salauyou Aug 31 '15 at 20:03

2 Answers2

2

You should use your IDE (eclipse, intelliJ), or some static code analysis tool such as findbugs, pmd, checkstyle.

It seems like you are trying to reinvent the wheel.

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
  • I have tried some analysis tools. The source code is too large for them to work in a reasonable amount of time. I would be fine with using them as long as I could specify what methods they look at. Is there anyway I could do that? – pythonHelpRequired Aug 31 '15 at 17:19
0

One option might be to use "coverage analysis" tools to see what is not used (https://en.m.wikipedia.org/wiki/Java_Code_Coverage_Tools). If you have good branch coverage with your unit tests, simply running the tests with coverage will yield the result you're looking for. If you don't have good tests coverage, you might run the application itself with code instrumented for coverage calculation, but as with unit tests - the quality of the result will depend on the amount of code executed with your unit tetst or manual test.

Some examples of the coverage tools you might use are : JaCoCo (http://www.eclemma.org/jacoco/) and Cobertura (http://cobertura.github.io/cobertura).

Alternatively you might instrument your code yourself in order to log methods usage, as it might be more lightweight than calculating full line coverage. This is however indeed reinventing the wheel.

This SO question has similar solutions: How to find unused/dead code in java projects

Community
  • 1
  • 1
Tim
  • 12,318
  • 7
  • 50
  • 72