public class BSHInterpreter extends GenericInterpreter implements SerializableAware, HierarchicalAware
Unlike many other implementations, it supports the hierarchical
scopes (HierarchicalAware
).
That is, it uses an independent BeanShell NameSpace
(a.k.a. interpreter's scope) to store the variables/classes/methods
defined in BeanShell script for each ZK scope (Scope
).
Since one-to-one relationship between BeanShell's scope and ZK scope,
the invocation of BeanShell methods can execute correctly without knowing
what scope it is.
However, if you want your codes portable across different interpreters,
you had better to call
Scopes.beforeInterpret(org.zkoss.zk.ui.ext.Scope)
to prepare the proper scope, before calling any method defined in
zscript.
First, all NameSpace objects have to serialize. Second,
the top-level namespace (GlobalNS) is wrapped with NSWrap, which
is not serializable. It is serialized when SerializableAware.write(java.io.ObjectOutputStream, org.zkoss.zk.scripting.SerializableAware.Filter)
is called (triggered by PageImpl's write).
On the other hand, all non-top-level namespaces (NS) are wrapped with NSWrapSR which is serializable, so they are serialized with the attributes of a ID space owner being serialized.
SerializableAware.Filter
UNDEFINED
Constructor and Description |
---|
BSHInterpreter() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
contains(Scope scope,
java.lang.String name)
Tests whether a variable is defined in the interpreter's scope
associated with the specified scope.
|
protected boolean |
contains(java.lang.String name)
Tests whether a variable is defined in this interpreter.
|
void |
destroy()
Reset the owner (
GenericInterpreter.getOwner() ) to null. |
protected void |
exec(java.lang.String script)
Executes the specified script.
|
protected java.lang.Object |
get(Scope scope,
java.lang.String name)
Gets the variable from the interpreter's scope associated with
the giving scope.
|
protected java.lang.Object |
get(java.lang.String name)
Gets the variable from the interpreter.
|
java.lang.Class<?> |
getClass(java.lang.String clsnm)
Returns null since retrieving class is not supported.
|
Function |
getFunction(Scope scope,
java.lang.String name,
java.lang.Class[] argTypes)
Returns null since retrieving methods is not supported.
|
Function |
getFunction(java.lang.String name,
java.lang.Class[] argTypes)
Returns null since retrieving methods is not supported.
|
java.lang.Object |
getNativeInterpreter()
Returns the native interpreter, or null if it is not initialized
or destroyed.
|
void |
init(Page owner,
java.lang.String zslang)
Initializes the interpreter.
|
protected void |
loadDefaultImports(bsh.NameSpace bshns)
Called when the top-level BeanShell scope is created.
|
void |
read(java.io.ObjectInputStream s)
Reads the name and value of the variable from the specified input
stream.
|
protected void |
set(Scope scope,
java.lang.String name,
java.lang.Object val)
Sets the variable to the interpreter's scope associated with the
giving scope.
|
protected void |
set(java.lang.String name,
java.lang.Object val)
Sets the variable to the interpreter.
|
protected void |
unset(Scope scope,
java.lang.String name)
Removes the variable from the interpreter.
|
protected void |
unset(java.lang.String name)
Removes the variable from the interpreter.
|
void |
write(java.io.ObjectOutputStream s,
SerializableAware.Filter filter)
Writes the name and value of the variables of this namespace
to the specified stream.
|
afterExec, afterInterpret, beforeExec, beforeInterpret, containsVariable, containsVariable, getCurrent, getFromNamespace, getFromNamespace, getImplicit, getLanguage, getOwner, getVariable, getVariable, interpret, setVariable, setVariable, unsetVariable, unsetVariable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
containsVariable, getVariable, setVariable, unsetVariable
protected void loadDefaultImports(bsh.NameSpace bshns)
Note: to speed up the performance, this implementation
disabled NameSpace.loadDefaultImports()
.
It only imports the java.lang and java.util packages.
If you want the built command and import packages, you can override
this method. For example,
protected void loadDefaultImports(NameSpace bshns) {
bshns.importCommands("/bsh/commands");
}
protected void exec(java.lang.String script)
GenericInterpreter
GenericInterpreter.interpret(java.lang.String, org.zkoss.zk.ui.ext.Scope)
.exec
in class GenericInterpreter
protected boolean contains(java.lang.String name)
GenericInterpreter
By default, it tests whether GenericInterpreter.get(String)
returns non-null.
contains
in class GenericInterpreter
protected boolean contains(Scope scope, java.lang.String name)
GenericInterpreter
By default, it tests whether GenericInterpreter.get(Scope, String)
returns non-null.
contains
in class GenericInterpreter
protected java.lang.Object get(java.lang.String name)
GenericInterpreter
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
An empty (and fake) scope is pushed so GenericInterpreter.getFromNamespace(java.lang.String)
always returns null.
get
in class GenericInterpreter
protected java.lang.Object get(Scope scope, java.lang.String name)
GenericInterpreter
This method is implemented only if the interpreter that supports
hierarchical scopes (HierarchicalAware
).
Default: the same as GenericInterpreter.get(String)
.
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
An empty (and fake) scope is pushed so GenericInterpreter.getFromNamespace(java.lang.String)
always returns null.
get
in class GenericInterpreter
protected void set(java.lang.String name, java.lang.Object val)
GenericInterpreter
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
set
in class GenericInterpreter
protected void set(Scope scope, java.lang.String name, java.lang.Object val)
GenericInterpreter
This method is implemented only if the interpreter that supports
hierarchical scopes (HierarchicalAware
).
Default: the same as GenericInterpreter.set(String, Object)
.
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
set
in class GenericInterpreter
protected void unset(java.lang.String name)
GenericInterpreter
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
unset
in class GenericInterpreter
protected void unset(Scope scope, java.lang.String name)
GenericInterpreter
This method is implemented only if the interpreter that supports
hierarchical scopes (HierarchicalAware
).
Default: the same as GenericInterpreter.unset(String)
.
GenericInterpreter.beforeExec()
is called first, before this method is invoked.
unset
in class GenericInterpreter
public void init(Page owner, java.lang.String zslang)
Interpreter
init
in interface Interpreter
init
in class GenericInterpreter
zslang
- the language this interpreter is associated withpublic void destroy()
GenericInterpreter
GenericInterpreter.getOwner()
) to null.destroy
in interface Interpreter
destroy
in class GenericInterpreter
public java.lang.Object getNativeInterpreter()
Interpreter
getNativeInterpreter
in interface Interpreter
public java.lang.Class<?> getClass(java.lang.String clsnm)
GenericInterpreter
getClass
in interface Interpreter
getClass
in class GenericInterpreter
public Function getFunction(java.lang.String name, java.lang.Class[] argTypes)
GenericInterpreter
getFunction
in interface Interpreter
getFunction
in class GenericInterpreter
argTypes
- the list of argument (a.k.a., parameter) types.
If null, Class[0] is assumed.public Function getFunction(Scope scope, java.lang.String name, java.lang.Class[] argTypes)
GenericInterpreter
getFunction
in interface HierarchicalAware
getFunction
in class GenericInterpreter
scope
- the scope used as a reference to identify the
correct scope for searching the method.
Note: this method doesn't look for any variable stored in scope.argTypes
- the list of argument (a.k.a., parameter) types.
If null, Class[0] is assumed.public void write(java.io.ObjectOutputStream s, SerializableAware.Filter filter) throws java.io.IOException
SerializableAware
If the variable's value is not serializable, it won't be written.
To read back, use SerializableAware.read(java.io.ObjectInputStream)
.
write
in interface SerializableAware
java.io.IOException
public void read(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
SerializableAware
read
in interface SerializableAware
java.io.IOException
java.lang.ClassNotFoundException
SerializableAware.write(java.io.ObjectOutputStream, org.zkoss.zk.scripting.SerializableAware.Filter)
Copyright © 2005-2023 Potix Corporation. All Rights Reserved.