Search code examples
stringtemplate-4

Can a stringtemplate ModelAdaptor fall back to default stringtemplate property evaluation?


It appears that, if I supply a ModelAdaptor for a class I supply to stringtemplate, then I have to respond to every property I want accessible in a template. I'd like to be able to be able to implement getProperty for properties that don't follow the normal naming convention, but let the default behavior handle "normal" properties. Is there a class I can subclass to get the normal behavior (perhaps just calling super() when it's not a property I've implemented, or a method I can call to get the default stringtemplate logic)?

That is, I'd like to handle just the exceptional properties in the adaptor.


Solution

  • You can extend the ObjectModelAdaptor class.

    Override the getProperty method to include a try/catch block, and use your custom handling in the catch block for a STNoSuchPropertyException.

    public class MyModelAdaptor extends ObjectModelAdaptor {
      @Override
      public Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName) {
        try {
          return super.getProperty(interp, self, o, property, propertyName);
        } catch (STNoSuchPropertyException ex) {
          throw new STNoSuchPropertyException("TODO: custom handling goes here");
        }
      }
    }