public class JythonWrapper extends java.lang.Object implements ObjectWrapper
Modifier and Type | Field and Description |
---|---|
static JythonWrapper |
INSTANCE |
BEANS_WRAPPER, DEFAULT_WRAPPER, SIMPLE_WRAPPER
Constructor and Description |
---|
JythonWrapper() |
Modifier and Type | Method and Description |
---|---|
void |
setAttributesShadowItems(boolean attributesShadowItems)
Sets whether attributes shadow items in wrapped objects.
|
void |
setUseCache(boolean useCache)
Sets whether this wrapper caches model instances.
|
org.python.core.PyObject |
unwrap(TemplateModel model)
Coerces a template model into a
PyObject . |
TemplateModel |
wrap(java.lang.Object obj)
Wraps the passed Jython object into a FreeMarker template model.
|
public static final JythonWrapper INSTANCE
public void setUseCache(boolean useCache)
wrap(Object)
multiple times for
the same object will return the same model.public void setAttributesShadowItems(boolean attributesShadowItems)
${object.name}
will first
try to locate a python attribute with the specified name on the object
using PyObject.__findattr__(java.lang.String)
, and only if it
doesn't find the attribute will it call
PyObject.__getitem__(org.python.core.PyObject)
.
When set to false, the lookup order is reversed and items
are looked up before attributes.public TemplateModel wrap(java.lang.Object obj)
Py.java2py(java.lang.Object)
. PyDictionary
and PyStringMap
are wrapped into a hash model, PySequence
descendants are wrapped into a sequence model, PyInteger
, PyLong
, and PyFloat
are wrapped into a number model. All objects
are wrapped into a scalar model (using Object.toString()
and a
boolean model (using PyObject.__nonzero__()
. For internal
general-purpose PyObject
s returned from a call to unwrap(TemplateModel)
, the template model that was passed to
unwrap
is returned.wrap
in interface ObjectWrapper
obj
- The object to wrap into a TemplateModel
. If it already implements TemplateModel
,
it should just return the object as is. If it's null
, the method should return null
(however, BeansWrapper
, has a legacy option for returning a null model object instead, but it's not
a good idea).TemplateModel
wrapper of the object passed in. To support un-wrapping, you may consider the
return value to implement WrapperTemplateModel
and AdapterTemplateModel
.
The default expectation is that the TemplateModel
isn't less thread safe than the wrapped object.
If the ObjectWrapper
returns less thread safe objects, that should be clearly documented, as it
restricts how it can be used, like, then it can't be used to wrap "shared variables"
(Configuration.setSharedVaribles(Map)
).public org.python.core.PyObject unwrap(TemplateModel model) throws TemplateModelException
PyObject
.model
- the model to coerceAdapterTemplateModel
s (i.e. BeanModel
) are marshalled
using the standard Python marshaller Py.java2py(Object)
on
the result of getWrappedObject(PyObject.class)
s. The
native JythonModel instances will just return the underlying PyObject.
scalars
are
marshalled as PyString
.
numbers
are
marshalled using the standard Python marshaller
Py.java2py(Object)
on their underlying Number
PyObject
subclass that'll correctly pass
__finditem__
, __len__
,
__nonzero__
, and __call__
invocations to
appropriate hash, sequence, and method models.TemplateModelException