Interface Control
- All Known Implementing Classes:
ClassReferenceControl
,ConstantPoolControl
,TimeLimitControl
JavaBox
.
Script controls are allowed to modify a script's generated bytecode, for example, to prevent linking to certain classes, weave in periodic checsks, etc.
Control Contexts
Controls are given per-container and per-execution contexts, to which they may create and attach
their own private state. The lifecycle methods initialize()
and shutdown()
are for managing per-container context, and startExecution()
and
finishExecution()
are for per-execution context. An "execution" happens with
script snippets that contain statements or expressions; snippets that simply declare classes, etc.,
do not execute (not immediately, anyway).
If the return value from a script execution is an invokable object, then any subsequent invocations into that object's methods will not have any per-container or per-execution context, because that execution will be happening outside of the container. However, a control could use other tricks to regain context outside of the container, e.g., wrapping return values in a proxy interface, static method invocations that provide a unique symbolic key, etc.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final record
static final record
The per-execution context associated with aControl
. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
finishExecution
(Control.ExecutionContext context, Object result, Throwable error) Notification that the execution of a script snippet has finished, either successfully or by throwing an exception.default Object
initialize
(JavaBox box) Initialize this control for the givenJavaBox
and return any associated private context.default byte[]
modifyBytecode
(ClassDesc name, byte[] bytecode) Apply this control to the given class which was generated from a script.default void
shutdown
(Control.ContainerContext context) Shutdown this control for the givenJavaBox
.default Object
startExecution
(Control.ContainerContext context) Notification that the execution of a script snippet has started.
-
Method Details
-
initialize
Initialize this control for the givenJavaBox
and return any associated private context.Controls may create their own private context on a per-
JavaBox
basis using this method. If no private context is needed, this method may return null.This method should also perform any other required per-container initialization, for example, loading custom support classes.
When the given
JavaBox
is closed,shutdown()
will be invoked with aControl.ContainerContext
referencing the returned context object.The default implementation in
Control
returns null.- Parameters:
box
- theJavaBox
instance- Returns:
- this control's private context for the given
JavaBox
, or null if none is needed - Throws:
JavaBoxException
- if some error occurs
-
shutdown
- Parameters:
context
- the container context for this control
-
modifyBytecode
Apply this control to the given class which was generated from a script.The default implementation in
Control
just returnsbytecode
.- Parameters:
name
- the name of the class being addedbytecode
- the Java bytecode of the class being added- Returns:
- replacement java bytecode; must not be null
- Throws:
ControlViolationException
- if the class contains something rejected by this controlJavaBoxException
- if some other error occurs
-
startExecution
Notification that the execution of a script snippet has started.This method should initialize this control for the new execution and return any associated private context.
This method should also perform any required per-execution initialization, for example, initializing resource counters, etc.
The current thread will be the thread that is actually executing the script snippet. A
Control.ExecutionContext
will be created using the returned private context and made available in this thread viaJavaBox.executionContextFor()
, and also provided tofinishExecution()
.The default implementation in
Control
returns null.- Parameters:
context
- the container context for this control
-
finishExecution
Notification that the execution of a script snippet has finished, either successfully or by throwing an exception.The current thread will be the thread that actually executed the snippet.
The default implementation in
Control
does nothing.- Parameters:
context
- the execution context for this controlresult
- the return value from the successful execution of an expression snippet, otherwise nullerror
- the exception thrown by snippet execution if there was an error, otherwise null
-