如何通过像ASM或Javassist这样的jar来获取Java方法的所有引用类?



如下代码所示:

public class Main implements MainInter{
public static void main(String[] args) {
List<String> s = new ArrayList<>();
DClassFather var = new DClassFather();
DClassFather d = new DClass();
}
}

我怎样才能得到方法";Main:Main"指类";java.lang.String"java.util.ArrayList"java.util.List"DClassFather"DClass";

没有内置的方法可以做到这一点,但您可以访问(或在asm树的情况下迭代(每个指令并收集集合中的引用。

基本概念如下(使用asm树(:

Set<String> dependencies = new HashSet<String>();
for (AbstractInsnNode insnNode : methodNode.instructions) {
if (insnNode instanceof MethodInsnNode methodInsn) {
dependencies.add(methodInsn.owner);
//If you want the full references you should also get the types of the methodDescriptor
}
}

最新更新