Search code examples
javadebuggingreflectionjdi

Java Reflection vs Java Debug Interface (JDI)


What are the differences between use Java Reflection and Java Debug Interface? when can I use one and when the other?


Solution

  • From the 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...

    From the 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.

    I see a little bit of overlap, but to me, you use the JDI if you plan on creating your own debugger or possibly if you have some insane problem that you can't solve with any of the existing debuggers. You use reflection as a means of object inspection/manipulation at runtime to satisfy some unorthodox application requirement, such as creating your own ClassLoader or Annotation.