Java: defining reflection code usage

We have a huge code base, and some classes are often used through reflection throughout the code. We can safely remove classes, and the compiler is happy, but some of them are used dynamically using reflection, so I cannot find them other than searching for strings ...

Is there any reflex explorer for Java code?

+6
source share
4 answers

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

(You may have to crack issues with the loading order of classes in system classes.)

+3
source

There is no simple tool for this. However, you can use code coverage. This gives you an account of the entire line of executable code. This can be even more useful when improving test code or removing dead code.

Reflections by definition are very dynamic, and you need to run the correct code to see what it will do. those. You must have reasonable tests. You can add an entry to all Reflection if you can access this code, or maybe you can use the tools of these libraries (or change them directly)

+4
source

I doubt that any such utility is readily available, but I could be wrong.

This is quite difficult, given that dynamically loaded classes (via reflection) can themselves load other classes dynamically and that the names of the loaded classes can come from variables or some kind of runtime input.

Your codebase probably does nothing. If this is a one-time search of search strings, this might be a good option. Or are you looking for calls for reflection techniques.

0
source

As other posters noted, this cannot be done with static analysis due to the dynamic nature of Reflection. If you use Eclipse, you may find this coverage tool useful and very easy to use. It is called EclEmma

0
source

Source: https://habr.com/ru/post/896626/


All Articles