AbstractScriptEngine (Java SE 26 & JDK 26)
Description: declaration: module: java.scripting, package: javax.script, class: AbstractScriptEngine
Keywords: javax.script.AbstractScriptEngine class

# Class AbstractScriptEngine

java.lang.Object
javax.script.AbstractScriptEngine

All Implemented Interfaces:
  `ScriptEngine`

public abstract class AbstractScriptEngine extends Object implements ScriptEngine
Provides a standard implementation for several of the variants of the `eval` method.
`eval(Reader)`

`eval(String)`

`eval(String, Bindings)`

`eval(Reader, Bindings)`
are implemented using the abstract methods
`eval(Reader,ScriptContext)` or `eval(String, ScriptContext)`
with a `SimpleScriptContext`.
A `SimpleScriptContext` is used as the default `ScriptContext` of the `AbstractScriptEngine`..

Since:
  1.6

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected ScriptContext`
  `context`
  The default `ScriptContext` of this `AbstractScriptEngine`.

  ### Fields declared in interface ScriptEngine

  `ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME`
  Modifier and Type
  Field
  Description
  `static final String`
  `ARGV`
  Reserved key for a named value that passes an array of positional arguments to a script.
  `static final String`
  `ENGINE`
  Reserved key for a named value that is the name of the `ScriptEngine` implementation.
  `static final String`
  `ENGINE_VERSION`
  Reserved key for a named value that identifies the version of the `ScriptEngine` implementation.
  `static final String`
  `FILENAME`
  Reserved key for a named value that is the name of the file being executed.
  `static final String`
  `LANGUAGE`
  Reserved key for a named value that is the full name of Scripting Language supported by the implementation.
  `static final String`
  `LANGUAGE_VERSION`
  Reserved key for the named value that identifies the version of the scripting language supported by the implementation.
  `static final String`
  `NAME`
  Reserved key for a named value that identifies the short name of the scripting language.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AbstractScriptEngine()`
  Creates a new instance of AbstractScriptEngine using a `SimpleScriptContext` as its default `ScriptContext`.
  `AbstractScriptEngine(Bindings n)`
  Creates a new instance using the specified `Bindings` as the `ENGINE_SCOPE` `Bindings` in the protected `context` field.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Object`
  `eval(Reader reader)`
  `eval(Reader)` calls the abstract `eval(Reader, ScriptContext)` passing the value of the `context` field.
  `Object`
  `eval(Reader reader, Bindings bindings)`
  `eval(Reader, Bindings)` calls the abstract `eval(Reader, ScriptContext)` method, passing it a `ScriptContext` whose Reader, Writers and Bindings for scopes other that `ENGINE_SCOPE` are identical to those members of the protected `context` field.
  `Object`
  `eval(String script)`
  Same as `eval(Reader)` except that the abstract `eval(String, ScriptContext)` is used.
  `Object`
  `eval(String script, Bindings bindings)`
  Same as `eval(Reader, Bindings)` except that the abstract `eval(String, ScriptContext)` is used.
  `Object`
  `get(String key)`
  Gets the value for the specified key in the `ENGINE_SCOPE` of the protected `context` field.
  `Bindings`
  `getBindings(int scope)`
  Returns the `Bindings` with the specified scope value in the protected `context` field.
  `ScriptContext`
  `getContext()`
  Returns the value of the protected `context` field.
  `protected ScriptContext`
  `getScriptContext(Bindings nn)`
  Returns a `SimpleScriptContext`.
  `void`
  `put(String key, Object value)`
  Sets the specified value with the specified key in the `ENGINE_SCOPE` `Bindings` of the protected `context` field.
  `void`
  `setBindings(Bindings bindings, int scope)`
  Sets the `Bindings` with the corresponding scope value in the `context` field.
  `void`
  `setContext(ScriptContext ctxt)`
  Sets the value of the protected `context` field to the specified `ScriptContext`.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

  ### Methods declared in interface ScriptEngine

  `createBindings, eval, eval, getFactory`
  Modifier and Type
  Method
  Description
  `Bindings`
  `createBindings()`
  Returns an uninitialized `Bindings`.
  `Object`
  `eval(Reader reader, ScriptContext context)`
  Same as `eval(String, ScriptContext)` where the source of the script is read from a `Reader`.
  `Object`
  `eval(String script, ScriptContext context)`
  Causes the immediate execution of the script whose source is the String passed as the first argument.
  `ScriptEngineFactory`
  `getFactory()`
  Returns a `ScriptEngineFactory` for the class to which this `ScriptEngine` belongs.

- ## Field Details

  - ### context

    protected ScriptContext context
    The default `ScriptContext` of this `AbstractScriptEngine`.

- ## Constructor Details

  - ### AbstractScriptEngine

    public AbstractScriptEngine()
    Creates a new instance of AbstractScriptEngine using a `SimpleScriptContext` as its default `ScriptContext`.

  - ### AbstractScriptEngine

    public AbstractScriptEngine(Bindings n)
    Creates a new instance using the specified `Bindings` as the `ENGINE_SCOPE` `Bindings` in the protected `context` field.
    Parameters:
      `n` - The specified `Bindings`.
    Throws:
      `NullPointerException` - if n is null.

- ## Method Details

  - ### setContext

    public void setContext(ScriptContext ctxt)
    Sets the value of the protected `context` field to the specified `ScriptContext`.
    Specified by:
      `setContext` in interface `ScriptEngine`
    Parameters:
      `ctxt` - The specified `ScriptContext`.
    Throws:
      `NullPointerException` - if ctxt is null.

  - ### getContext

    public ScriptContext getContext()
    Returns the value of the protected `context` field.
    Specified by:
      `getContext` in interface `ScriptEngine`
    Returns:
      The value of the protected `context` field.

  - ### getBindings

    public Bindings getBindings(int scope)
    Returns the `Bindings` with the specified scope value in the protected `context` field.
    Specified by:
      `getBindings` in interface `ScriptEngine`
    Parameters:
      `scope` - The specified scope
    Returns:
      The corresponding `Bindings`.
    Throws:
      `IllegalArgumentException` - if the value of scope is invalid for the type the protected `context` field.

  - ### setBindings

    public void setBindings(Bindings bindings, int scope)
    Sets the `Bindings` with the corresponding scope value in the `context` field.
    Specified by:
      `setBindings` in interface `ScriptEngine`
    Parameters:
      `bindings` - The specified `Bindings`.
      `scope` - The specified scope.
    Throws:
      `IllegalArgumentException` - if the value of scope is invalid for the type the `context` field.
      `NullPointerException` - if the bindings is null and the scope is `ScriptContext.ENGINE_SCOPE`

  - ### put

    public void put(String key, Object value)
    Sets the specified value with the specified key in the `ENGINE_SCOPE` `Bindings` of the protected `context` field.
    Specified by:
      `put` in interface `ScriptEngine`
    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 the `ENGINE_SCOPE` of the protected `context` field.
    Specified by:
      `get` in interface `ScriptEngine`
    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 abstract `eval(Reader, ScriptContext)` method, passing it a `ScriptContext` whose Reader, Writers and Bindings for scopes other that `ENGINE_SCOPE` are identical to those members of the protected `context` field. The specified `Bindings` is used instead of the `ENGINE_SCOPE` `Bindings` of the `context` field.
    Specified by:
      `eval` in interface `ScriptEngine`
    Parameters:
      `reader` - A `Reader` containing the source of the script.
      `bindings` - A `Bindings` to use for the `ENGINE_SCOPE` while 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 as `eval(Reader, Bindings)` except that the abstract `eval(String, ScriptContext)` is used.
    Specified by:
      `eval` in interface `ScriptEngine`
    Parameters:
      `script` - A `String` containing the source of the script.
      `bindings` - A `Bindings` to use as the `ENGINE_SCOPE` while 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 abstract `eval(Reader, ScriptContext)` passing the value of the `context` field.
    Specified by:
      `eval` in interface `ScriptEngine`
    Parameters:
      `reader` - A `Reader` containing 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 as `eval(Reader)` except that the abstract `eval(String, ScriptContext)` is used.
    Specified by:
      `eval` in interface `ScriptEngine`
    Parameters:
      `script` - A `String` containing 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 a `SimpleScriptContext`. The `SimpleScriptContext`:
    - Uses the specified `Bindings` for its `ENGINE_SCOPE`
    - Uses the `Bindings` returned by the abstract `getGlobalScope` method as its `GLOBAL_SCOPE`
    - Uses the Reader and Writer in the default `ScriptContext` of this `ScriptEngine`
    A `SimpleScriptContext` returned by this method is used to implement eval methods using the abstract `eval(Reader,Bindings)` and `eval(String,Bindings)` versions.
    Parameters:
      `nn` - Bindings to use for the `ENGINE_SCOPE`
    Returns:
      The `SimpleScriptContext`