2

Is it possible to call a function of Java class through dynamically generated function from StringTemplate ?

for instance, following is a Java class with three functions

public class RegionManager {

    public static List<String> getCenter(){
       return somelist; 
    }   

    public static List<String> getFloor(){
         return somelist;   
    }

    public static List<String> getRoom(){
         return somelist;   
    }   

}

Now, my String template file contains $CS.name$ .. the value of could be "Room", "Floor", "Center".

based on the $CS.name$ value, I want to call function ( could be getRoom(), getFloor(), getCenter() ) . Please note that When I write String template file, I do not know , which function is going to be called.

user-517752
  • 1,188
  • 5
  • 21
  • 54

2 Answers2

3

You can't call static functions. You can only call getters on objects.

Terence Parr
  • 5,912
  • 26
  • 32
2

As it was already mentioned, you can't call static functions in your template. However, there is another interesting mechanism, which can help you out with your requirement.

StringTemplate library has a mechanism of custom Renderers.

You can build a renderer, which will call your static method, based on the input and / or potentially format e.g.

<your_item; format="your_format">

I hope that will help to solve your problem. It helped me a lot in different templates.

Tom
  • 26,212
  • 21
  • 100
  • 111