jeudi 27 août 2015

How to get all Groovy variables with the last values using reflection

I would like in my groovy script to dump all variables and display all values.

I would like to do it dynamically because I would like to surround all my huge groovies by a try/catch. In catch part I want to dump all variables state with the stacktrace. The code should be generic to all groovies.

The problem is that this.getBinding().getVariables() doesn't return the correct variable state.

I've made a small script to illustrate the situation:

def test1 = 1;
test1 = 2;

int test2 = 1;
test2 = 2;

test3 = 1;
test3 = 2;

def errLog=new File("c:/temp/groovy_debug.txt");   
errLog.append("--------------------------------------------------------" + "\n");
errLog.append("  Context ["+getBinding().getVariables()+" ] \n");
errLog.append("--------" + "\n") ;

after the execution I get a very strange result

--------------------------------------------------------
  Context [[[creationStackTrace= <not available>], test1:null, errLog:null, test2:null, test3:2] ] 
--------

it means that the declared variables are always reported as null or as first assignment, but for not typed variables it get the last value. I would like to get the last situation for all variables (value=2).

Is it possible to get them?





Aucun commentaire:

Enregistrer un commentaire