1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
amphibient
  • 29,770
  • 54
  • 146
  • 240

2 Answers2

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