buriedpot
buriedpot

Reputation: 41

How to get a Java method's all referring classes by jars like ASM or Javassist?

As the code shows below:

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

How can I get that the method "Main: main" refers classes "java.lang.String", "java.util.ArrayList", "java.util.List", "DClassFather", "DClass"

Upvotes: 3

Views: 141

Answers (1)

uglibubla
uglibubla

Reputation: 93

There is no built in way to do this, however you could visit (or iterate over in case of asm-tree) every instruction and collect the references in a Set.

The basic concept would go like this (using asm-tree):

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
    }
}

Upvotes: 3

Related Questions