Arthas OGNL表达式的使用

596 阅读2分钟

写一篇文章来总结OGNL表达式,也是在最近工作遇到的情况下才来使用的,采用了网上的一些例子来总结demo,希望大家不要介意

我把我的arthas demo项目test-arthas-springboot上传gitee,大家可以下载运行,用命令尝试

  1. OGNL demo
启动测试类com.roy.OGNLTest#testOGNL

watch com.roy.controller.OGNLTest test '{params,returnObj,throwExp}'  -n 5  -x 3 
观察到指定方法的调用情况,
-n 指定方法的调用次数
-x 对象的遍历深度


watch com.roy.controller.OGNLTest test '{params[0],returnObj,throwExp}'  -n 5  -x 3 
只查看第一个参数

watch com.roy.controller.OGNLTest test '{params[0][0],returnObj,throwExp}'  -n 1  -x 3
查看第一个参数list,查看list的第一个元素


watch com.roy.controller.OGNLTest test '{params[0].get(0).age,returnObj,throwExp}'  -n 1  -x 3
查看第一个参数list,查看list的第一个元素,第一个元素对象的age属性

上面的demo也可以这么写
watch com.roy.controller.OGNLTest test '{params[0][0]["age"],returnObj,throwExp}'  -n 1  -x 3

watch com.roy.controller.OGNLTest test '{params[0].{name},returnObj,throwExp}'  -n 1  -x 3
取第一个元素list,显示list中的所有name元素

watch com.roy.controller.OGNLTest test '{params[0].{? #this.age > 5 }.{name},returnObj,throwExp}'  -n 1  -x 3
取第一个元素list,显示list中的所有name元素,但是要过滤age > 5, 显示所有的


watch com.roy.controller.OGNLTest test '{params[0].{^ #this.age > 5 }.{name},returnObj,throwExp}'  -n 1  -x 3
显示第一个

watch com.roy.controller.OGNLTest test '{params[0].{$ #this.age > 5 }.{name},returnObj,throwExp}'  -n 1  -x 3
显示最后一个

watch com.roy.controller.OGNLTest test '(#value = params[0].{name}, #value.add("dingyawu"), #value)'  -n 1  -x 3
多行表达式的使用方式


watch com.roy.controller.OGNLTest test '(#value = new java.util.ArrayList(), #value.add("dingyawu"), #value)'  -n 1  -x 3
调用构造函数

watch com.roy.controller.OGNLTest test '@com.roy.controller.OGNLTest@m'  -n 1  -x 3
调用静态变量,可以通过`@class@filed`方式访问,注意需要填写全类名


watch com.roy.controller.OGNLTest test '@com.roy.controller.OGNLTest@invoke("dingyawu")'  -n 1  -x 3
借助watch来执行一个表达式,这个表达式是一个static的静态方法,正好得到执行


watch com.roy.controller.OGNLTest test '@com.roy.controller.OGNLTest@n.keys'  -n 1
访问Map中的keys元素

watch com.roy.controller.OGNLTest test '@com.roy.controller.OGNLTest@n.entrySet().iterator.{ ? #this.key.name() == "RUN"}'  -n 1
通过迭代器+过滤的方式