amphibient
amphibient

Reputation: 31278

How to find where Class1 is referenced in Class2 using reflection?

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.

Upvotes: 1

Views: 62

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299078

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)

Upvotes: 1

Louis Wasserman
Louis Wasserman

Reputation: 198321

Java reflection cannot inspect the internal implementation of methods and classes; only the external API. This cannot be done with reflection.

Upvotes: 2

Related Questions