| java.lang.Object freemarker.ext.beans.BeansWrapper
All known Subclasses: freemarker.ext.rhino.RhinoWrapper, freemarker.template.DefaultObjectWrapper,
Field Summary | |
final public static int | EXPOSE_ALL At this level of exposure, all methods and properties of the
wrapped objects are exposed to the template. | final public static int | EXPOSE_NOTHING At this level of exposure, no bean properties and methods are exposed.
Only map items, resource bundle items, and objects retrieved through
the generic get method (on objects of classes that have a generic get
method) can be retrieved through the hash interface. | final public static int | EXPOSE_PROPERTIES_ONLY At this level of exposure, only property getters are exposed.
Additionally, property getters that map to unsafe methods are not
exposed (i.e. | final public 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. | final static Object | GENERIC_GET_KEY |
Constructor Summary | |
public | BeansWrapper() Creates a new instance of BeansWrapper. |
Method Summary | |
public static void | coerceBigDecimals(AccessibleObject callable, Object[] args) Converts any
BigDecimal s in the passed array to the type of
the corresponding formal argument of the method. | protected TemplateModel | create(Object object, Object factory) | static Class[] | getArgTypes(Map classMap, AccessibleObject methodOrCtor) | Map | getClassKeyMap(Class clazz) | protected int | getDefaultDateType() | final public static BeansWrapper | getDefaultInstance() Returns the default instance of the wrapper. | public TemplateHashModel | getEnumModels() Returns a hash model that represents the so-called class enum models.
Every class' enum model is itself a hash through which you can access
enum value declared by the specified class, assuming that class is an
enumeration. | int | getExposureLevel() | protected TemplateModel | getInstance(Object object, ModelFactory factory) | public ObjectWrapper | getOuterIdentity() By default returns this. | public TemplateHashModel | getStaticModels() Returns a hash model that represents the so-called class static models.
Every class static model is itself a hash through which you can call
static methods on the specified class. | void | introspectClass(Class clazz) | TemplateModel | invokeMethod(Object object, Method method, Object[] args) Invokes the specified method, wrapping the return value. | public boolean | isExposeFields() | boolean | isMethodsShadowItems() | boolean | isSafeMethod(Method method) | public boolean | isSimpleMapWrapper() | public boolean | isStrict() | int | keyCount(Class clazz) Returns the number of introspected methods/properties that should
be available via the TemplateHashModel interface. | Set | keySet(Class clazz) Returns the Set of names of introspected methods/properties that
should be available via the TemplateHashModel interface. | public Object | newInstance(Class clazz, List arguments) | public synchronized 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. | public void | setExposeFields(boolean exposeFields) | public void | setExposureLevel(int exposureLevel) Sets the method exposure level. | public synchronized void | setMethodsShadowItems(boolean methodsShadowItems) Sets whether methods shadow items in beans. | public void | setNullModel(TemplateModel nullModel) Sets the null model. | public void | setOuterIdentity(ObjectWrapper outerIdentity) When wrapping an object, the BeansWrapper commonly needs to wrap
"sub-objects", for example each element in a wrapped collection.
Normally it wraps these objects using itself. | public void | setSimpleMapWrapper(boolean simpleMapWrapper) By default the BeansWrapper wraps classes implementing
java.util.Map using
MapModel . | public 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 .
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. | public void | setUseCache(boolean useCache) Sets whether this wrapper caches model instances. | public Object | unwrap(TemplateModel model) Attempts to unwrap a model into underlying object. | public Object | unwrap(TemplateModel model, Class hint) | Object[] | unwrapArguments(List arguments, Class[] argTypes) Auxiliary method that unwraps arguments for a method or constructor call.
Parameters: arguments - the argument list of template models Parameters: argTypes - the preferred types of the arguments Object[] the unwrapped arguments. | Object[] | unwrapArguments(List arguments) | public TemplateModel | wrap(Object object) Wraps the object with a template model that is most specific for the object's
class. |
EXPOSE_ALL | final public static int EXPOSE_ALL(Code) | | At this level of exposure, all methods and properties of the
wrapped objects are exposed to the template.
|
EXPOSE_NOTHING | final public static int EXPOSE_NOTHING(Code) | | At this level of exposure, no bean properties and methods are exposed.
Only map items, resource bundle items, and objects retrieved through
the generic get method (on objects of classes that have a generic get
method) can be retrieved through the hash interface. You might want to
call
BeansWrapper.setMethodsShadowItems(boolean) with false value to
speed up map item retrieval.
|
EXPOSE_PROPERTIES_ONLY | final public static int EXPOSE_PROPERTIES_ONLY(Code) | | At this level of exposure, only property getters are exposed.
Additionally, property getters that map to unsafe methods are not
exposed (i.e. Class.classLoader and Thread.contextClassLoader).
|
EXPOSE_SAFE | final public static int EXPOSE_SAFE(Code) | | 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. The not safe methods are java.lang.Object methods wait() and
notify(), java.lang.Class methods getClassLoader() and newInstance(),
java.lang.reflect.Method and java.lang.reflect.Constructor invoke() and
newInstance() methods, all java.lang.reflect.Field set methods, all
java.lang.Thread and java.lang.ThreadGroup methods that can change its
state, as well as the usual suspects in java.lang.System and
java.lang.Runtime.
|
BeansWrapper | public BeansWrapper()(Code) | | Creates a new instance of BeansWrapper. The newly created instance
will use the null reference as its null object, it will use
BeansWrapper.EXPOSE_SAFE method exposure level, and will not cache
model instances.
|
coerceBigDecimals | public static void coerceBigDecimals(AccessibleObject callable, Object[] args)(Code) | | Converts any
BigDecimal s in the passed array to the type of
the corresponding formal argument of the method.
|
getDefaultDateType | protected int getDefaultDateType()(Code) | | |
getDefaultInstance | final public static BeansWrapper getDefaultInstance()(Code) | | Returns the default instance of the wrapper. This instance is used
when you construct various bean models without explicitly specifying
a wrapper. It is also returned by
freemarker.template.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.
|
getEnumModels | public TemplateHashModel getEnumModels()(Code) | | Returns a hash model that represents the so-called class enum models.
Every class' enum model is itself a hash through which you can access
enum value declared by the specified class, assuming that class is an
enumeration. To obtain an enum model for a class, get the element of this
hash with the fully qualified class name. For example, if you place this
hash model inside the root data model under name "enums", you can use
i.e. statics["java.math.RoundingMode"].UP to access the
java.math.RoundingMode.UP value.
a hash model whose keys are fully qualified class names, andthat returns hash models whose elements are the enum models of theclasses. throws: UnsupportedOperationException - if this method is invoked on a pre-1.5 JRE, as Java enums aren't supported there. |
getExposureLevel | int getExposureLevel()(Code) | | |
getStaticModels | public TemplateHashModel getStaticModels()(Code) | | Returns a hash model that represents the so-called class static models.
Every class static model is itself a hash through which you can call
static methods on the specified class. To obtain a static model for a
class, get the element of this hash with the fully qualified class name.
For example, if you place this hash model inside the root data model
under name "statics", you can use i.e. statics["java.lang.
System"]. currentTimeMillis() to call the
java.lang.System.currentTimeMillis method.
a hash model whose keys are fully qualified class names, andthat returns hash models whose elements are the static models of theclasses. |
introspectClass | void introspectClass(Class clazz)(Code) | | |
invokeMethod | TemplateModel invokeMethod(Object object, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException, TemplateModelException(Code) | | Invokes the specified method, wrapping the return value. The specialty
of this method is that if the return value is null, and the return type
of the invoked method is void,
TemplateModel.NOTHING is returned.
Parameters: object - the object to invoke the method on Parameters: method - the method to invoke Parameters: args - the arguments to the method the wrapped return value of the method. throws: InvocationTargetException - if the invoked method threw an exception throws: IllegalAccessException - if the method can't be invoked due to anaccess restriction. throws: TemplateModelException - if the return value couldn't be wrapped(this can happen if the wrapper has an outer identity or is subclassed,and the outer identity or the subclass throws an exception. PlainBeansWrapper never throws TemplateModelException). |
isExposeFields | public boolean isExposeFields()(Code) | | |
isMethodsShadowItems | boolean isMethodsShadowItems()(Code) | | |
isSimpleMapWrapper | public boolean isSimpleMapWrapper()(Code) | | |
setDefaultDateType | public synchronized void setDefaultDateType(int defaultDateType)(Code) | | 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. Default value is
TemplateDateModel.UNKNOWN .
Parameters: defaultDateType - the new default date type. |
setExposeFields | public void setExposeFields(boolean exposeFields)(Code) | | |
setExposureLevel | public void setExposureLevel(int exposureLevel)(Code) | | Sets the method exposure level. By default, set to EXPOSE_SAFE .
Parameters: exposureLevel - can be any of the EXPOSE_xxx constants. |
setMethodsShadowItems | public synchronized void setMethodsShadowItems(boolean methodsShadowItems)(Code) | | Sets whether methods shadow items in beans. When true (this is the
default value), ${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.
|
setOuterIdentity | public void setOuterIdentity(ObjectWrapper outerIdentity)(Code) | | When wrapping an object, the BeansWrapper commonly needs to wrap
"sub-objects", for example each element in a wrapped collection.
Normally it wraps these objects using itself. However, this makes
it difficult to delegate to a BeansWrapper as part of a custom
aggregate ObjectWrapper. This method lets you set the ObjectWrapper
which will be used to wrap the sub-objects.
Parameters: outerIdentity - the aggregate ObjectWrapper |
setSimpleMapWrapper | public void setSimpleMapWrapper(boolean simpleMapWrapper)(Code) | | By default the BeansWrapper wraps classes implementing
java.util.Map using
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.
Parameters: simpleMapWrapper - enable simple map wrapping |
setStrict | public void setStrict(boolean strict)(Code) | | Specifies if an attempt to read a bean property that doesn't exist in the
wrapped object should throw an
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.
|
setUseCache | public void setUseCache(boolean useCache)(Code) | | Sets whether this wrapper caches model instances. Default is false.
When set to true, calling
BeansWrapper.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).
|
unwrapArguments | Object[] unwrapArguments(List arguments, Class[] argTypes) throws TemplateModelException(Code) | | Auxiliary method that unwraps arguments for a method or constructor call.
Parameters: arguments - the argument list of template models Parameters: argTypes - the preferred types of the arguments Object[] the unwrapped arguments. null if the passed list wasnull. throws: TemplateModelException - if unwrapping any argument throws one |
wrap | public TemplateModel wrap(Object object) throws TemplateModelException(Code) | | Wraps the object with a template model that is most specific for the object's
class. Specifically:
- if the object is null, returns the
BeansWrapper.setNullModel(TemplateModel) null model ,
- if the object is a Number returns a
NumberModel for it,
- if the object is a Date returns a
DateModel for it,
- if the object is a Boolean returns
freemarker.template.TemplateBooleanModel.TRUE or
freemarker.template.TemplateBooleanModel.FALSE
- if the object is already a TemplateModel, returns it unchanged,
- if the object is an array, returns a
ArrayModel for it
- if the object is a Map, returns a
MapModel for it
- if the object is a Collection, returns a
CollectionModel for it
- if the object is an Iterator, returns a
IteratorModel for it
- if the object is an Enumeration, returns a
EnumerationModel for it
- if the object is a String, returns a
StringModel for it
- otherwise, returns a generic
BeanModel for it.
|
|
|