Class RexxScriptEngine
- All Implemented Interfaces:
Compilable,Invocable,ScriptEngine
javax.script.ScriptEngine class and implements the
javax.script.Compilalbe and * the javax.script.Invocable interface,
although the latter one would only make sense in RexxCompiledScript class, because of the
getInterface(Class clasz) method. Therefore the author (Rony
G. Flatscher who served on the JSR-223 specification committee) thinks that the documentation
in javax.script.Invocable is false, as it states that Invocable gets implemented by a
ScriptEngine, somehow indicating it has to operate on some "state" of the script engine.
Please note: the JSR-223 specification does not imply that ScriptEngine should implement this interface!
The [oo]Rexx implementation of JSR-223 tries to be as flexible as possible and also implements this interface in its implementation of
ScriptEngine sharing as much code between
the tow (and caching compiled scripts for invoking the aforementioned method in a sensible way.- Since:
- 2015-05-11
- Version:
- 104.20230411
- Author:
- Rony G. Flatscher
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInner class for determining a RexxScript annotation, if one exists, the annotation field will refer to the appropriate enum value and all fields are filled in to ease processing of the script's code by the caller. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanThis controls whether the supplied Rexx scripts should get a dynamic requires statement prepended that makes sure that the Java support is present.static booleanDetermines whether the ScriptContext's Reader, Writer and ErrorWriter should replace the Rexx script .input (and also .debugInput, if present), .output, .error (and also .traceOutput if present).static booleanThis controls whether ScriptExceptions should supply theRexxExceptionobject (extendsBSFException) or fill in the ScriptException fields (message, line and column number, if present).static final StringRexx code to dynamically require "BSF.CLS".static StringThe Rexx code to inject for the Rexx script annotation@GET.static StringThe Rexx code to inject for the Rexx script annotation@SET.Fields inherited from class javax.script.AbstractScriptEngine
contextFields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor which creates the peer BSF Rexx engine instance. -
Method Summary
Modifier and TypeMethodDescriptionCompiles (tokenizes) the script (source read from Reader) for later execution.Compiles (tokenizes) the script (source given as a String) for later execution.Creates and returns a SimpleBindings instance.static voidUtility method to dump the content of the suppliedScriptContext.eval(Reader r, ScriptContext sc) Creates the script from the suppliedReaderand uses(String script, ScriptContext sc)to carry out the operation.eval(String script, ScriptContext sc) Creates aRexxCompiledScriptobject for the supplied Rexx script and then usesRexxCompiledScript.eval(ScriptContext sc)to carry out the operation.booleanReturns the current setting ofbAddLatestPackageDynamically.Getter method that returns the BSF scripting engine in use.Getter method for "current" (the latest evaluated) script.Returns the default RexxScriptEngineFactory instance for the Rexx scripting language.static StringUtility method to format aSimpleDateFormatsuch that it can be used as part of a generated filename.<T> TgetInterface(Class<T> clasz) Returns an implementation of an interface using functions compiled in the interpreter.<T> TgetInterface(Object thiz, Class<T> clasz) Returns an implementation of a Java interface where the Java method invocations will be implemented by the methods of the supplied Rexx object (aRexxProxy.getNil()Getter method that returns the ooRexx.nilobject.invokeFunction(String name, Object... args) Invokes (calls) a public Rexx routine defined in this compiled script.invokeMethod(Object thiz, String name, Object... args) Sends a Rexx message to the Rexx object contained in the addressedRexxProxyobject.voidsetAddLatestPackageDynamically(boolean newValue) Allows to change the behaviour from adding thelatestRexxPackageto new Rexx routines.Methods inherited from class javax.script.AbstractScriptEngine
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
-
Field Details
-
bSupplyBSFExceptionObject
public static boolean bSupplyBSFExceptionObjectThis controls whether ScriptExceptions should supply theRexxExceptionobject (extendsBSFException) or fill in the ScriptException fields (message, line and column number, if present). UsingBSFException.getCause()will return theRexxExceptionobject, that has a methodgetRexxConditionObjectwhich is aRexxProxyfor the Rexx directory (cf. the ooRexx condition object documentation for its entries).
If set tofalse, then an attempt is made to supply as much information according to theScriptExceptionconstructors, which might ease generic script editors that use JSR223 script engines. -
bRedirectStandardFiles
public static boolean bRedirectStandardFilesDetermines whether the ScriptContext's Reader, Writer and ErrorWriter should replace the Rexx script .input (and also .debugInput, if present), .output, .error (and also .traceOutput if present). There may be situations (like a GUI based interface to run Rexx scripts) where this automatic redirection should be inhibited. -
strFormatGET
The Rexx code to inject for the Rexx script annotation@GET. Get the last argument (a Rexx Directory, dubbed "slotDir") and fetch the entry "ScriptContext" from it. -
strFormatSET
The Rexx code to inject for the Rexx script annotation@SET. Get the last argument (a Rexx Directory, dubbed "slotDir") and fetch the entry "ScriptContext" from it. -
dynamicallyRequireBSF
Rexx code to dynamically require "BSF.CLS". This makes sure that the native part ofBSF4ooRexxis set to support the camouflaging support from the beginning.- See Also:
-
bPrependDynamicRequiresStatement
public static boolean bPrependDynamicRequiresStatementThis controls whether the supplied Rexx scripts should get a dynamic requires statement prepended that makes sure that the Java support is present. Only honored, if the instance fieldbAddLatestPackageDynamicallyis set tofalse.
-
-
Constructor Details
-
RexxScriptEngine
public RexxScriptEngine()Default constructor which creates the peer BSF Rexx engine instance.
-
-
Method Details
-
getCurrentScript
Getter method for "current" (the latest evaluated) script. Due to the ooRexx architecture each script gets compiled (actually tokenized and its package set up) and will be stored in a field. Each script that gets evaluated will replace thecurrentScriptobject. Fetching thisRexxCompiledScriptallows one to execute it as many times as needed, but also use all of the implementedInvocablemethods.- Returns:
- the current (latest)
RexxCompiledScriptobject ornullif no script has been evaluated or compiled yet
-
setAddLatestPackageDynamically
public void setAddLatestPackageDynamically(boolean newValue) Allows to change the behaviour from adding thelatestRexxPackageto new Rexx routines. Default istrue, which makes sure that any new Rexx routine will get the latest Rexx package added to it, thereby gaining access to the packageBSF.CLSand all previously defined public routines and public classes.- Parameters:
newValue-trueorfalse
-
getAddLatestPackageDynamically
public boolean getAddLatestPackageDynamically()Returns the current setting ofbAddLatestPackageDynamically.- Returns:
- current setting of
bAddLatestPackageDynamically.
-
getBsfRexxEngine
Getter method that returns the BSF scripting engine in use.- Returns:
- the BSF Rexx engine used for this RexxScriptEngine
-
getNil
Getter method that returns the ooRexx.nilobject.- Returns:
- a
RexxProxyreferring to the ooRexx.nilobject
-
getFactory
Returns the default RexxScriptEngineFactory instance for the Rexx scripting language.- Specified by:
getFactoryin interfaceScriptEngine- Returns:
- the default RexxScriptEngine Factory (RexxScriptEngineFactory.DEFAULT_REXX_SCRIPT_ENGINE_FACTORY)
-
createBindings
Creates and returns a SimpleBindings instance.- Specified by:
createBindingsin interfaceScriptEngine- Returns:
- a new SimpleBindings instance
-
eval
Creates the script from the suppliedReaderand uses(String script, ScriptContext sc)to carry out the operation.- Specified by:
evalin interfaceScriptEngine- Parameters:
r- - AReadersupplying the Rexx code to evaluate.sc- - AScriptContextto use for evaluating the script- Returns:
- the object the Rexx routine returns. In the case that a Rexx object gets returned it will
be wrapped (boxed) in a
RexxProxy. Supplying aRexxProxyas an argument to a Rexx routine will cause the contained Rexx object to be used in Rexx instead.
Hint: one can interact with the contained Rexx object of aRexxProxyby sending it messages from Java using itssendMessagemethods. - Throws:
ScriptException
-
eval
Creates aRexxCompiledScriptobject for the supplied Rexx script and then usesRexxCompiledScript.eval(ScriptContext sc)to carry out the operation.- Specified by:
evalin interfaceScriptEngine- Parameters:
script- - The Rexx script to evaluate.sc- - AScriptContextobject supplying information for evaluating the Rexx script- Returns:
- the object the Rexx routine returns. In the case that a Rexx object gets returned it will
be wrapped (boxed) in a
RexxProxy. Supplying aRexxProxyas an argument to a Rexx routine will cause the contained Rexx object to be used in Rexx instead.
Hint: one can interact with the contained Rexx object of aRexxProxyby sending it messages from Java using itssendMessagemethods. - Throws:
ScriptException
-
compile
Compiles (tokenizes) the script (source read from Reader) for later execution. The actual compilation (tokenization) will be carried out bycompile(String script).- Specified by:
compilein interfaceCompilable- Parameters:
script- a Reader that supplies the characters of the script- Returns:
- a CompiledScript that can be repeatedly evaluate()'d or used to invoke functions of the script or methods in an object
- Throws:
ScriptException
-
compile
Compiles (tokenizes) the script (source given as a String) for later execution. The actual compilation (tokenization) will be carried out bycompile(String script, String filename).- Specified by:
compilein interfaceCompilable- Parameters:
script- - The Rexx script to be compiled (tokenized).- Returns:
- a CompiledScript that can be repeatedly evaluate()'d or used to invoke functions of the script or methods in an object
- Throws:
ScriptException
-
getInterface
Returns an implementation of an interface using functions compiled in the interpreter.Note: Uses the implementation in
RexxCompiledScript.getInterface(Class clasz).- Specified by:
getInterfacein interfaceInvocable- Parameters:
clasz- - The Java interface class object<T>, whose methods are implemented as public Rexx routines.- Returns:
- a Java object of type
<T>
-
getInterface
Returns an implementation of a Java interface where the Java method invocations will be implemented by the methods of the supplied Rexx object (aRexxProxy.Hint #1: the external
BSF4ooRexxexternal Rexx functionBSFCreateRexxProxy(...)is much more versatile and among other things allows for implementing multiple Java interface classes in a single Rexx class.Hint #2: when an interface method gets invoked, the Rexx method routine gets all its arguments in the same order supplied to, plus an appended
RexxSlotArgument"slot directory" object, which contains information about this particular method invocation. This slot directory will also contain an entry, named"SCRIPTCONTEXT", which allows the Rexx routine to fully interact with theScriptContextused for its invocation.
(Hint: one can always access this object from Rexx code with the statementscriptcontext=arg('A')~lastItem~scriptContext).)- Specified by:
getInterfacein interfaceInvocable- Parameters:
thiz- - The Java object (must be aRexxProxy) that implements the Java interface methodsclasz- - The Java interface class object<T>, whose methods are implemented in thethizobject- Returns:
- a Java object of type
<T>
-
invokeFunction
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException Invokes (calls) a public Rexx routine defined in this compiled script.Note: Uses the implementation in
RexxCompiledScriptwhere this method belongs to.- Specified by:
invokeFunctionin interfaceInvocable- Parameters:
name- - The name of the public routine to invoke.args- - The arguments (a Java array of typeObject) for the routine.Note # 1: If any argument is of type
RexxProxy, then the Rexx routine will receive the contained Rexx object instead.Note # 2: This method will append an additional Rexx directory object argument with an entry named
"SCRIPTCONTEXT", which allows the Rexx routine to fully interact with theScriptContextused for its invocation.
(Hint: one can always access this object from Rexx code with the statementscriptcontext=arg('A')~lastItem~scriptContext).)- Returns:
- the object the Rexx routine returns. In the case that a Rexx object gets returned it will
be wrapped (boxed) in a
RexxProxy. Supplying aRexxProxyas an argument to a Rexx routine will cause the contained Rexx object to be used in Rexx instead.
Hint: one can interact with the contained Rexx object of aRexxProxyby sending it messages from Java using itssendMessagemethods or theinvokeMethod(Object thiz, String name, Object...args)method. - Throws:
ScriptException- depending on what went wrong in invocationNoSuchMethodException- if method does not exist
-
invokeMethod
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException Sends a Rexx message to the Rexx object contained in the addressedRexxProxyobject.- Specified by:
invokeMethodin interfaceInvocable- Parameters:
thiz- - The wrapped (as aRexxProxy) Rexx object to receive the messagename- - The Rexx message (method) name.args- - The arguments (a Java array of typeObject) for the Rexx method.Note # 1: If any argument is of type
RexxProxy, then the Rexx routine will receive the contained Rexx object instead.Note # 2: This method will append an additional Rexx directory object argument with an entry named
"SCRIPTCONTEXT", which allows the Rexx routine to fully interact with theScriptContextused for its invocation.
(Hint: one can always access this object from Rexx code with the statementscriptcontext=arg('A')~lastItem~scriptContext).)- Returns:
- the object the Rexx routine returns. In the case that a Rexx object gets returned it will
be wrapped (boxed) in a
RexxProxy. Supplying aRexxProxyas an argument to a Rexx routine will cause the contained Rexx object to be used in Rexx instead.
Hint: one can interact with the contained Rexx object of aRexxProxyby sending it messages from Java using itssendMessagemethods or theinvokeMethod(Object thiz, String name, Object...args)method. - Throws:
ScriptException- depending on what went wrong in invocationNoSuchMethodException- if method does not exist
-
dumpScriptContext
Utility method to dump the content of the suppliedScriptContext.- Parameters:
sc- the ScriptContext object for which the Bindings of all scopes get dumped
-
getGMTFormattedDate4Filename
Utility method to format aSimpleDateFormatsuch that it can be used as part of a generated filename.- Returns:
- a string indicating the current date and time suitable to be used as part of a filename
-