Tips & Tricks

Type Renderers

While developing any application, you most probably debug it, and I bet my bottom dollar, making this process a little bit easier and faster would not be out of place.
In this post, I’d like to tell you about one of the IntelliJ IDEA features — type renderers. They provide you the ability to customize how objects are displayed in the debugger, offering “logic-oriented” presentation of data vs. structure-oriented as it is by default, and thus improving the readability of the Debug tool window. Let’s see how it works.

To define your own type renderers, open Settings (Ctrl+Alt+S) dialog, click Debugger and select Type Renderers tab. This tab lists all the renderers you’ve defined and allows you to turn them on and off at any time using the check box next to their name. The order of the list is important. It determines which renderer is used, if more than one renderer is applicable to an object instance.

Now let’s add a new renderer. To do so click the plus button, then specify renderer name, the object class to which it applies, and which expressions to use while rendering.

Defining Type Renderer

In our example, we’ve created an expression that displays the user’s full name by accessing the first and last name properties of the object.

Also we’ve defined the appearance of the expanded node. Normally, expanding a node in the debugger lists the object’s member variables. Nevertheless, you can override that behavior. Here we’ve selected to reflect only three of the object’s properties and have used descriptive names rather than the member variable’s name.

Note: There is also a possibility to use a single expression to calculate information to be presented for the node. When expanding the node, the specified expression is evaluated and applicable renderer is picked for the resulting value. The chosen renderer is then applied to get the nodes that will be attached as children to the node that was originally rendered. We can also specify a Boolean expression to test if the node can be expanded. If it is true the renderer displays the expandable nodes for the defined objects. Otherwise, no nodes are displayed.

These expressions are helpful when you want to limit the amount of information displayed or to be more precise in how the information is presented, for example.

Rendered View

When defining expressions, feel free to use IntelliJ IDEA’s code-completion features to help you. All method calls and member variable access is relative to the object you’re rendering. Use this to refer to the instance, to which the renderer is applied.

Note: Using “heavy” expressions in renderers may slow down data rendering in views. Much as method calls should be used with caution because of possible side-effects.

One more trick: you can easily switch between applicable renderers for particular object in already built data tree with the View As option in the context menu of the Variables or Watches tab of the Debugger, even if your custom type renderer is disabled.

P.S. Type renderers are available since IntelliJ IDEA version 5.0.

Technorati tags: , ,
Comments below can no longer be edited.

7 Responses to Type Renderers

  1. Avatar

    Honza Novotný says:

    April 21, 2008

    Nice, I didn’t know about it, though I am using version 6 of the Idea. Thanks for the tip.

  2. Avatar

    Ruslan says:

    June 27, 2008

    Is it possible to create Type Render for collection ? For instance I’d like to have TypeRender for org.w3c.dom.NodeList.

    I’m using IDEA 8.


  3. Avatar

    Viktor says:

    June 18, 2009

    Is is possible to turn off fully qualified type names (or type names in general) in the debugger view? We have a deep package hierarchy and I hate that I always have to scroll right to see the rendered value…

  4. Avatar

    Viktor says:

    June 18, 2009

    Idea 8.1.3 – I forgot to mention…

  5. Avatar

    teo says:

    January 12, 2012

    @Victor: Did you ever find a solution to the fully-qualified-name problem? It would make life so much better. Hate the right scrolling, and I’m always doing it!

  6. Avatar

    Sergiu Popa says:

    February 21, 2013

    I´m trying to output an array on integers like this: i1 + ” ”

    What should I write in the expression field?

    I tried with this and its ouput is like {i1, i2… in} But how can I access every element of the array?


  7. Avatar

    Victor says:

    November 5, 2013

    Images are down 🙁
    Atleast write the expression as text.

Discover more