0

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.

Mike Cargal
  • 6,610
  • 3
  • 21
  • 27

1 Answers1

0

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");
    }
  }
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • I'm pretty sure this gives me what I was looking for, though I'm thinking that I would make the call to super.getProperty(...) once I had exhausted the properties that I code in my ModelAdaptor, and let the super class decide to throw the exception. That would allow me to override a property that ObjectModelAdaptor might otherwise handle. I'm assuming this would work as well. (I'll give it a try tomorrow). Thanks once again for the quick response. This would be good documentation to have in the documentation for ModelAdaptors. – Mike Cargal Jan 31 '14 at 01:23