7

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?

2
  • how are the classes being used dynamically are they loaded using their full name as a string or being used in a script that's evaluate at runtime? Commented Sep 5, 2011 at 8:31
  • Avoid doing that if at all possible. The reason typesafe languages like Java are so powerful and used in enterprise systems is that the compile time type checking can find most type conflicts at compile time - except if you use reflection to do certain things - then all bets are off and you are back to the dark ages of programming where your code is embedded with 'mines' that can go off at any time when one of your users just happens to exercise a piece of the code in a way that you hadn't thought of testing for. Commented Feb 24, 2017 at 10:21

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

Comments

3

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.)

2 Comments

But this will only find what has been loaded in that particular run of the program. What I suggested was to do the same thing statically, and it does not require going through the pain of modifying the classes. Interested in your comments. Thanks.
@Hemal Kind of depends what reflection is being used for. The more useful uses of reflection wouldn't have the strings hardcoded. If the strings are hardcoded, then I'#d suggest grepping for java.lang.reflect and removing the reflection.
0

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

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

Your codebase probably does neither of these. If this a one time effort searching strings might be a good option. Or you look for calls to reflection methods.

Comments

0

As the other posters have mentioned, this cannot be done with static analysis due to the dynamic nature of Reflection. If you are using Eclipse, you might find this coverage tool to be useful, and it's very easy to work with. It's called EclEmma

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.