Kai
Kai

Reputation: 2205

How to define IntelliJ java breakpoint on first construction of a String

I am debugging into some pretty complex software. Some string is constructed and replaced with a modified form in several different places (the stack is pages long), but I want to find out quickly where the String with a given value is first time constructed. Conditional breakpoints seem to be tricky on this as the condition itself constructs the string I want to detect...

Any ideas? I am using IntelliJ

Upvotes: 3

Views: 289

Answers (2)

Egor
Egor

Reputation: 2664

To avoid creating a new string inside the condition, you can compare the content, not the String objects. For example like this:

Arrays.equals(str.toCharArray(), new char[]{'c', 'o', 'n', 't', 'e', 'n', 't'});

Upvotes: 2

Karol Dowbecki
Karol Dowbecki

Reputation: 44942

This might not be the easiest approach if your are looking for interned String (e.g. compile time constants) or JVM is performing String de-duplication (G1GC feature).

To make it trickier there were at least two changes in Java 9 that will be hard to breakpoint at:

If you know you are constructing your string with a StringBuilder I'd place a breakpoint in StringBuilder.toString() method were the string is hopefully constructed for the first time.

Otherwise good luck debugging through JVM internals e.g. java.lang.invoke.StringConcatFactory.

Upvotes: 0

Related Questions