freemarker.ext.beans2
Class BeanWrapper<T>

java.lang.Object
  extended by freemarker.ext.beans2.BeanWrapper<T>
All Implemented Interfaces:
ObjectWrapper, TemplateHashModel, TemplateModel, TemplateObjectModel, TemplateScalarModel, TemplateWriteableHashModel, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
EnumerationWrapper, EnumWrapper, IterableWrapper, IteratorWrapper, MapWrapper, PropertiesWrapper

public class BeanWrapper<T>
extends java.lang.Object
implements TemplateWriteableHashModel, ObjectWrapper, TemplateObjectModel, TemplateScalarModel, java.io.Serializable

Wrapper for a JavaBean or a Plain Old Java Object. Provides access to fields, properties and methods of the object. Also a base class for other Wrapper objects.

Since:
1.9
Version:
$Id: BeanWrapper.java 1067 2011-06-06 10:55:28Z run2000 $
Author:
Nicholas Cull
See Also:
Serialized Form

Field Summary
protected static java.lang.Object[] emptyParams
          For the benefit of property read methods, pre-construct an array of zero argument to pass to the method
protected  T object
          The object being wrapped by this wrapper
 
Constructor Summary
BeanWrapper()
          Default constructor where an object is not provided initially.
BeanWrapper(T object)
          Constructor that takes a POJO or JavaBean object to be wrapped.
 
Method Summary
 java.lang.Object clone()
          Creates and returns a copy of this object.
 TemplateModel get(java.lang.String key)
          Gets a TemplateModel from the hash.
 java.lang.Object getAsObject()
          Return the underlying object to the reflection mechanism.
 java.lang.String getAsString()
          Returns the bean's value as a String using the object's toString method.
 boolean isEmpty()
          Is the object empty?
 void put(java.lang.String key, TemplateModel model)
          Sets a value in the hash model.
 void setObject(java.lang.Object object)
          Sets the object to be wrapped by this object wrapper.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

object

protected T object
The object being wrapped by this wrapper


emptyParams

protected static final java.lang.Object[] emptyParams
For the benefit of property read methods, pre-construct an array of zero argument to pass to the method

Constructor Detail

BeanWrapper

public BeanWrapper()
Default constructor where an object is not provided initially. This allows the wrapper to be used as a prototype.


BeanWrapper

public BeanWrapper(T object)
Constructor that takes a POJO or JavaBean object to be wrapped.

Parameters:
object - the object to be wrapped
Method Detail

setObject

public void setObject(java.lang.Object object)
Description copied from interface: ObjectWrapper
Sets the object to be wrapped by this object wrapper.

Specified by:
setObject in interface ObjectWrapper
Parameters:
object - the object to be wrapped

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Description copied from interface: TemplateModel
Is the object empty?

Specified by:
isEmpty in interface TemplateModel
Returns:
true if this object is empty, otherwise false
Throws:
TemplateModelException

get

public TemplateModel get(java.lang.String key)
                  throws TemplateModelException
Description copied from interface: TemplateHashModel
Gets a TemplateModel from the hash.

Specified by:
get in interface TemplateHashModel
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 - there was a problem getting the value for the given key

put

public void put(java.lang.String key,
                TemplateModel model)
         throws TemplateModelException
Sets a value in the hash model.

Specified by:
put in interface TemplateWriteableHashModel
Parameters:
key - the hash key
model - the value to be added to the hash model
Throws:
TemplateModelException - there was a problem setting the value for the given key

getAsObject

public java.lang.Object getAsObject()
                             throws TemplateModelException
Return the underlying object to the reflection mechanism. Any variables, methods or properties can be called directly via reflection.

Specified by:
getAsObject in interface TemplateObjectModel
Returns:
the underlying object for this template model
Throws:
TemplateModelException - the object could not be returned

getAsString

public java.lang.String getAsString()
                             throws TemplateModelException
Returns the bean's value as a String using the object's toString method.

Specified by:
getAsString in interface TemplateScalarModel
Returns:
the String value of this object.
Throws:
TemplateModelException

clone

public java.lang.Object clone()
Creates and returns a copy of this object.

Specified by:
clone in interface ObjectWrapper
Overrides:
clone in class java.lang.Object
Returns:
a clone of this instance.
Throws:
java.lang.RuntimeException - the clone operation was not successful
java.lang.OutOfMemoryError - if there is not enough memory.
See Also:
Cloneable