如何获取groovy模板上使用的变量列表

时间:2013-06-06 20:45:20

标签: templates groovy

我想获取模板中所需的变量列表。

例如考虑模板:

Hello ${firstName} ${surname}

我想获取列表['firstName', 'surname']

在开始解析字符串之前,我想知道groovy是否有适合我的东西?

1 个答案:

答案 0 :(得分:1)

它们似乎不再在运行时可用。

我编译了以下类:

class Gstr {
  static main(args) {
    def a = 10
    def c = 20
    def b = "the value is ${a} with ${c}"
    println( b.class )
    println (b.values)
    println (b.strings)
  }
}

用jd-gui反编译后,出现以下内容:

Object a = Integer.valueOf(10);
Object c = Integer.valueOf(20);
Object b = new GStringImpl(
    new Object[] { a, c }, new String[] { "the value is ", " with ", "" });
arrayOfCallSite[0].callStatic(
    Gstr.class, arrayOfCallSite[1].callGetProperty(b));
arrayOfCallSite[2].callStatic(
    Gstr.class, arrayOfCallSite[3].callGetProperty(b));
arrayOfCallSite[4].callStatic(
    Gstr.class, arrayOfCallSite[5].callGetProperty(b));

它们似乎在编译中被引用绑定。