You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arthas/tutorials/katacoda/arthas-advanced-cn/ognl.md

50 lines
1.6 KiB
Markdown

在Arthas里有一个单独的`ognl`命令,可以动态执行代码。
### 调用static函数
`ognl '@java.lang.System@out.println("hello ognl")'`{{execute T2}}
可以检查`Terminal 1`里的进程输出,可以发现打印出了`hello ognl`。
### 查找UserController的ClassLoader
`sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash`{{execute T2}}
```bash
$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
classLoaderHash 1be6f5c3
```
### 获取静态类的静态字段
获取`UserController`类里的`logger`字段:
`ognl -c 1be6f5c3 @com.example.demo.arthas.user.UserController@logger`{{execute T2}}
还可以通过`-x`参数控制返回值的展开层数。比如:
`ognl -c 1be6f5c3 -x 2 @com.example.demo.arthas.user.UserController@logger`{{execute T2}}
### 执行多行表达式赋值给临时变量返回一个List
`ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'`{{execute T2}}
```bash
$ ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'
@ArrayList[
@String[/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre],
@String[Java(TM) SE Runtime Environment],
]
```
### 更多
在Arthas里`ognl`表达式是很重要的功能,在很多命令里都可以使用`ognl`表达式。
一些更复杂的用法,可以参考:
* OGNL特殊用法请参考https://github.com/alibaba/arthas/issues/71
* OGNL表达式官方指南https://commons.apache.org/proper/commons-ognl/language-guide.html