1

How do you read the content of a file from the expression of a conditional JAVA breakpoint in Eclipse?

My breakpoint's condition is true when the value of my string variable contains 'earnings':

myVariable != null && myVariable.contains("earnings")

I want a conditional breakpoint to replace the content of a string when true; however, I have only been able to do so by waiting the JVM to break (at the breakpoint) and then having the following block displayed (Ctrl+Shift+D). The block reads the content of the file into myVariable.

String sCurrentLine, fileName = "modified-earnings.xml";

java.lang.StringBuffer sb = new java.lang.StringBuffer();
java.io.FileReader fr = new java.io.FileReader(fileName);

java.io.BufferedReader br = new java.io.BufferedReader(fr);
while ((sCurrentLine = br.readLine()) != null) {
    sb.append(sCurrentLine + System.getProperty("line.separator"));
}
fr.close();
// this is where my variable gets the new value from a file
myVariable = sb.toString();
br.close();

As you see, having to evaluate the lines every time that I want to replace the value of the string is a little bit of a hassle, so I want the debugger to do it for me, using the very same conditional nature of my breakpoint. Note the additional (last) boolean expression (myVariable = magicCode("modified-earnings.xml")) != null

myVariable != null && myVariable.contains("earnings") && (myVariable = magicCode("modified-earnings.xml")) != null

What I'm missing is the magicCode function that can not code, since I can not (repeat can not) change the code or add new JARS to the classpath. Ideally, a System API would do it:

myVariable = StreamToString.do(System.getResourceAsStream("modified-earnings.xml"))

1 Answers1

0

For Java 8,
this eclipse breakpoint conditional code checks for content hello in file c:/temp/hello.txt.

"hello".equals(new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get("c:/temp/hello.txt"))))
Robert
  • 19,800
  • 5
  • 55
  • 85
Frank M.
  • 997
  • 1
  • 10
  • 19