public class BeanModel extends java.lang.Object implements TemplateHashModelEx, AdapterTemplateModel, WrapperTemplateModel
TemplateHashModel
interface allowing calls to arbitrary property getters and invocation of
accessible methods on the object from a template using the
object.foo to access properties and object.bar(arg1, arg2) to
invoke methods on it. You can also use the object.foo[index] syntax to
access indexed properties. It uses Beans Introspector
to dynamically discover the properties and methods.Modifier and Type | Field and Description |
---|---|
protected java.lang.Object |
object |
protected BeansWrapper |
wrapper |
NOTHING
Constructor and Description |
---|
BeanModel(java.lang.Object object,
BeansWrapper wrapper)
Creates a new model that wraps the specified object.
|
Modifier and Type | Method and Description |
---|---|
TemplateModel |
get(java.lang.String key)
Uses Beans introspection to locate a property or method with name
matching the key name.
|
java.lang.Object |
getAdaptedObject(java.lang.Class hint)
Retrieves the underlying object, or some other object semantically
equivalent to its value narrowed by the class hint.
|
java.lang.Object |
getWrappedObject()
Retrieves the original object wrapped by this model.
|
protected boolean |
hasPlainGetMethod()
Whether the model has a plain get(String) or get(Object) method
|
protected TemplateModel |
invokeGenericGet(java.util.Map keyMap,
java.lang.Class clazz,
java.lang.String key) |
boolean |
isEmpty()
Tells whether the model is empty.
|
TemplateCollectionModel |
keys() |
protected java.util.Set |
keySet()
Helper method to support TemplateHashModelEx.
|
int |
size() |
java.lang.String |
toString() |
protected java.lang.Object |
unwrap(TemplateModel model) |
TemplateCollectionModel |
values() |
protected TemplateModel |
wrap(java.lang.Object obj) |
protected final java.lang.Object object
protected final BeansWrapper wrapper
public BeanModel(java.lang.Object object, BeansWrapper wrapper)
BeansWrapper.wrap(Object)
.object
- the object to wrap into a model.wrapper
- the BeansWrapper
associated with this model.
Every model has to have an associated BeansWrapper
instance. The
model gains many attributes from its wrapper, including the caching
behavior, method exposure level, method-over-item shadowing policy etc.public TemplateModel get(java.lang.String key) throws TemplateModelException
TemplateMethodModelEx
(for a method or
indexed property), or evaluated on-the-fly and the return value wrapped
into appropriate model (for a simple property) 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.
(Side-note: 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. The
framework does, however detect class reloads (if you happen to be in an
environment that does this kind of things--servlet containers do it when
they reload a web application) and flushes the cache. If no method or
property matching the key is found, the framework will try to invoke
methods with signature
non-void-return-type get(java.lang.String),
then non-void-return-type get(java.lang.Object), or
alternatively (if the wrapped object is a resource bundle)
Object getObject(java.lang.String).get
in interface TemplateHashModel
key
- the name by which the TemplateModel
is identified in the template.TemplateModelException
- if there was no property nor method nor
a generic get method to invoke.protected boolean hasPlainGetMethod()
protected TemplateModel invokeGenericGet(java.util.Map keyMap, java.lang.Class clazz, java.lang.String key) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, TemplateModelException
java.lang.IllegalAccessException
java.lang.reflect.InvocationTargetException
TemplateModelException
protected TemplateModel wrap(java.lang.Object obj) throws TemplateModelException
TemplateModelException
protected java.lang.Object unwrap(TemplateModel model) throws TemplateModelException
TemplateModelException
public boolean isEmpty()
isEmpty
in interface TemplateHashModel
public java.lang.Object getAdaptedObject(java.lang.Class hint)
AdapterTemplateModel
getAdaptedObject
in interface AdapterTemplateModel
hint
- the desired class of the returned value. An implementation
should make reasonable effort to retrieve an object of the requested
class, but if that is impossible, it must at least return the underlying
object as-is. As a minimal requirement, an implementation must always
return the exact underlying object when
hint.isInstance(underlyingObject) == true holds. When called
with java.lang.Object.class, it should return a generic Java
object (i.e. if the model is wrapping a scripting language object that is
further wrapping a Java object, the deepest underlying Java object should
be returned).public java.lang.Object getWrappedObject()
WrapperTemplateModel
getWrappedObject
in interface WrapperTemplateModel
public int size()
size
in interface TemplateHashModelEx
public TemplateCollectionModel keys()
keys
in interface TemplateHashModelEx
TemplateScalarModel
(as the keys of hashes are always strings).public TemplateCollectionModel values() throws TemplateModelException
values
in interface TemplateHashModelEx
TemplateModel
-s.TemplateModelException
public java.lang.String toString()
toString
in class java.lang.Object
protected java.util.Set keySet()