Is there any way to export a specific class constructor into the FreeMarker data model?
ObjectConstructor provides the power to access any accessible constructor:
Java:
myDataModel.put("objectConstructor", new ObjectConstructor());
Template:
<#assign aList = objectConstructor("java.util.ArrayList", 100)>
But I don't want to do that; if I have a class Foo with two constructors Foo(int x) and Foo(String name, int x), I want to somehow export an object into the data model as Foo so I can do this in a template:
<#assign myfoo1 = Foo(1) >
<#assign myfoo2 = Foo("Buffalo Bill", 2) >
I can do this manually with TemplateMethodModelEx but it requires me to implement exec(List arguments) and figure out how to pull the arguments out and push them into my Foo class constructor.
What I would like is a ClassConstructor object that takes a Class<T> argument of the class in question, and implements TemplateMethodModelEx and automatically does the casting, the same way that FreeMarker automatically handles Java object method calls, and I could do this in Java:
myDataModel.put("Foo", new ClassConstructor(Foo.class));
Is there a way to do this?