Pulak Agrawal
Pulak Agrawal

Reputation: 2481

Remove Unnecessary Dependencies in a java project

This problem is Java dependencies not static, but dynamic. I read this stackoverflow link, but this is about static dependencies and googling around gets me similar results.

So here is my problem:

This is a very big project (20K+ Java files) running for 10 years with 50+ developers now. Here re-architecture happens all the time, so you have a new stack and an old stack of layers. Most times, risk-mitigation means you cannot get rid of a class straightaway, instead the calle method keeps calls to the new and old class both. The standard instruction to developer being: the old class should not be called during execution of application in production environment.

Current workaround:

What they do is keep a "false flag" on the old class and pray it does not get called. If it does (as a developer may miss pointing to the new class) and things fail in production, then set the flag against the old class to true and everything is alright in the world. (this way they just change a property file which has the flag and do not have to recompile) After a year or so of such testing in prod, they let go of the old class file. Now this might not be the best approach technically, but in practice with a mission-critical app you don't take chances and nobody is capable of remembering/ knowing what these 20K classes are doing in Java or even functionally.

What I am looking for:

A tool or method of finding out unused classes.

  1. Now this is a chicken-egg in some ways as until you execute a certain rare scenario, you won't find out which old class is being called at runtime and hence the existing solution.

  2. If you use static code analysis tools like PMD etc. they won't tell you the unused ones as they are actually being called by some or the other class at least at code level.

  3. Maybe, and its just wild thoughts, some tool which traverses all dependency chains/trees starting from the top-level action layer, and gets the result

And of course, the reactive way is build a fully automated regression test suite and run all scenarios possible which unfortunately is not possible here owing to budget, human limitations etc.

What I need is a pro-active way which optimizes my code. Maybe this is wishful thinking that this is even possible, but I have a nagging sense that this problem is not random and sort of structured, so there might be a solution somewhere.

Any thoughts appreciated. !!

Upvotes: 1

Views: 1872

Answers (1)

piotrek
piotrek

Reputation: 14520

it may be impossible to have correct solution for this because of dynamic class loading and reflection. you can start from JBoss Tattletale. it is probably available also as a sonar plugin. it can show you unused jars, but it does only static code analyse so you will get some false errors. but when you go through the list, you can ignore things like spring-security etc. but when you find your own jar it is likely you will know if there is no dynamic class loading or reflection in that module

Upvotes: 2

Related Questions