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
NOTHINGFields inherited from interface freemarker.template.TemplateScalarModel
EMPTY_STRING -
Constructor Summary
ConstructorsConstructorDescriptionGenericObjectModel(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
getAsStringMethods inherited from class freemarker.ext.beans.BeanModel
get, getAdaptedObject, getAPI, getWrappedObject, hasPlainGetMethod, invokeGenericGet, isEmpty, keys, keySet, size, toString, unwrap, values, wrapMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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- theBeansWrapperassociated with this model. Every model has to have an associatedBeansWrapperinstance. 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:BeanModelUses 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:
getin interfaceTemplateHashModel- Overrides:
getin classBeanModel- Parameters:
key- The name by which theTemplateModelis identified in the template.- Returns:
- The
TemplateModelreferred to by the key, ornullif not found. - Throws:
TemplateModelException- if there was no property nor method nor a genericgetmethod to invoke.
-
getBeforeMethodCall
public TemplateModel getBeforeMethodCall(String key) throws TemplateModelException, MethodCallAwareTemplateHashModel.ShouldNotBeGetAsMethodExceptionDescription copied from class:BeanModelCan be overridden to be public, to implementMethodCallAwareTemplateHashModel. We don't implement that inBeanModelfor 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:
getBeforeMethodCallin interfaceMethodCallAwareTemplateHashModel- Overrides:
getBeforeMethodCallin classBeanModel- Parameters:
key- Same as forTemplateHashModel.get(String)- Returns:
- Same as for
TemplateHashModel.get(String), except it should return aTemplateMethodModel(aTemplateMethodModelExif possible), or in very rare case aMacrothat was created with thefunctiondirective. Or,nullin 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 isTemplateModelbecause 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 toNonMethodExceptionby the engine, but in this exception you can optionally give a more specific explanation, and that will be added to the resultingNonMethodExceptionas a hint to the user.TemplateModelException
-