sfandler
sfandler

Reputation: 640

IntelliJ Customize Data Views for Debugger: How to have a multiline expression for Java Data Type Renderers?

I want to customize the way the IntelliJ debugger display the value of Set objects. Therefore I created a new Java Data Type Renderers in the Customize Data Views dialog and pasted this code into the Use following expression field:

String s = "";
if (this.size() > 0) {
  Iterator iterator = this.iterator();
  if (iterator.hasNext()) {
    s += iterator.next();
    if (iterator.hasNext()) {
      s += ", ";
      do {
        s += iterator.next();
      } while (iterator.hasNext());
    }
  }
}
return s;

However following error is shown in the debugger: Unable to evaluate the expression Initializer for 's' has incompatible type. How should I write a multiline expression into the field so my above code works properly?

Upvotes: 1

Views: 876

Answers (2)

SD.
SD.

Reputation: 9549

What is wrong with using just:

this.toString()

Upvotes: 1

Will Humphreys
Will Humphreys

Reputation: 2791

If you are using Java 8 you can use String.join(", ", this) It should look something like this. Image showing what to change on the customize data views dialog

If you are using an older version of Java you can use.

  StringBuilder sb = new StringBuilder();
    Iterator<?> iter = this.iterator();
    if (iter.hasNext())
        sb.append(iter.next().toString());
    while (iter.hasNext()) {
        sb.append(", ");
        sb.append(iter.next().toString());
    }
    return sb.toString();

IntelliJ highligted the iterator code in red but it still works. How to the long version should look

Upvotes: 1

Related Questions