freemarker.ext.beans
Class BeansWrapper

java.lang.Object
  |
  +--freemarker.ext.beans.BeansWrapper

public final class BeansWrapper
extends java.lang.Object

Utility class that provides generic services to reflection classes. It handles all polymorphism issues in the wrap(Object) and unwrap(Object) methods.

Version:
$Id: BeansWrapper.java,v 1.6 2003/11/02 07:06:40 run2000 Exp $
Author:
Attila Szegedi, attila@szegedi.org

Field Summary
static boolean WRAP_AS_OBJECT
          Use in the wrap() method to override logic for determining whether the parameter should be wrapped as a TemplateScalarModel or as a TemplateHashModel, and force wrapping into hash model.
static boolean WRAP_AS_SCALAR
          Use in the wrap() method to override logic for determining whether the parameter should be wrapped as a TemplateScalarModel or as a TemplateHashModel, and force wrapping into scalar.
 
Method Summary
static boolean isScalar(java.lang.Class clazz)
          Determines whether the object of this class should be wrapped into a FastScalar (true), or into a descendant of ObjectModelBase (false).
static boolean isScalar(java.lang.Object object)
          Determines whether the object should be wrapped into a SimpleScalar (true), or into a descendant of ObjectModelBase (false).
static java.lang.Object unwrap(java.lang.Object model)
          Attempts to unwrap a model into underlying object.
static TemplateModel wrap(java.lang.Object object)
          Wraps the object with a template model that is most specific for the object's class.
static TemplateModel wrap(java.lang.Object object, boolean asScalar)
          Wraps the object with a template model that is most specific for the object's class, just as wrap(Object) would, however it can force wrapping into scalar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WRAP_AS_SCALAR

public static final boolean WRAP_AS_SCALAR
Use in the wrap() method to override logic for determining whether the parameter should be wrapped as a TemplateScalarModel or as a TemplateHashModel, and force wrapping into scalar.

WRAP_AS_OBJECT

public static final boolean WRAP_AS_OBJECT
Use in the wrap() method to override logic for determining whether the parameter should be wrapped as a TemplateScalarModel or as a TemplateHashModel, and force wrapping into hash model.
Method Detail

isScalar

public static final boolean isScalar(java.lang.Class clazz)
Determines whether the object of this class should be wrapped into a FastScalar (true), or into a descendant of ObjectModelBase (false). All classes representing primitive types, as well as String and Boolean qualify.

isScalar

public static final boolean isScalar(java.lang.Object object)
Determines whether the object should be wrapped into a SimpleScalar (true), or into a descendant of ObjectModelBase (false). Convenience method for isScalar(Class) that deals with object being null. All classes representing primitive types, as well as String and Boolean qualify.

wrap

public static final TemplateModel wrap(java.lang.Object object)
Wraps the object with a template model that is most specific for the object's class. Specifically:

wrap

public static final TemplateModel wrap(java.lang.Object object,
                                       boolean asScalar)
Wraps the object with a template model that is most specific for the object's class, just as wrap(Object) would, however it can force wrapping into scalar. (I.e. MethodModel uses this when it knows that its return type is a primitive type wrapper).
Parameters:
object - the object to wrap
asScalar - If WRAP_AS_SCALAR, the object will be wrapped into a scalar (in case it is neither a Boolean nor a String, its toString() value is wrapped). If WRAP_AS_OBJECT, the object will be wrapped into ObjectModel even if it is a String or Boolean.

unwrap

public static final java.lang.Object unwrap(java.lang.Object model)
                                     throws TemplateModelException
Attempts to unwrap a model into underlying object. It can unwrap TemplateObjectModel instances into their underlying objects, generic TemplateScalarModel into a String, and generic TemplateNumberModel into a Long. All other objects are returned unchanged.