java.lang.Object
org.bzdev.scripting.ScriptListenerAdapter
- Direct Known Subclasses:
DefaultSimAdapter
Create an adapter for interfaces representing listeners.
The adapter allows listener interfaces to be implemented in a scripting
language or in Java. A listener interface for this purpose is simply
an interface all of whose methods have no return value and no declared
exceptions. Subclasses will extend this class and implement the
listener interface. Each listener method implemented will simply
call
callScriptMethod(String,Object...)
with a first argument
being the name of a method for an object implemented in a scripting
language. Typically this name will be the same as the method's name,
although this is not necessary.
A subclass should contain two constructors. The constructor with no arguments will set the scripting context and script object to null. The methods will then return without performing any actions. The constructor with two arguments is used when the implementation for the adapter is provided by a scripting language.
Example:
In a script one can create the adapter with code such aspublic interface FooListener { void method1(Object src, int status); void method2(Object src, String msg); } public class FooScriptAdapter extends ScriptListenerAdapter implements FooListener { public FooScriptAdapter() { super(); } public FooScriptAdapter(Object scriptObject) { super(scriptObject); } public FooScriptAdapter(ScriptingContext context, Object scriptObject) { super(context, scriptObject); } void method1(Object src, int status) { callScriptMethod("method1", src, status); } void method2(Object src, String msg) { callScriptMethod("method2", src, msg); } }
var adapter = new FooScriptAdapter(scripting, { method1(src, status) { ... } method2(src, msg) { ... } });
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Exception thrown when a scripting language throws an exception. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Constructor.protected
ScriptListenerAdapter
(ScriptingContext context, Object scriptObject) Constructor given a scripting context and script object.protected
ScriptListenerAdapter
(ExpressionParser.ESPObject scriptObject) Constructor with only a script object. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
callScriptMethod
(String name, Object... args) Call a method defined in a scripting language.
-
Constructor Details
-
ScriptListenerAdapter
protected ScriptListenerAdapter()Constructor. This creates an adapter with no scripting support. -
ScriptListenerAdapter
Constructor with only a script object. This creates an adapter for use with the ESP scripting language when an instance ofScriptingContext
that can provide a script engine is not available (an example of such a case is when the yrunner command is used, in which case ESP will be started without a scripting context).Note: This is equivalent to using the constructor
ScriptListenerAdapter(ScriptingContext,Object)
with a null first argument.- Parameters:
scriptObject
- the scripting-language object implementing the listener interface for this adapter.
-
ScriptListenerAdapter
protected ScriptListenerAdapter(ScriptingContext context, Object scriptObject) throws IllegalArgumentException Constructor given a scripting context and script object. This constructor implements the adapter using a scripting language provided its arguments are not null. If a method is added to the script object after this constructor is called, that method will be ignored, so all of the methods the adapter implements must be defined by the script object when this constructor is calledIf ESP is the scripting language, the context may be null provided that scriptObject is an ESP object. This special case is provided for use with
ObjectNamerLauncher
and the yrunner program.- Parameters:
context
- the scripting context for this adapter; null if there is none or (optionally) if ESP is the scripting languagescriptObject
- the scripting-language object implementing the listener interface for this adapter.- Throws:
IllegalArgumentException
- the script object was ill formed
-
-
Method Details
-
callScriptMethod
Call a method defined in a scripting language. This method will be used to implement the listener interface declared for a subclass of this class.- Parameters:
name
- the name of a methodargs
- the arguments for the method- Throws:
ScriptListenerAdapter.ScriptMethodException
- an error occurred (as a runtime exception, this exception does not have to be caught).UnsupportedOperationException
- scripting is not supported but the constructor set a non-null value for the scripting context and the script objectIllegalArgumentException
- the scriptObject was null or is not an object recognized by the scripting language
-