GenericObjectModel (FreeMarker 2.3.35 API)
Description: declaration: package: freemarker.ext.beans, class: GenericObjectModel

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`

public class GenericObjectModel extends StringModel implements MethodCallAwareTemplateHashModel
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 class freemarker.ext.beans.BeanModel

  `object, wrapper`

  ### Fields inherited from interface freemarker.template.TemplateModel

  `NOTHING`

  ### Fields inherited from interface freemarker.template.TemplateScalarModel

  `EMPTY_STRING`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GenericObjectModel​(Object object, BeansWrapper wrapper)`
  Creates a new model that wraps the specified object with BeanModel + scalar functionality.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `TemplateModel`
  `get​(String key)`
  Uses Beans introspection to locate a property or method with name matching the key name.
  `TemplateModel`
  `getBeforeMethodCall​(String key)`
  Can be overridden to be public, to implement `MethodCallAwareTemplateHashModel`.

  ### 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

    public GenericObjectModel​(Object object, BeansWrapper wrapper)
    Creates a new model that wraps the specified object with BeanModel + scalar functionality.
    Parameters:
      `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.

- ## Method Details

  - ### get

    public final TemplateModel get​(String key) throws TemplateModelException
    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 into `TemplateMethodModelEx` (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 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)`.

    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 implements `MethodCallAwareTemplateHashModel`, as that will certainly call `BeanModel.get(String, boolean)` internally, and not the overridden version of this method.

    Specified by:
      `get` in interface `TemplateHashModel`
    Overrides:
      `get` in class `BeanModel`
    Parameters:
      `key` - The name by which the `TemplateModel` is identified in the template.
    Returns:
      The `TemplateModel` referred to by the key, or `null` if not found.
    Throws:
      `TemplateModelException` - if there was no property nor method nor a generic `get` method to invoke.

  - ### getBeforeMethodCall

    public TemplateModel getBeforeMethodCall​(String key) throws TemplateModelException, MethodCallAwareTemplateHashModel.ShouldNotBeGetAsMethodException
    Description copied from class: `BeanModel`
    Can be overridden to be public, to implement `MethodCallAwareTemplateHashModel`. We don't implement that in `BeanModel` for backward compatibility, but the functionality is present. If you expose this method by implementing `MethodCallAwareTemplateHashModel`, then be sure that `BeanModel.get(String)` is not overridden in custom subclasses; if it is, then those subclasses should be modernized to override `BeanModel.get(String, boolean)` instead.
    Specified by:
      `getBeforeMethodCall` in interface `MethodCallAwareTemplateHashModel`
    Overrides:
      `getBeforeMethodCall` in class `BeanModel`
    Parameters:
      `key` - Same as for `TemplateHashModel.get(String)`
    Returns:
      Same as for `TemplateHashModel.get(String)`, except it should return a `TemplateMethodModel` (a `TemplateMethodModelEx` if possible), or in very rare case a `Macro` that was created with the `function` directive. Or, `null` in the same case as `TemplateHashModel.get(String)`. The method should never return something that's not callable in the template language as a method or function; the return type is `TemplateModel` because that's the most specific common super-interface of `TemplateMethodModel`, and `Macro`.
    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 to `NonMethodException` by the engine, but in this exception you can optionally give a more specific explanation, and that will be added to the resulting `NonMethodException` as a hint to the user.
      `TemplateModelException`