Data Models

Object Model

The TemplateObjectModel interface is different from all the other TemplateModel interfaces, in that it is not used directly by the FM-Classic engine. Instead, it is used by the freemarker.ext.beans package to support reflection. The signature of its one method is below:

public Object getAsObject() throws TemplateModelException;

This method returns the underlying object model of the implementing class. This is useful when the beans package wants to access properties, methods, and variables directly rather than going through the main TemplateModel interfaces.

Implementation of this interface should be done with caution, since it allows the beans package access to all the public methods and properties, rather than a restricted subset that may be enforced by other TemplateModels.

For some object models, this could be a potential security hazard. In these cases, the getAsObject method should return a delegate object instead of the underlying object. The delegate would pass method calls to the underlying object, possibly performing security checks before forwarding the call.

In the case where a business object directly implements other TemplateModel interfaces, the getAsObject method can just return this.

ObjectModel and method calls

There is one other situation where this interface may be useful: When arguments are passed to a TemplateMethodModel2 method call, the implementation class could cast each parameter to TemplateObjectModel, and call getAsObject for each to retrieve the underlying object. This way the method call can perform its business logic on the underlying objects directly, rather than having to work through the other TemplateModel interfaces.