Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ change toString for debug mode (variables view)

If I overload the toString(), the debugger in IntelliJ will show me the Object.toString() result near the relevant object in the variables tab.

If toString is overloaded with: return "Test1: This is toString overload";:

enter image description here

Sometimes, what I want to see in debug isn't the same as the general toString overload. I know it's possible to set another expression for a specific type/class/etc, but only from the settings.

Is there a way to (globally) set an arbitrary function name that will take precedence over toString when such function exists?

For example:
If Object.toDebuggerString() exists use it, otherwise - use Object.toString().

class Test1 {
    @Override
    public String toString() {
        return "Test1";
    }
}

class Test2 {
    @Override
    public String toString() {
        return "Test2";
    }

    public String toDebuggerString() {
        return "Testing debugging mode";
    }
}
like image 895
arieljannai Avatar asked Jun 06 '18 13:06

arieljannai


People also ask

How do I change the value of a variable in debug mode IntelliJ?

Select a variable or a property of a complex object in the Debug window, press F2 or right-click and choose Set Value... from the context menu, and then specify a new value and press Enter .

How do I show Debug Variables in IntelliJ?

You can click on the Run icon in the gutter area and select the Debug option. You can invoke context actions on the class or main method by using Alt+Enter and choose the Debug action. You can also start it from the Run menu, or by pressing Shift F9.

How do I disable toString in IntelliJ?

The solution was simply to uncheck "Enable 'toString()' object view" -> Apply -> re-enable the same option -> apply and they're back!


2 Answers

You can add your own.

interface Debuggable {
    String toDebugString();
}

then in right click a variable in the debugger -> Customize Data Views -> Java Type Renderer -> + -> type: Debuggable

It will do this for any object of this type for any program in future.

like image 158
Peter Lawrey Avatar answered Oct 22 '22 04:10

Peter Lawrey


Based on the other answers, I got an idea to create a static class that will supply the correct debug string for the debugger.

So I created such class, which decrease the number of steps needed to configure those data views, and makes it more flexible and comfortable.

The steps

  1. Set a constant expression for Objects in the debug customized data views. [*]
  2. Throw the package/class I made near your code
  3. Create toDebugString() methods for the relevant data types, and the class will supply it if exists. Otherwise, if toString() is overridden it will use it, and if non exist - it will use the defualt Object.toString().

[*]This step needs to be done only once, since IntelliJ keeps this in the global settings.

Screenshots

enter image description here enter image description here

How to get it, and more examples

Publicly available on my GitLab - IntelliJ-CustomDebugDataView

Advantages

  1. No need to explicitly set a data view for each different data type we're working with.
  2. The data view is the same for all objects and so it's easy to track and control what you want to see.
  3. Minimal change to the code if you want to use it (one method).
like image 45
arieljannai Avatar answered Oct 22 '22 05:10

arieljannai