[stringtemplate-interest] Primitives and Model Adapters

Collin Fagan collin.fagan at gmail.com
Fri Jan 28 05:59:05 PST 2011


I understand, no problem at all. In this case though can we do one of two
things?

1. Throw an IllegalArgumentException when one tries to register a model
adapter on a primitive.
2. Translate any primitive into it's boxed type during registration. ST
already seems to treat return values of primitive types like boxed types
when the template is run.

Thanks,

Collin

On Thu, Jan 27, 2011 at 6:37 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Actually, can't make it work. register with Integer.class :)  everything is
> object inside interp.
>
> Ter
> 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. One added complexity is that you cannot
> pass a primitive to add because it gets auto-boxed to it's object type.
> >
> > 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
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110128/ede2eb3d/attachment.html 


More information about the stringtemplate-interest mailing list