liveBook · Manning

Runtime variable modification

As explained in Troubleshooting Java, Second Edition

Runtime data modification is changing program data while execution is paused to examine behavior under a custom state.

Runtime variable modification changes an in-scope variable while the debugger has paused execution. The altered value creates a custom investigation scenario without changing the source code or waiting for the original input. section 3.3

Use the technique when:

  • A scheduled or otherwise long-running process would take too long to reach the relevant code repeatedly. section 3.3
  • The suspected parameter values cannot be reproduced in the available environment, including inaccessible production data. section 3.3
  • You want to test a specific hypothesis before deciding how to correct the logic. section 3.3

Testing a missing input case

Pause at the relevant line, replace the in-scope variable s with "abcd", and step over the next operation. Because the string contains no digits, extractDigits() returns an empty list, revealing how the logic behaves under a case you could not otherwise provide. section 3.3

Figure 3.13 When using the step over operation, the app uses the new value you set to the s variable. extractDigits() returns an empty list because string "abcd" doesn’t contain digits. Setting values in variables on the fly enables testing different scenarios even if you don’t have the input data you need.

Use the execution trace to connect the replacement value to the observed empty result and verify the suspected branch behavior. section 3.3

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains suitable scenarios, debugger actions, observed results, and complexity risks from changing multiple values.
Examples
Uses a long-running scheduled process, inaccessible production data, and the string "abcd" producing an empty digit list.
What it assumes
Assumes a debugger can pause execution and expose variables in scope; introduces the technique through concrete investigation problems.
What you can do after
Diagnose/debug with it: alter one or two variables, step through execution, and compare behavior against a suspected condition.
How it connects
Ties runtime variable modification to conditional breakpoints, debugger stepping, and dropping a frame.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.