Java how to print all local variables?

后端 未结 6 2035
长情又很酷
长情又很酷 2021-01-12 04:25

I have a method and want to examine variables inside it without debugging - is it possible in Java?

I do not want to write tons of code like:

Syste         


        
6条回答
  •  甜味超标
    2021-01-12 04:39

    Well, you can write a method with a varargs parameter and just write:

    dump(variable1, variable2, variable3, variable4, ...);
    

    It's not ideal, but it will be enough in some circumstances. There's no way to automatically grab all the local variables from a method and dump them though.

    You might consider some sort of bytecode manipulation (e.g. with BCEL) which could do it... but it would be pretty ugly.

提交回复
热议问题