12

How change val property (not method variable) in debugger?

Yes, I know that val is final analog

Breakpoint here:

Code with breakpoint

Set value disabled in context menu:

Context menu

Evaluate not work:

Evaluate window

Nick
  • 3,691
  • 18
  • 36
  • Then you should know you can't change a final, assigned value unless you somehow [hack something together with reflection](https://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection). It's final for a reason – Zoe Apr 12 '19 at 14:55

3 Answers3

6

In the debugger select the value use the context menue on right click to select "set Value..." (default shortcut F2). The value of the variable should now be displayed in a text field. Enter the desired value and press enter to change the value. You only need to enter the value not an assignment (e.g. x = "test" is wrong, just enter the value "test"). The changed value is now applied to the field.

In the debugger it IS also possible to change values on immutable val defined values in Kotlin as it is also possible to change the value on final defined variables in Java.

Simulant
  • 19,190
  • 8
  • 63
  • 98
2

You can do it using reflection.
Just open the Evaluate Expression menu, then type:
val field = Main::class.java.getDeclaredField("b")
field.isAccessible = true
field.set(b, true)

Le_Enot
  • 799
  • 6
  • 16
2

I was able to modify val value on the fly using "evaluate and log". Check full answer enter image description here

Islam Salah
  • 953
  • 11
  • 22