user2461271
user2461271

Reputation: 1716

How to get the list of variables used on a groovy template

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?

Upvotes: 1

Views: 352

Answers (1)

Will
Will

Reputation: 14549

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.

Upvotes: 1

Related Questions