I am trying to run some primitive static code analysis and, for starters, I want to find all the references to Class1 in Class2, similar to how an IDE find usage for a class (e.g. methods and line numbers). Just browsing throw the reflection javadoc, I was not able to detect a way.
Asked
Active
Viewed 59 times
2 Answers
2
Java reflection cannot inspect the internal implementation of methods and classes; only the external API. This cannot be done with reflection.

Louis Wasserman
- 191,574
- 25
- 345
- 413
1
You can however do it with a technology that understands byte code analysis. asm is an example of such a technology.
You'd create a ClassVisitor
with a MethodVisitor
that does its magic in visitMethodInsn(int, java.lang.String, java.lang.String, java.lang.String)
But I'd say it's way easier to use an IDE and do a Usage search (both Eclipse and IntelliJ do that very well)

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
-
do the IDEs use that API ? – amphibient Nov 15 '13 at 17:43
-
also, do you know of any alternatives to this approach? – amphibient Nov 15 '13 at 18:27
-
1Ides work with source code, they generate such lookups based on the abstract syntax tree. The approach is pretty similar, you'll write visitors for method invocations, either on source or byte code – Sean Patrick Floyd Nov 15 '13 at 19:50