How to use Data Type Renderers list for byte [] in IntelliJ

So, I have all these bytes [], and their encoding can be one of many different formats, depending on where I am debugging now. I would like to be able to combine a prediction list for an array of bytes. I use the IntelliJ Data Type Renderers view, applying the renderer to the byte type [], and I'm primarily interested in the Expression List field.

So, I saw how you can display a node with a new String(this) expression, however this does not work in the list of expressions below. In the end, I want to do expressions like new String(this, "UTF16") (or do conversions to hex or base64 or w / e), but this does not appear byte [] in the list of expressions - actually, when I try typecast like (byte[])this result says Inconvertible types; cannot cast '_Dummy_.__Array__≤T≥' to 'byte[]' Inconvertible types; cannot cast '_Dummy_.__Array__≤T≥' to 'byte[]' (same behavior for java.lang.Byte []). This is a really strange behavior, that in one place it is a byte [], and in another it is some opaque internal type.

What the job does is by simply displaying the fields, i.e. an expression like this.length works as expected. In addition, an expression like this simply reloads the node, claiming that its type iste [], and its object identifier matches the original identifier.

+7
java intellij-idea intellij-13
source share
1 answer

I answered a similar question here . The message about the inability to use _Dummy_.__Array__<T> to byte[] sounds like an IntelliJ error, where it cannot determine the class name in the call stack. Perhaps adding a DTR for other "forms" byte[] will help. I gave examples of the three DTRs below - byte[] , byte[] and ArrayList .

Test and helper method (written in Groovy, so make sure it is listed in your class path or rewritten in pure Java):

 @Test void testShouldHandleDTR() { // Arrange byte[] primitiveArray = "90abcdef".bytes Byte[] objectArray = "90abcdef".bytes List<Byte> objectList = "90abcdef".bytes final String EXPECTED_STRING = Hex.encodeHexString(primitiveArray) logger.info("Expected hex string: ${EXPECTED_STRING}") // Fully qualified for DTR dialog String primitiveDTR = "org.bouncycastle.util.encoders.Hex.toHexString(this);" String objectArrayDTR = "org.example.ThisTest.encodeObjectArrayToHex(this);" String objectListDTR = "org.example.ThisTest.encodeObjectArrayToHex(this.toArray());" def types = [primitiveArray, objectArray, objectList] def expressions = [(primitiveArray.class.name): primitiveDTR, (objectArray.class.name): objectArrayDTR, (objectList.class.name): objectListDTR] // Act types.each { it -> logger.info("Contents: ${it}") logger.info("Type: ${it.class.name}") String result = Eval.x(it, expressions[it.class.name].replaceAll(/this/, "x")) logger.info("Result: ${result}") // Assert assert result == EXPECTED_STRING } } public static String encodeObjectArrayToHex(Object[] objectArray) { byte[] primitiveArray = objectArray.collect { it as byte } Hex.encodeHexString(primitiveArray) } 

For each DTR you want to define, just copy the exact line above, in Rendering node> Use the following expression . I would recommend placing the utility method in the source class in your classpath, and not in the test, since on each assembly you will have to re-import the test class in the DTR dialog box because target/ is cleared.

Test Successful

DTR for ArrayList in action

0
source share

All Articles