Reputation: 1313
Let's say I have the following classes:
class A {
static public String a;
}
class B {
public function referToFieldInClassA() {
System.out.println(A.a);
}
}
Is there anything in the Java reflection APIs to allow me to find all places where a particular field is referenced? What I'm looking for is a way to find out that (given the example) class B has a reference to A.a.
I know I can get all the Fields in a Class via the reflection API. But now I want to find all references to that Field.
Thanks.
Upvotes: 0
Views: 1491
Reputation: 9018
If your need is compile-time rather than run-time, and you don't have an IDE, the very old trick of changing the names of the fields and recompiling, and searching for what fails to compile. ;]
Then, of course, get yourself an IDE.
Upvotes: 1
Reputation: 40358
From the standard APIs, I think the answer is no. Eclipse can do it, but that's not at runtime.
For an incredibly hack-ish way of doing it, get the ClassLoader object, get the resourceAsStream to get something, use something like JODE to decompile it, and parse the source from inside of the program.
But that's a crazy amount of work. What the heck are you using this for?
Upvotes: 2