如何获取groovy模板上使用的变量列表 [英] How to get the list of variables used on a groovy template

查看:370
本文介绍了如何获取groovy模板上使用的变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如考虑模板:



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

 您好$ {firstName} $ {姓氏} 

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



在开始解析字符串之前我自己想知道groovy是否为我准备好了一些东西?解析方案

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



我编译了以下类:

  class Gstr {
$ b $ def a = 10
def c = 20
def b =值为$ {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 [] {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));

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


I would like to get the list of variables needed in a template.

for example consider the template:

Hello ${firstName} ${surname}

I would like to get the list ['firstName', 'surname']

Before starting to parse the string myself I was wondering if groovy has something ready for me?

解决方案

They don't seem to be available at runtime anymore.

I compiled the following class:

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)
  }
}

Upon decompilation with jd-gui, the following comes out:

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));

They seem to be binded by reference in compilation.

这篇关于如何获取groovy模板上使用的变量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆