Data Type Renderers in Intellij?

2019-02-04 15:05发布

Recent convert to Intellij. Eclipse has a feature in the debugger, "Primitive Display Options", which has the debugger render primitives in hex and ascii values.

Eclipse Debugger

Since everything has been a breeze in Intellij so far, I figured this would be a no brainer, but after an hour and a number of non-functional data type renders later, I find myself ready to crawl back to eclipse. I made a few renderers that worked, but only for objects, so I can only conclude that renderers seem to not work for primitives. Can someone point me in the right direction here?

Edit: I've seen the option to select hex in the default render, but I am hoping for something more than a solution that requires 2 clicks for every variable.

6条回答
小情绪 Triste *
2楼-- · 2019-02-04 15:36

This is similar to Andy's solution, but does not require you to define the Hex class (which he even does not mention). Only works for Java8 projects (that is, not on Android currently).

You have to provide a custom data type renderer using the following expression for byte[] (see Andy's answer):

IntStream.range(0,Math.min((this.length+3)/4,20)).mapToObj(i->IntStream.range(i*4,Math.min(i*4+4,_this.length)).mapToObj(i1->String.format("%02x",_this[i1])).collect(Collectors.joining())).collect(Collectors.joining(" "))+(this.length>80?"...":"")

It will format the data such that "01020304 05060708 09", i.e. grouped by four and separated by spaces. Only first 80 bytes are displayed, ellipsis is added if there are more.

查看更多
爷的心禁止访问
3楼-- · 2019-02-04 15:42

You can also try DeltaHex Editor plugin for IntelliJ based IDEs which can show window with typical hex + text preview for byte array in debugger.

DeltaHex Editor Plugin Debug Byte Array Image

Plugin is available in Settings/Plugins/Browse repositories/Search for DeltaHex

查看更多
戒情不戒烟
4楼-- · 2019-02-04 15:47

I made a few renderers that worked, but only for objects, so I can only conclude that renderers seem to not work for primitives.

With IDEA 2016.3 it's finally possible to define custom Java Data Type Renderers for primitive types (including arrays).

https://www.jetbrains.com/idea/whatsnew/#v2016-3

查看更多
一夜七次
5楼-- · 2019-02-04 15:53

My solution was to provide a custom data type renderer using the following expression for byte[]: new String(Hex.encode(this));.

This works by default as long as the data type renderer is enabled (maintains state across application restarts, etc.). You can add more elaborate String formatting if you wish (I usually have a utility class which does further formatting and I just invoke it from the DTR expression field).

Data Type Renderer

Debugger view

查看更多
Root(大扎)
6楼-- · 2019-02-04 15:55

There is an option (checkbox) to permanently change all primitives to display as Hex under the options for Data Views. See:

Settings > Build, Execution, Deployment > Debugger > Data Views > Java >

"Show hex value for primitives"

enter image description here

查看更多
Animai°情兽
7楼-- · 2019-02-04 15:59

For integral values (that is, neither floating-point values nor booleans), you can view them as a hexadecimal value by right-clicking on the primitive in the debugger, and selecting "View As" -> "Hex".

enter image description here

By default, the Data Type Renderer feature only works with objects, and the only realistic way to represent a primitive is either as its integral value or in hex, so I think this is a happy compromise.

查看更多
登录 后发表回答