org.szegedi.expose.model
Class ReflectionObjectModel
java.lang.Object
|
+--org.szegedi.expose.model.ReflectionObjectModelBase
|
+--org.szegedi.expose.model.ReflectionObjectModel
- All Implemented Interfaces:
- freemarker.template.TemplateHashModel, freemarker.template.TemplateModel, freemarker.template.TemplateScalarModel
- Direct Known Subclasses:
- ReflectionCollectionModel, ReflectionEnumerationModel, ReflectionIteratorModel, ReflectionMapModel
- public class ReflectionObjectModel
- extends ReflectionObjectModelBase
- implements freemarker.template.TemplateHashModel
A class that will wrap an arbitrary object into
TemplateHashModel interface. It uses Beans Introspector
to dinamically discover the properties and methods. Properties will be wrapped
into ReflectionMethodModel and ReflectionScalarModel 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:
- 1.0
- Author:
- Attila Szegedi, attila@szegedi.org
|
Constructor Summary |
ReflectionObjectModel(java.lang.Object object)
Creates a new model that wraps the specified object.
|
|
Method Summary |
freemarker.template.TemplateModel |
get(java.lang.String key)
Uses Beans introspection to locate a property or method with name matching the
key name. |
static ReflectionObjectModel |
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 java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface freemarker.template.TemplateModel |
isEmpty |
ReflectionObjectModel
public ReflectionObjectModel(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
ReflectionUtilities.wrap(Object).
- Parameters:
object - the object to wrap into a model.
getInstance
public static final ReflectionObjectModel 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 ReflectionUtilities.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 ReflectionObjectModelBase
get
public freemarker.template.TemplateModel get(java.lang.String key)
throws freemarker.template.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
ReflectionMethodModel or ReflectionScalarModel 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. 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 freemarker.template.TemplateHashModel
- Throws:
freemarker.template.TemplateModelException - if there was no property nor method nor
a generic get method to invoke.