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";
:
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";
}
}
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 .
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.
The solution was simply to uncheck "Enable 'toString()' object view" -> Apply -> re-enable the same option -> apply and they're back!
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.
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.
Objects
in the debug customized data views. [*]
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.
Publicly available on my GitLab - IntelliJ-CustomDebugDataView
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With