简体   繁体   English

如何:在Java中查找对字段的所有引用

[英]HowTo: Find all references to a Field in Java

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? Java反射API中是否有任何内容可以让我查找所有引用了特定字段的地方? What I'm looking for is a way to find out that (given the example) class B has a reference to Aa 我正在寻找的是一种发现(给定示例)类B对Aa的引用的方法。

I know I can get all the Fields in a Class via the reflection API. 我知道我可以通过反射API获取类中的所有字段。 But now I want to find all references to that Field. 但是现在我想找到对该字段的所有引用。

Thanks. 谢谢。

From the standard APIs, I think the answer is no. 从标准的API,我认为答案是否定的。 Eclipse can do it, but that's not at runtime. Eclipse可以做到,但是那不是在运行时。

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. 对于一种令人难以置信的骇客方式,请获取ClassLoader对象,获取resourceAsStream以获取内容,使用诸如JODE的方式对其进行反编译,然后从程序内部解析源代码。

But that's a crazy amount of work. 但这是一项疯狂的工作。 What the heck are you using this for? 您到底要干什么?

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. 如果您需要的是编译时而不是运行时,并且您没有IDE,则可以更改字段名称并重新编译,然后搜索无法编译的内容,这是非常古老的技巧。 ;] ;]

Then, of course, get yourself an IDE. 然后,当然,让自己成为一个IDE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM