Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display custom string in debugger variables

I have a class with an overridden toString() method. It looks like this:

public class Foo {

    private int debugInfo;

    /* some code */

    @Override
    public String toString() {
        return "some-string";
    }
}

In debug tab in Android Studio, in 'Variables', it's represented as text from toString: debugger view

Is it possible to have a custom message for the debugger to display in 'Variables'? I want to see the debugInfo field value instead of the output from toString, without having to click on the 'expand' arrow.

I know an analog exists in C# - it is the [DebuggerDisplay] attribute:

[DebuggerDisplay("info = {debugInfo}")]
public class Foo 
{ 
    private int debugInfo;
}

Is there something similar for Java/Android?

like image 734
Kirill Avatar asked Sep 19 '25 03:09

Kirill


1 Answers

When you debug, change toString() like the following, and then you can change it back to whatever you like afterwards!

public class Foo {

    private int debugInfo;

    /* some code */

    @Override
    public String toString() {
        return debugInfo;
    }
}
like image 105
cjerik Avatar answered Sep 20 '25 18:09

cjerik