groovygroovyshellgroovysh

Debugging groovy code in groovysh step by step


I'm new to Java and Groovy, and this is a really simple question on debugging groovy code in groovysh step by step.

$ groovysh
groovy:000> String str = "abcd"
===> abcd
groovy:000> println str
Unknown property: str

In interactives debug shell for all other languages, I was able to define a variable and use it in all the following steps. How to do that as well in groovysh?


Solution

  • As defined in the groovysh documentation page, by default all variables are untyped and thus using def or specific type identifier (like String) does not work. In this case, the proper syntax is just str = "abcd".

    $ groovysh
    Groovy Shell (3.0.6, JVM: 11.0.6)
    Type ':help' or ':h' for help.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    groovy:000> str = "abcd"
    ===> abcd
    groovy:000> str
    ===> abcd
    groovy:000> :S variables
    Variables:
      str = abcd
      _ = abcd
    ===> [str:abcd, _:abcd]
    

    As you can see in the example above, you can list all variables that are registered in the shell session with the :S variables command. (You can list all available commands with :h executed in the shell window.)

    However, there is a way to turn on typed variables. This is called the interpreted mode and it can be activated with := interpretedMode command like in the example below:

    $ groovysh
    Groovy Shell (3.0.6, JVM: 11.0.6)
    Type ':help' or ':h' for help.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    groovy:000> str = "abcd"
    ===> abcd
    groovy:000> str
    ===> abcd
    groovy:000> :S variables
    Variables:
      str = abcd
      _ = abcd
    ===> [str:abcd, _:abcd]
    groovy:000> := interpreterMode
    groovy:000> String str2 = "efgh"
    ===> efgh
    groovy:000> str2
    ===> efgh
    groovy:000> str == str2
    ===> false
    groovy:000> :S variables
    Variables:
      str = abcd
      _ = false
      groovysh_collected_boundvars = [str2:efgh]
      str2 = efgh
    ===> [str:abcd, _:false, groovysh_collected_boundvars:[str2:efgh], str2:efgh]