Java 反射与 Java 调试接口 (JDI)

Java Reflection vs Java Debug Interface (JDI)

使用 Java 反射和 Java 调试接口有什么区别? 我什么时候可以使用一个,什么时候可以使用另一个?

来自JDI documentation

The JavaTM Debug Interface (JDI) is a high level Java API providing information useful for debuggers and similiar systems needing access to the running state of a (usually remote) virtual machine. The JDI provides introspective access to a running virtual machine's state, Class, Array, Interface, and primitive types, and instances of those types.

The JDI also provides explicit control over a virtual machine's execution. The ability to suspend and resume threads, and to set breakpoints, watchpoints, ... Notification of exceptions, class loading, thread creation... The ability to inspect a suspended thread's state, local variables, stack backtrace...

来自Reflection Documentation

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

我看到有一点重叠,但对我来说,如果您计划创建自己的调试器,或者如果您有一些现有调试器无法解决的疯狂问题,您可以使用 JDI。您在运行时使用反射作为对象 inspection/manipulation 的一种方式来满足一些非正统的应用程序需求,例如创建您自己的 ClassLoaderAnnotation