在访问字节码时,有没有办法知道哪些 类 是用 `new` 创建的?

Is there a way to know which classes are created with `new` when visiting bytecode?

我想通过访问它的字节码来计算 class 的所有依赖关系,在 ClassVisitor 中有方法、字段、属性等的访问者,但是当 class 通过调用新构造函数来使用,例如 this.add(new MyComponent("Hi")) 我希望使用 MyComponent,因此我需要在我的结果中列出此依赖项。

顺便说一句,我使用的是 org.objectweb.asm 版本 7.1

我认为您不能直接使用 ClassVisitor 来做到这一点,但您需要 MethodVisitor。您 return 来自 ClassVisitor 的 visitMethod. There you can implement the visitTypeInsn 方法的 MethodVisitor 实例并检查参数 opcode == Opcodes.NEW.

void visitTypeInsn(final int opcode, final String type)