Package freemarker.ext.beans
Class GenericObjectModel
java.lang.Object
freemarker.ext.beans.BeanModel
freemarker.ext.beans.StringModel
freemarker.ext.beans.GenericObjectModel
- All Implemented Interfaces:
WrapperTemplateModel
,AdapterTemplateModel
,MethodCallAwareTemplateHashModel
,TemplateHashModel
,TemplateHashModelEx
,TemplateModel
,TemplateModelWithAPISupport
,TemplateScalarModel
This is used for wrapping objects that has no special treatment (unlike
Map
-s, Collection
-s,
Number
-s, Boolean
-s, and some more, which have), hence they are just "generic" Java
objects. Users usually just want to call the public Java methods on such objects.
These objects can also be used as string values in templates, and that value is provided by
the Object.toString()
method of the wrapped object.
This extends StringModel
for backward compatibility, as now BeansWrapper
returns instances of
GenericObjectModel
instead of StringModel
-s, but user code may have insteanceof StringModel
,
or casing to StringModel
. StringModel
served the same purpose as this class, but didn't implement
MethodCallAwareTemplateHashModel
.
- Since:
- 2.3.33
-
Nested Class Summary
Nested classes/interfaces inherited from interface freemarker.template.MethodCallAwareTemplateHashModel
MethodCallAwareTemplateHashModel.ShouldNotBeGetAsMethodException
-
Field Summary
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
Fields inherited from interface freemarker.template.TemplateScalarModel
EMPTY_STRING
-
Constructor Summary
ConstructorDescriptionGenericObjectModel(Object object, BeansWrapper wrapper)
Creates a new model that wraps the specified object with BeanModel + scalar functionality. -
Method Summary
Modifier and TypeMethodDescriptionUses Beans introspection to locate a property or method with name matching the key name.getBeforeMethodCall(String key)
Can be overridden to be public, to implementMethodCallAwareTemplateHashModel
.Methods inherited from class freemarker.ext.beans.StringModel
getAsString
Methods inherited from class freemarker.ext.beans.BeanModel
get, getAdaptedObject, getAPI, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keys, keySet, size, toString, unwrap, values, wrap
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface freemarker.template.TemplateHashModel
isEmpty
-
Constructor Details
-
GenericObjectModel
Creates a new model that wraps the specified object with BeanModel + scalar functionality.- Parameters:
object
- the object to wrap into a model.wrapper
- theBeansWrapper
associated with this model. Every model has to have an associatedBeansWrapper
instance. The model gains many attributes from its wrapper, including the caching behavior, method exposure level, method-over-item shadowing policy etc.
-
-
Method Details
-
get
Description copied from class:BeanModel
Uses Beans introspection to locate a property or method with name matching the key name. If a method or property is found, it's wrapped intoTemplateMethodModelEx
(for a method or indexed property), or evaluated on-the-fly and the return value wrapped into appropriate model (for a non-indexed 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 signaturenon-void-return-type get(java.lang.String)
, thennon-void-return-type get(java.lang.Object)
, or alternatively (if the wrapped object is a resource bundle)Object getObject(java.lang.String)
.As of 2.3.33, the default implementation of this method delegates to
BeanModel.get(String, boolean)
. It's better to override that, instead of this method. Otherwise, unwanted behavior can arise if the model class also implementsMethodCallAwareTemplateHashModel
, as that will certainly callBeanModel.get(String, boolean)
internally, and not the overridden version of this method.- Specified by:
get
in interfaceTemplateHashModel
- Overrides:
get
in classBeanModel
- Parameters:
key
- The name by which theTemplateModel
is identified in the template.- Returns:
- The
TemplateModel
referred to by the key, ornull
if not found. - Throws:
TemplateModelException
- if there was no property nor method nor a genericget
method to invoke.
-
getBeforeMethodCall
public TemplateModel getBeforeMethodCall(String key) throws TemplateModelException, MethodCallAwareTemplateHashModel.ShouldNotBeGetAsMethodExceptionDescription copied from class:BeanModel
Can be overridden to be public, to implementMethodCallAwareTemplateHashModel
. We don't implement that inBeanModel
for backward compatibility, but the functionality is present. If you expose this method by implementingMethodCallAwareTemplateHashModel
, then be sure thatBeanModel.get(String)
is not overridden in custom subclasses; if it is, then those subclasses should be modernized to overrideBeanModel.get(String, boolean)
instead.- Specified by:
getBeforeMethodCall
in interfaceMethodCallAwareTemplateHashModel
- Overrides:
getBeforeMethodCall
in classBeanModel
- Parameters:
key
- Same as forTemplateHashModel.get(String)
- Returns:
- Same as for
TemplateHashModel.get(String)
, except it should return aTemplateMethodModel
(aTemplateMethodModelEx
if possible), or in very rare case aMacro
that was created with thefunction
directive. Or,null
in the same case asTemplateHashModel.get(String)
. The method should never return something that's not callable in the template language as a method or function; the return type isTemplateModel
because that's the most specific common super-interface ofTemplateMethodModel
, andMacro
. - Throws:
MethodCallAwareTemplateHashModel.ShouldNotBeGetAsMethodException
- If the value for the given key exists, but shouldn't be coerced to something callable as a method. This will be converted toNonMethodException
by the engine, but in this exception you can optionally give a more specific explanation, and that will be added to the resultingNonMethodException
as a hint to the user.TemplateModelException
-