|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectfreemarker.ext.beans.BeansWrapper
public class BeansWrapper
Utility class that provides generic services to reflection classes.
It handles all polymorphism issues in the wrap(Object)
and unwrap(TemplateModel)
methods.
Field Summary | |
---|---|
static int |
EXPOSE_ALL
At this level of exposure, all methods and properties of the wrapped objects are exposed to the template. |
static int |
EXPOSE_NOTHING
At this level of exposure, no bean properties and methods are exposed. |
static int |
EXPOSE_PROPERTIES_ONLY
At this level of exposure, only property getters are exposed. |
static int |
EXPOSE_SAFE
At this level of exposure, all methods and properties of the wrapped objects are exposed to the template except methods that are deemed not safe. |
Fields inherited from interface freemarker.template.ObjectWrapper |
---|
BEANS_WRAPPER, DEFAULT_WRAPPER, SIMPLE_WRAPPER |
Constructor Summary | |
---|---|
BeansWrapper()
Creates a new instance of BeansWrapper. |
Method Summary | |
---|---|
static void |
coerceBigDecimals(java.lang.reflect.AccessibleObject callable,
java.lang.Object[] args)
Converts any BigDecimal s in the passed array to the type of
the corresponding formal argument of the method. |
protected TemplateModel |
create(java.lang.Object object,
java.lang.Object factory)
|
protected int |
getDefaultDateType()
|
static BeansWrapper |
getDefaultInstance()
Returns the default instance of the wrapper. |
protected TemplateModel |
getInstance(java.lang.Object object,
ModelFactory factory)
|
ObjectWrapper |
getOuterIdentity()
By default returns this. |
TemplateHashModel |
getStaticModels()
Returns a hash model that represents the so-called class static models. |
boolean |
isSimpleMapWrapper()
|
boolean |
isStrict()
|
java.lang.Object |
newInstance(java.lang.Class clazz,
java.util.List arguments)
|
void |
setDefaultDateType(int defaultDateType)
Sets the default date type to use for date models that result from a plain java.util.Date instead of java.sql.Date or java.sql.Time or java.sql.Timestamp. |
void |
setExposureLevel(int exposureLevel)
Sets the method exposure level. |
void |
setMethodsShadowItems(boolean methodsShadowItems)
Sets whether methods shadow items in beans. |
void |
setNullModel(TemplateModel nullModel)
Sets the null model. |
void |
setOuterIdentity(ObjectWrapper outerIdentity)
When wrapping an object, the BeansWrapper commonly needs to wrap "sub-objects", for example each element in a wrapped collection. |
void |
setSimpleMapWrapper(boolean simpleMapWrapper)
By default the BeansWrapper wraps classes implementing java.util.Map using MapModel . |
void |
setStrict(boolean strict)
Specifies if an attempt to read a bean property that doesn't exist in the wrapped object should throw an InvalidPropertyException . |
void |
setUseCache(boolean useCache)
Sets whether this wrapper caches model instances. |
java.lang.Object |
unwrap(TemplateModel model)
Attempts to unwrap a model into underlying object. |
java.lang.Object |
unwrap(TemplateModel model,
java.lang.Class hint)
|
TemplateModel |
wrap(java.lang.Object object)
Wraps the object with a template model that is most specific for the object's class. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int EXPOSE_ALL
public static final int EXPOSE_SAFE
public static final int EXPOSE_PROPERTIES_ONLY
public static final int EXPOSE_NOTHING
setMethodsShadowItems(boolean)
with false value to
speed up map item retrieval.
Constructor Detail |
---|
public BeansWrapper()
EXPOSE_SAFE
method exposure level, and will not cache
model instances.
Method Detail |
---|
public boolean isStrict()
setStrict(boolean)
public void setStrict(boolean strict)
InvalidPropertyException
.
If this property is false (the default) then an attempt to read a missing bean property is the same as reading an existing bean property whose value is null. The template can't tell the difference, and thus always can use ?default('something') and ?exists and similar built-ins to handle the situation.
If this property is true then an attempt to read a bean propertly in
the template (like myBean.aProperty) that doesn't exist in the bean
object (as opposed to just holding null value) will cause
InvalidPropertyException
, which can't be suppressed in the template
(not even with myBean.noSuchProperty?default('something')). This way
?default('something') and ?exists and similar built-ins can be used to
handle existing properties whose value is null, without the risk of
hiding typos in the property names. Typos will always cause error. But mind you, it
goes against the basic approach of FreeMarker, so use this feature only if you really
know what are you doing.
public void setOuterIdentity(ObjectWrapper outerIdentity)
outerIdentity
- the aggregate ObjectWrapperpublic ObjectWrapper getOuterIdentity()
setOuterIdentity(ObjectWrapper)
public void setSimpleMapWrapper(boolean simpleMapWrapper)
MapModel
. Setting this flag will
cause it to use a SimpleMapModel
instead. The biggest
difference is that when using a SimpleMapModel
, the
map will be visible as TemplateHashModelEx
,
and the subvariables will be the content of the map,
without the other methods and properties of the map object.
simpleMapWrapper
- enable simple map wrappingpublic boolean isSimpleMapWrapper()
public void setExposureLevel(int exposureLevel)
EXPOSE_SAFE
.
exposureLevel
- can be any of the EXPOSE_xxx
constants.public void setMethodsShadowItems(boolean methodsShadowItems)
${object.name}
will first try to locate
a bean method or property with the specified name on the object, and
only if it doesn't find it will it try to call
object.get(name)
, the so-called "generic get method" that
is usually used to access items of a container (i.e. elements of a map).
When set to false, the lookup order is reversed and generic get method
is called first, and only if it returns null is method lookup attempted.
public void setDefaultDateType(int defaultDateType)
TemplateDateModel.UNKNOWN
.
defaultDateType
- the new default date type.protected int getDefaultDateType()
public void setUseCache(boolean useCache)
wrap(Object)
multiple times for
the same object will likely return the same model (although there is
no guarantee as the cache items can be cleared anytime).
public void setNullModel(TemplateModel nullModel)
wrap(Object)
method whenever the underlying object
reference is null. It defaults to null reference, which is dealt
with quite strictly on engine level, however you can substitute an
arbitrary (perhaps more lenient) model, such as
TemplateScalarModel.EMPTY_STRING
.
public static final BeansWrapper getDefaultInstance()
ObjectWrapper.BEANS_WRAPPER
and this is the sole instance that is used by the JSP adapter.
You can modify the properties of the default instance (caching,
exposure level, null model) to affect its operation. By default, the
default instance is not caching, uses the EXPOSE_SAFE
exposure level, and uses null reference as the null model.
public TemplateModel wrap(java.lang.Object object) throws TemplateModelException
null model
,NumberModel
for it,DateModel
for it,TemplateBooleanModel.TRUE
or
TemplateBooleanModel.FALSE
ArrayModel
for it
MapModel
for it
CollectionModel
for it
IteratorModel
for it
EnumerationModel
for it
StringModel
for it
BeanModel
for it.
wrap
in interface ObjectWrapper
TemplateModelException
protected TemplateModel getInstance(java.lang.Object object, ModelFactory factory)
protected TemplateModel create(java.lang.Object object, java.lang.Object factory)
public java.lang.Object unwrap(TemplateModel model) throws TemplateModelException
wrap(Object)
method. In addition
it will unwrap arbitrary TemplateNumberModel
instances into
a number, arbitrary TemplateDateModel
instances into a date,
TemplateScalarModel
instances into a String, and
TemplateBooleanModel
instances into a Boolean.
All other objects are returned unchanged.
TemplateModelException
public java.lang.Object unwrap(TemplateModel model, java.lang.Class hint) throws TemplateModelException
TemplateModelException
public TemplateHashModel getStaticModels()
statics["java.lang.
System"]. currentTimeMillis()
to call the System.currentTimeMillis()
method.
public java.lang.Object newInstance(java.lang.Class clazz, java.util.List arguments) throws TemplateModelException
TemplateModelException
public static void coerceBigDecimals(java.lang.reflect.AccessibleObject callable, java.lang.Object[] args)
BigDecimal
s in the passed array to the type of
the corresponding formal argument of the method.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |