获取要加载的类的必要类列表 [英] Get list of necessary classes for a class to load
问题描述
我想获取一个类的导入列表。由于编译器将其删除,我认为通过反射可以扫描类,它的方法,字段等,并收集类加载类所需的类列表。是否有某种类型的库,教程或文章可以指向我,以便我能更好地理解如何做到这一点? (我知道有类似的问题已被提出,但我找不到合适的答案,我很确定这应该是可行的)。我看到了一些例子,展示了你如何做到这一点,如果你有资源,但在我的情景中不一定如此。
I'd like to obtain the list of imports a class has. Since this is removed by the compiler, I suppose via reflection one could scan through the class, it's methods, fields and so on and collect a list of classes which are required in order for the classloader to load the class. Is there some sort of library, tutorial, or article you could point me at so I could better understand how this can be done? (I understand similar questions have been asked, but I could not find a proper answer and I'm quite sure this should be possible). I saw some examples showing how you could do it, if you had the sources, but that would not necessarily be the case in my scenario.
非常感谢提前!
推荐答案
不,反思不会帮助
void test() {
Date date = new Date();
}
您无法检测到在反射方法内使用Date。但您可以使用Javassist https://github.com/jboss-javassist/javassist
you cannot detect that Date is used inside method with reflection. But you can use Javassist https://github.com/jboss-javassist/javassist
ClassPool cp = ClassPool.getDefault();
Collection classes = cp.get("test.Test").getRefClasses();
此代码生成test.Test类中引用的所有类的名称的集合
this code produces a collection of the names of all the classes referenced in test.Test class
这篇关于获取要加载的类的必要类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!