- java.lang.Object
-
- javax.script.AbstractScriptEngine
-
- All Implemented Interfaces:
ScriptEngine
- Direct Known Subclasses:
NashornScriptEngine
public abstract class AbstractScriptEngine extends Object implements ScriptEngine
Provides a standard implementation for several of the variants of theevalmethod.
eval(Reader)eval(String)eval(String, Bindings)eval(Reader, Bindings)
are implemented using the abstract methods
eval(Reader,ScriptContext)oreval(String, ScriptContext)
with aSimpleScriptContext.
ASimpleScriptContextis used as the defaultScriptContextof theAbstractScriptEngine..- Since:
- 1.6
-
-
Field Summary
Fields Modifier and Type Field Description protected ScriptContextcontextThe defaultScriptContextof thisAbstractScriptEngine.-
Fields declared in interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
-
-
Constructor Summary
Constructors Constructor Description AbstractScriptEngine()Creates a new instance of AbstractScriptEngine using aSimpleScriptContextas its defaultScriptContext.AbstractScriptEngine(Bindings n)Creates a new instance using the specifiedBindingsas theENGINE_SCOPEBindingsin the protectedcontextfield.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objecteval(Reader reader)eval(Reader)calls the abstracteval(Reader, ScriptContext)passing the value of thecontextfield.Objecteval(Reader reader, Bindings bindings)eval(Reader, Bindings)calls the abstracteval(Reader, ScriptContext)method, passing it aScriptContextwhose Reader, Writers and Bindings for scopes other thatENGINE_SCOPEare identical to those members of the protectedcontextfield.Objecteval(String script)Same aseval(Reader)except that the abstracteval(String, ScriptContext)is used.Objecteval(String script, Bindings bindings)Same aseval(Reader, Bindings)except that the abstracteval(String, ScriptContext)is used.Objectget(String key)Gets the value for the specified key in theENGINE_SCOPEof the protectedcontextfield.BindingsgetBindings(int scope)Returns theBindingswith the specified scope value in the protectedcontextfield.ScriptContextgetContext()Returns the value of the protectedcontextfield.protected ScriptContextgetScriptContext(Bindings nn)Returns aSimpleScriptContext.voidput(String key, Object value)Sets the specified value with the specified key in theENGINE_SCOPEBindingsof the protectedcontextfield.voidsetBindings(Bindings bindings, int scope)Sets theBindingswith the corresponding scope value in thecontextfield.voidsetContext(ScriptContext ctxt)Sets the value of the protectedcontextfield to the specifiedScriptContext.-
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods declared in interface javax.script.ScriptEngine
createBindings, eval, eval, getFactory
-
-
-
-
Field Detail
-
context
protected ScriptContext context
The defaultScriptContextof thisAbstractScriptEngine.
-
-
Constructor Detail
-
AbstractScriptEngine
public AbstractScriptEngine()
Creates a new instance of AbstractScriptEngine using aSimpleScriptContextas its defaultScriptContext.
-
AbstractScriptEngine
public AbstractScriptEngine(Bindings n)
Creates a new instance using the specifiedBindingsas theENGINE_SCOPEBindingsin the protectedcontextfield.- Parameters:
n- The specifiedBindings.- Throws:
NullPointerException- if n is null.
-
-
Method Detail
-
setContext
public void setContext(ScriptContext ctxt)
Sets the value of the protectedcontextfield to the specifiedScriptContext.- Specified by:
setContextin interfaceScriptEngine- Parameters:
ctxt- The specifiedScriptContext.- Throws:
NullPointerException- if ctxt is null.
-
getContext
public ScriptContext getContext()
Returns the value of the protectedcontextfield.- Specified by:
getContextin interfaceScriptEngine- Returns:
- The value of the protected
contextfield.
-
getBindings
public Bindings getBindings(int scope)
Returns theBindingswith the specified scope value in the protectedcontextfield.- Specified by:
getBindingsin interfaceScriptEngine- Parameters:
scope- The specified scope- Returns:
- The corresponding
Bindings. - Throws:
IllegalArgumentException- if the value of scope is invalid for the type the protectedcontextfield.
-
setBindings
public void setBindings(Bindings bindings, int scope)
Sets theBindingswith the corresponding scope value in thecontextfield.- Specified by:
setBindingsin interfaceScriptEngine- Parameters:
bindings- The specifiedBindings.scope- The specified scope.- Throws:
IllegalArgumentException- if the value of scope is invalid for the type thecontextfield.NullPointerException- if the bindings is null and the scope isScriptContext.ENGINE_SCOPE
-
put
public void put(String key, Object value)
Sets the specified value with the specified key in theENGINE_SCOPEBindingsof the protectedcontextfield.- Specified by:
putin interfaceScriptEngine- Parameters:
key- The specified key.value- The specified value.- Throws:
NullPointerException- if key is null.IllegalArgumentException- if key is empty.
-
get
public Object get(String key)
Gets the value for the specified key in theENGINE_SCOPEof the protectedcontextfield.- Specified by:
getin interfaceScriptEngine- Parameters:
key- The key whose value is to be returned- Returns:
- The value for the specified key.
- Throws:
NullPointerException- if key is null.IllegalArgumentException- if key is empty.
-
eval
public Object eval(Reader reader, Bindings bindings) throws ScriptException
eval(Reader, Bindings)calls the abstracteval(Reader, ScriptContext)method, passing it aScriptContextwhose Reader, Writers and Bindings for scopes other thatENGINE_SCOPEare identical to those members of the protectedcontextfield. The specifiedBindingsis used instead of theENGINE_SCOPEBindingsof thecontextfield.- Specified by:
evalin interfaceScriptEngine- Parameters:
reader- AReadercontaining the source of the script.bindings- ABindingsto use for theENGINE_SCOPEwhile the script executes.- Returns:
- The return value from
eval(Reader, ScriptContext) - Throws:
ScriptException- if an error occurs in script.NullPointerException- if any of the parameters is null.
-
eval
public Object eval(String script, Bindings bindings) throws ScriptException
Same aseval(Reader, Bindings)except that the abstracteval(String, ScriptContext)is used.- Specified by:
evalin interfaceScriptEngine- Parameters:
script- AStringcontaining the source of the script.bindings- ABindingsto use as theENGINE_SCOPEwhile the script executes.- Returns:
- The return value from
eval(String, ScriptContext) - Throws:
ScriptException- if an error occurs in script.NullPointerException- if any of the parameters is null.
-
eval
public Object eval(Reader reader) throws ScriptException
eval(Reader)calls the abstracteval(Reader, ScriptContext)passing the value of thecontextfield.- Specified by:
evalin interfaceScriptEngine- Parameters:
reader- AReadercontaining the source of the script.- Returns:
- The return value from
eval(Reader, ScriptContext) - Throws:
ScriptException- if an error occurs in script.NullPointerException- if any of the parameters is null.
-
eval
public Object eval(String script) throws ScriptException
Same aseval(Reader)except that the abstracteval(String, ScriptContext)is used.- Specified by:
evalin interfaceScriptEngine- Parameters:
script- AStringcontaining the source of the script.- Returns:
- The return value from
eval(String, ScriptContext) - Throws:
ScriptException- if an error occurs in script.NullPointerException- if any of the parameters is null.
-
getScriptContext
protected ScriptContext getScriptContext(Bindings nn)
Returns aSimpleScriptContext. TheSimpleScriptContext:
- Uses the specified
Bindingsfor itsENGINE_SCOPE - Uses the
Bindingsreturned by the abstractgetGlobalScopemethod as itsGLOBAL_SCOPE - Uses the Reader and Writer in the default
ScriptContextof thisScriptEngine
ASimpleScriptContextreturned by this method is used to implement eval methods using the abstracteval(Reader,Bindings)andeval(String,Bindings)versions.- Parameters:
nn- Bindings to use for theENGINE_SCOPE- Returns:
- The
SimpleScriptContext
- Uses the specified
-
-