javadebuggingintellij-ideaintellij-plugin

Save state of object in IntelliJ debug?


Does anyone know whether it is possible to save the state of an object while debugging?

I would like to compare the state of an object in two different execution cycles.

Bonus Question: Anyone with experience in writing IntelliJ plugins? Does the IntelliJ SDK allow to access debug values in debug mode? Read them out of IntelliJ cache and write them to disk?


Solution

  • TL;DR

    As a very simple solution you can use the Fully Expand Tree Node action for objects in Variables or Watches views. This action is bound to Numpad * by default and opens the whole object tree. Then you select all the elements of opened object tree with shift and copy them to clipboard. For a full-fledged solution, use a JSON library like Gson().toJson(yourObject)

    Notes and Updates

    All these are workarounds - IntelliJ has had a ticket open for 5+ years now (thanks @Line for this link https://youtrack.jetbrains.com/issue/IJPL-56173/Debugger-Expand-all-items-of-Variables-Watches-tab-at-once ) But who needs debugging improvements when you can chat with your co-worker or GPT directly from IntelliJ? Priorities! 😉

    1. Numpad * Key Must Be Used The Numpad * key triggers the Fully Expand Tree Node action. Using the regular * key (e.g., on the main keyboard) opens the context search instead. You can find and reconfigure this action in IntelliJ's settings under Keymap → Debugger Actions → Fully Expand Tree Node. See the screenshot below for reference: Settings example

    2. Keyboard Shortcut to Select All Nodes After expanding the tree, use Shift + Ctrl/Cmd + End to select the entire node and beyond. Previously, Ctrl/Cmd-A worked for selecting all items, but this doesn’t always work now, especially in the Variables/Watches views.

    3. Using the Context Button "View Text" You can right-click the object and select View Text to see its full context. Note: This might be slow for large objects, so use with caution.

    4. Using a Scratch File with Syntax Highlighting Once you’ve copied the tree to the clipboard: Go to File > New > Scratch File and choose the Properties format. Paste the content. This provides syntax highlighting and allows you to use standard IntelliJ tools like Compare with Clipboard to track changes over time. Limitations of This Approach:

    While this solution works for quick inspection, it doesn’t drill down into deep structures. For a complete, structured export, use a library like Gson to serialize your object to JSON.

    Using Gson for a Complete Solution

    Here’s how to serialize an object to JSON in both Kotlin and Java:

    Kotlin Example:

    build.gradle.kts

    implementation("com.google.code.gson:gson:2.10.1")
    

    file.kt

    import com.google.gson.Gson
    
    val s = Gson().toJson(yourObject) 
    println(s)
    

    Java Example:

    import com.google.gson.Gson;
    
    Gson gson = new Gson();
    String json = gson.toJson(yourObject);
    System.out.println(json);
    

    You can then use IntelliJ’s View Text to inspect the serialized string or paste it into a JSON scratch file for further manipulation.

    View/Export as JSON techniques