Custom data views

IntelliJ IDEA, and thus Android Studio, has a really handy feature: Custom Data Views.

Sometimes, you need a specific type to be shown in the debugger in a certain way instead of the default one. The default representation for an object that has no toString() is the class name and the instance hashcode. That’s usually not helpful at all.

Even if you do have a toString() for your class, though, you’ll find it’s usually cluttered with fields you don’t need, and possibly the ones you need are buried outside of the visible area. In this case, you don’t want to write a custom toString() every time. That, and the risk of committing a hacky toString() to version control, should tell you it’s not a good idea.

In this case, you can create a custom Java Data Type Renderer for your class, that better suits your current needs, being confident you’re not altering the observed classes while still getting the information you want (hey, uncertainty principle).

For example, while working on the Smart Lock for Passwords demo app, I wanted to keep an eye on the Credentials I was getting back from Play Services when debugging. That class has no toString() implementation, and wasn’t under my control (nor I had the sources). I created a simple data renderer:

I’m not saying you should always do this, but when you need it, it’s super handy.

From that moment onwards, my IDE started showing me this information whenever I had a Credential instance in the scope:

Oh hai there, beautiful!

You can also define a list of expressions to use when expanding the node, like this: