活用ognl表达式
轉載:https://github.com/alibaba/arthas/issues/11
前言
Arthas 3.0中使用ognl表達式替換了groovy來實現表達式的求值功能,解決了groovy潛在會出現內存泄露的問題。靈活運用ognl表達式,能夠極大提升問題排查的效率。
ognl官方文檔:https://commons.apache.org/proper/commons-ognl/language-guide.html
一個測試應用
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random;/*** @author zhuyong on 2017/9/13.*/ public class Test {public static final Map m = new HashMap<>();public static final Map n = new HashMap<>();static {m.put("a", "aaa");m.put("b", "bbb");n.put(Type.RUN, "aaa");n.put(Type.STOP, "bbb");}public static void main(String[] args) throws InterruptedException {List<Pojo> list = new ArrayList<>();for (int i = 0; i < 40; i ++) {Pojo pojo = new Pojo();pojo.setName("name " + i);pojo.setAge(i + 2);list.add(pojo);}while (true) {int random = new Random().nextInt(40);String name = list.get(random).getName();list.get(random).setName(null);test(list);list.get(random).setName(name);Thread.sleep(1000l);}}public static void test(List<Pojo> list) {}public static void invoke(String a) {System.out.println(a);}static class Pojo {String name;int age;String hobby;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}} }public enum Type {RUN, STOP; }查看第一個參數
params是參數列表,是一個數組,可以直接通過下標方式訪問
$ watch Test test params[0] -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 26 ms. @ArrayList[@Pojo[Test$Pojo@6e2c634b],@Pojo[Test$Pojo@37a71e93],@Pojo[Test$Pojo@7e6cbb7a],... ]查看數組中的元素
第一個參數是一個List,想要看List中第一個Pojo對象,可以通過下標方式,也可以通過List的get方法訪問。
$ watch Test test params[0][0] -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 14 ms. @Pojo[name=@String[name 0],age=@Integer[2],hobby=null, ]$ watch Test test params[0].get(0) -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 14 ms. @Pojo[name=@String[name 0],age=@Integer[2],hobby=null, ]查看Pojo的屬性
拿到這個Pojo可以,直接訪問Pojo的屬性,如age
$ watch Test test params[0].get(0).age -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 21 ms. @Integer[2]還可以通過下標的方式訪問params[0][0]["age"],這個寫法等效于params[0][0].age:
$ watch Test test params[0][0]["name"] -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 53 ms. watch failed, condition is: null, express is: params[0][0][age], ognl.NoSuchPropertyException: com.taobao.arthas.core.advisor.Advice.age, visit /Users/wangtao/logs/arthas/arthas.log for more details.但這樣會報錯,這時候需要再加一個引號
$ watch Test test 'params[0][0]["age"]' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 25 ms. @Integer[2]集合投影
有時候我們只需要抽取對象數組中的某一個屬性,這種情況可以通過投影來實現,比如要將Pojo對象列表中的name屬性單獨抽出來,可以通過params[0].{name}這個表達式來實現。 ognl會便利params[0]這個List取出每個對象的name屬性,重新組裝成一個新的數組。用法相當于Java stream中的map函數。
$ watch Test test params[0].{name} -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 56 ms. @ArrayList[@String[name 0],@String[name 1],@String[name 2],@String[name 3],null,@String[name 5],@String[name 6],@String[name 7],@String[name 8],@String[name 9], ]集合過濾
有時候還需要針對集合對象按某種條件進行過濾,比如想找出所有age大于5的Pojo的name,可以這樣寫
$ watch Test test "params[0].{? #this.age > 5}.{name}" -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 25 ms. @ArrayList[@String[name 4],@String[name 5],@String[name 6],null,@String[name 8],@String[name 9], ]其中{? #this.age > 5}?相當于stream里面的filter,后面的name相當于stream里面的map
那如果要找到第一個age大于5的Pojo的name,怎么辦呢?可以用^或$來進行第一個或最后一個的匹配,像下面這樣:
$ watch Test test "params[0].{^ #this.age > 5}.{name}" -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 24 ms. @ArrayList[@String[name 4], ] Command hit execution time limit 1, therefore will be aborted. $ watch Test test "params[0].{$ #this.age > 5}.{name}" -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 43 ms. @ArrayList[@String[name 9], ]多行表達式
有些表達式一行之內無法表達,需要多行才能表達,應該怎么寫的?比如,假設我們要把所有Pojo的name拿出來,再往里面新加一個新的元素,在返回新的列表,應該如何寫?可以通過中括號將多個表達式串聯起來,最后一個表達式的返回值代表整個表達式的最終結果。臨時變量可以用#來表示。
$ watch Test test '(#test=params[0].{name}, #test.add("abc"), #test)' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 28 ms. @ArrayList[@String[name 0],@String[name 1],@String[name 2],@String[name 3],@String[name 4],@String[name 5],@String[name 6],@String[name 7],@String[name 8],null,@String[abc], ]調用構造函數
調用構造函數,必須要指定要創建的類的全類名。比如下面的例子中,創建一個新的list,然后添加一個新的元素,然后返回添加后的list。
$ watch Test test '(#test=new java.util.ArrayList(), #test.add("abc"), #test)' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 37 ms. @ArrayList[@String[abc], ]訪問靜態變量
可以通過@class@filed方式訪問,注意需要填寫全類名
$ watch Test test '@Test@m' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 35 ms. @HashMap[@String[a]:@String[aaa],@String[b]:@String[bbb], ]調用靜態方法
可以通過@class@method(args)方式訪問,注意需要填寫全類名
$ watch Test test '@java.lang.System@getProperty("java.version")' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 42 ms. @String[1.8.0_51]靜態方法和非靜態方法結合,例如想要獲取當前方法調用的TCCL,可以像下面這樣寫:
$ watch Test test '@java.lang.Thread@currentThread().getContextClassLoader()' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 84 ms. @AppClassLoader[ucp=@URLClassPath[sun.misc.URLClassPath@4cdbe50f],$assertionsDisabled=@Boolean[true], ]訪問Map中的元素
Test.n是一個HashMap,假設要獲取這個Map的所有key,ongl針對Map接口提供了keys,?values這兩個虛擬屬性,可以像普通屬性一樣訪問。
$ watch Test test '@Test@n.keys' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 57 ms. @KeySet[@Type[RUN],@Type[STOP], ]因為這個Map的Key是一個Enum,假設要把key為RUN這個值的value取出來應該怎么寫呢?可以通過Enum的valueOf方法來創建一個Enum,然后get出來,比如下面一樣
$ watch Test test '@Test@n.get(@Type@valueOf("RUN"))' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 168 ms. @String[aaa]或者是下面這樣,通過迭代器+過濾的方式:
$ watch Test test '@Test@n.entrySet().iterator.{? #this.key.name() == "RUN"}' -n 1 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 72 ms. @ArrayList[@Node[RUN=aaa], ]附錄: ognl內置的ognl的虛擬屬性
- Collection:
- size
- isEmpty
- List:
- iterator
- Map:
- keys
- values
- Set:
- iterator
- Iterator:
- next
- hasNext
- Enumeration:
- next
- hasNext
- nextElement
- hasMoreElements
最后
歡迎在留言區分享你的牛逼用法,互相交流進步~
?
?
總結
- 上一篇: Spring Data JPA 从入门到
- 下一篇: Encapsulate Collecti