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 a “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.
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.
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.
Subscribe to Blog updates
Java Best Practices
Good code follows certain rules, and knowing them increases your chances of success. We’d like to share some Java best practices that will help you on your way. We'll cover the must-know tips and tricks, from broad advice on software development to Java- and project-specific know-how. Let's get star…
The Best of IntelliJ IDEA’s HTTP Client
In today's fast-paced tech world, easy and efficient management of web services and APIs is crucial. That's where IntelliJ IDEA's HTTP Client steps in, turning complex HTTP requests into a walk in the park. The HTTP Client is a feature-rich, text-based tool in IntelliJ IDEA Ultimate. It seamlessl…
Profile Java and Kotlin Code with the IntelliJ Profiler
At JetBrains, we believe that profiling Java and Kotlin code should be simple and accessible to all developers, regardless of their level of expertise. That’s why the IntelliJ Profiler puts ease of use and seamless integration at the forefront of its mission. In previous releases, we introduced a r…
History of Gradle Delegation Process in IntelliJ IDEA
Nowadays, a build tool is an essential part of any project, and IntelliJ IDEA integrates with the majority of them. Among the most popular build tools is Gradle. We’d like to give a brief historical overview of its integration with IntelliJ IDEA and the delegation process implemented inside the I…