I have a large (legacy) application that I have to make a fix in, with terrible code structure and so much code smell that I'm choking. I can't really figure out where a string in the gui is populated. Therefore it would be nice if i somehow could have an expression/breakpoint once ANY string (I don't know the name of the variable, or where it is) is equal to "foobar" so I can backtrack from there.
So, what am I looking for? :)
This trick works as long as the string that you want to find isn't hard-coded in any way. If the string was getting passed to the compiler, then this trick may not work.
- Ensure you have installed the source-code alongside the JDK.
- Open
java.lang.String
class file. Your IDE should be automatically show the source-code for this class.
- Put a breakpoint in the
char[]
field variable. The variable name may vary for each JDK. On my machine it is private final char value[];
.
Put a condition on this breakpoint, e.g.:
value!=null && value.length==6 && value[0]=='f' && value[1]=='o' && value[2]=='o' ....
Please note that the performance will be slower due to this breakpoint.
I have tested this on a simple application that read the string from a file, and the breakpoint is being hit properly when the file is being read into a string.
If you don´t know where the String gets populated, but you do know where the variable it is in is, you can place a watchpoint for the variable.
A watchpoint is exactly placed as a breakpoint, but in the line where the variable is defined.
This watchpoint will pause the programm everytime the variable is changed or read.
IF you have an IDE, such as Eclipse, you can set getters and setters for all your variables then, from every one of these setters, call a function called "checkValue" for example, in this "checkValue" make a conditional breakpoint for the specific value you are looking for.
This will require code to be changed, but as a by-product, would create a cleaner code.
Use jmap and cause memory dumps. Inspect every dump for the value you are looking for
Use Introspection and cycle through every variable, looking for the value.