[stringtemplate-interest] Primitives and Model Adapters

Terence Parr parrt at cs.usfca.edu
Thu Jan 27 16:18:36 PST 2011


On Jan 27, 2011, at 3:58 PM, Collin Fagan wrote:

> Hi,
>      Should one be able to register a model adapter for a primitive type? I can't seem to get this to work.

seems like a good thing.

> One added complexity is that you cannot pass a primitive to add because it gets auto-boxed to it's object type.

we'd have to add an overloaded add(String name, int v) type method for each primitive.


ok, let me try it out.

Ter
> Here is some code to reproduce this. 
> 
> Thanks, Collin
> 
>     @Test
>     public void testPrimitiveMA() {
>         STGroup group = new STGroup();
>         group.registerModelAdaptor(boolean.class, testModelAdaptor);
>         group.defineTemplate("main", "example", "<example.boolean.callMA>");
>         ST mainTemplate = group.getInstanceOf("main");
> 
>         mainTemplate.add("example", new Example());
>         assertEquals("Model Adapter Called", mainTemplate.render());
>     }
> 
>     private ModelAdaptor testModelAdaptor = new ModelAdaptor() {
> 
>         @Override
>         public Object getProperty(ST self, Object o, Object property, String propertyName) throws STNoSuchPropertyException {
>             return "Model Adapter Called";
>         }
>     };
> 
>     private class Example {
>         public boolean getBoolean() {
>             return true;
>         }
>     }
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list