freemarker.ext.beans
Class ObjectModel

java.lang.Object
  |
  +--freemarker.ext.beans.ObjectModelBase
        |
        +--freemarker.ext.beans.ObjectModel
All Implemented Interfaces:
TemplateHashModel, TemplateModel, TemplateObjectModel, TemplateScalarModel
Direct Known Subclasses:
CollectionModel, EnumerationModel, IteratorModel, MapModel

public class ObjectModel
extends ObjectModelBase
implements TemplateHashModel

A class that will wrap an arbitrary object into TemplateHashModel interface. It uses Beans Introspector to dynamically discover the properties and methods. Properties will be wrapped into MethodModel and ScalarModel class instances. The models are cached (meaning requesting a model for same object twice will return the same model instance) unless the system property expose.reflection.nocache is set to true.

Version:
$Id: ObjectModel.java,v 1.6 2003/10/21 13:08:28 run2000 Exp $
Author:
Attila Szegedi, attila@szegedi.org

Fields inherited from class freemarker.ext.beans.ObjectModelBase
TYPE_ARRAY, TYPE_COLLECTION, TYPE_ENUMERATION, TYPE_ITERATOR, TYPE_LIST, TYPE_MAP, TYPE_OBJECT, TYPE_OBJECT_BASE, TYPE_RESOURCE_BUNDLE
 
Constructor Summary
ObjectModel(java.lang.Object object)
          Creates a new model that wraps the specified object.
 
Method Summary
 TemplateModel get(java.lang.String key)
          Uses Beans introspection to locate a property or method with name matching the key name.
static ObjectModel getInstance(java.lang.Object object)
          Returns a model wrapping the specified object.
 int getType()
          Returns the type of this object (which is TYPE_OBJECT)
 
Methods inherited from class freemarker.ext.beans.ObjectModelBase
getAsObject, getAsString, getInstance, isEmpty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface freemarker.template.TemplateModel
isEmpty
 

Constructor Detail

ObjectModel

public ObjectModel(java.lang.Object object)
Creates a new model that wraps the specified object. Note that there are specialized subclasses of this class for wrapping arrays, collections, enumeration, iterators, and maps. Note also that the superclass can be used to wrap String objects if only scalar functionality is needed. You can also choose to delegate the choice over which model class is used for wrapping to BeansWrapper.wrap(Object).
Parameters:
object - the object to wrap into a model.
Method Detail

getInstance

public static final ObjectModel getInstance(java.lang.Object object)
Returns a model wrapping the specified object. If there is already a cached model instance for this object, returns the cached model instance. Models are cached using WeakReference objects. The caching can be turned off by setting the expose.reflection.nocache system property to true. In this case calling this method is equivalent to constructing a new model. Note that there are specialized subclasses of this class for wrapping arrays, collections, enumeration, iterators, and maps. Note also that the superclass can be used to wrap String objects if only scalar functionality is needed. You can also choose to delegate the choice over which model class is used for wrapping to BeansWrapper.wrap(Object).
Parameters:
object - the object to wrap into a model.
Returns:
the model for the object.

getType

public int getType()
Returns the type of this object (which is TYPE_OBJECT)
Overrides:
getType in class ObjectModelBase

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Uses Beans introspection to locate a property or method with name matching the key name. If a method or property is found, it is wrapped into MethodModel or ScalarModel instance and returned. Models for various properties and methods are cached on a per-class basis, so the costly introspection is performed only once per property or method of a class. (Sidenote: this also implies that any class whose method has been called will be strongly referred to by the framework and will not become unloadable until this class has been unloaded first. Normally this is not an issue, but can be in a rare scenario where you create many classes on the fly. Also, as the cache grows with new classes and methods introduced to the framework, it may appear as if it were leaking memory.) If no method or propery matching the key is found, the framework will try to invoke methods with signature get(java.lang.String), then get(java.lang.Object).
Specified by:
get in interface TemplateHashModel
Throws:
TemplateModelException - if there was no property nor method nor a generic get method to invoke.