Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Locate reflection code usage

We have huge codebase and some classes are often used via reflection all over the code. We can safely remove classes and compiler is happy, but some of them are used dynamically using reflection so I can't locate them otherwise than searching strings ...

Is there some reflection explorer for Java code?

like image 988
Xorty Avatar asked Sep 05 '11 08:09

Xorty


2 Answers

No simple tool to do this. However you can use code coverage instead. What this does is give you a report of all the line of code executed. This can be even more useful in either improving test code or removing dead code.

Reflections is by definition very dynamic and you have to run the right code to see what it would do. i.e. you have to have reasonable tests. You can add logging to everything Reflection does if you can access this code, or perhaps you can use instrumentation of these libraries (or change them directly)

like image 79
Peter Lawrey Avatar answered Oct 19 '22 22:10

Peter Lawrey


I suggest, using appropriately licensed source for your JRE, modifying the reflection classes to log when classes are used by reflection (use a map/WeakHashMap to ignore duplicates). Your modified system classes can replace those in rt.jar with -Xbootclasspath/p: on the command line (on Oracle "Sun" JRE, others will presumably have something similar). Run your program and tests and see what comes up.

(Possibly you might have to hack around issues with class loading order in the system classes.)

like image 28
Tom Hawtin - tackline Avatar answered Oct 19 '22 20:10

Tom Hawtin - tackline