Package org.eclipse.ease.modules
Class EnvironmentModule
java.lang.Object
org.eclipse.ease.modules.AbstractScriptModule
org.eclipse.ease.modules.EnvironmentModule
- All Implemented Interfaces:
IEnvironment
,IScriptModule
The Environment provides base functions for all script interpreters. It is automatically loaded by any interpreter upon startup.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Used by code completion.static final String
Used by code completion.static final String
Used by code completion.static final String
static final String
Fields inherited from interface org.eclipse.ease.modules.IEnvironment
EASE_CODE_PREFIX, MODULE_PREFIX
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addModuleCallback
(IModuleCallbackProvider callbackProvider) Register a callback provider for module functions.void
addModuleListener
(IModuleListener listener) static void
final Object
Execute script code.final void
Terminates script execution immediately.formatText
(String format, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7, Object arg8, Object arg9) Format a text with format qualifiers.static List<ModuleDefinition>
getLoadedModules
(IScriptEngine engine) Get all loaded modules for a given script engine.final Object
Resolves a loaded module and returns the Java instance.<T,
U extends Class<T>>
TgetModule
(U clazz) Resolves a loaded module by its class.getModuleDefinition
(Object moduleInstance) Retrieve a definition for a given module instance.getModuleInstance
(ModuleDefinition definition) Retrieve a list of loaded modules.Get the current script engine instance.static final String
getWrappedVariableName
(Object toBeWrapped) boolean
hasMethodCallback
(String methodToken) Check if java callbacks are registered for a module method.void
Open help page on addressed topic.final Object
Include and execute a script file.boolean
Add a jar file to the classpath.final Object
loadModule
(String moduleIdentifier, boolean useCustomNamespace) Load a module.void
postMethodCallback
(String methodToken, Object result) void
preMethodCallback
(String methodToken, Object... parameters) final void
Write a message to the output stream of the script engine.final void
printError
(Object text, boolean printOnce) Write a message to the error stream of the script engine.readInput
(boolean blocking) Read a single line of data from the default input stream of the script engine.registerMethod
(Method method) Register a method in the environment to allow for callbacks.void
removeModuleListener
(IModuleListener listener) Convert input to a Java double.Convert input to a Java int.Convert input to a JavaString
.Wrap a java instance.Methods inherited from class org.eclipse.ease.modules.AbstractScriptModule
initialize
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.ease.modules.IScriptModule
initialize
-
Field Details
-
MODULE_ID
- See Also:
-
MODULE_NAME
- See Also:
-
INCLUDE_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
-
LOAD_JAR_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
-
LOAD_MODULE_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
-
-
Constructor Details
-
EnvironmentModule
public EnvironmentModule()
-
-
Method Details
-
bootstrap
- Throws:
ExecutionException
-
getWrappedVariableName
-
getLoadedModules
Get all loaded modules for a given script engine.- Parameters:
engine
- engine to parse- Returns:
- module definitions for all loaded modules (index 0 references the module loaded last)
-
getModuleInstance
-
loadModule
public final Object loadModule(String moduleIdentifier, @ScriptParameter(defaultValue="false") boolean useCustomNamespace) throws ExecutionException Description copied from interface:IEnvironment
Load a module. Loading a module generally enhances the script environment with new functions and variables. If a module was already loaded before, it gets refreshed and moved to the top of the module stack. When a module is loaded, all its dependencies are loaded too. So loading one module might change the whole module stack.When not using a custom namespace all variables and functions are loaded to the global namespace, possibly overriding existing functions. In such cases the Java module instance is returned. When useCustomNamespace is used a dynamic script object is created and returned.In such cases the global namespace is not changed. The namespace behavior is also used for loading dependencies.
- Specified by:
loadModule
in interfaceIEnvironment
- Parameters:
moduleIdentifier
- name of module to loaduseCustomNamespace
- set totrue
if functions and constants should not be stored to the global namespace but to a custom object- Returns:
- loaded module instance
- Throws:
ExecutionException
- when execution of wrapped module code fails
-
getModule
Resolves a loaded module and returns the Java instance. Will only query previously loaded modules.- Specified by:
getModule
in interfaceIEnvironment
- Parameters:
name
- name of the module to resolve- Returns:
- resolved module instance or
null
-
getModule
Resolves a loaded module by its class.- Specified by:
getModule
in interfaceIEnvironment
- Parameters:
clazz
- module class to look resolve- Returns:
- resolved module instance or
null
-
getModules
Description copied from interface:IEnvironment
Retrieve a list of loaded modules. The returned list is read only.- Specified by:
getModules
in interfaceIEnvironment
- Returns:
- list of modules (might be empty)
-
getModuleDefinition
Description copied from interface:IEnvironment
Retrieve a definition for a given module instance.- Specified by:
getModuleDefinition
in interfaceIEnvironment
- Parameters:
moduleInstance
- instance of module to retrieve definition for- Returns:
- module definition
-
print
public final void print(@ScriptParameter(defaultValue="") Object text, @ScriptParameter(defaultValue="true") boolean lineFeed) Write a message to the output stream of the script engine.- Specified by:
print
in interfaceIEnvironment
- Parameters:
text
- message to writelineFeed
-true
to add a line feed after the text
-
formatText
public String formatText(String format, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg1, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg2, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg3, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg4, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg5, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg6, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg7, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg8, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object arg9) Format a text with format qualifiers. Using parameters you may format numbers and objects according toFormatter
rules. Further inline tokens in the form ${identifier} will be replaced by script variables, system properties and environment variables.- Parameters:
format
- format stringarg1
- formatter contentarg2
- formatter contentarg3
- formatter contentarg4
- formatter contentarg5
- formatter contentarg6
- formatter contentarg7
- formatter contentarg8
- formatter contentarg9
- formatter content- Returns:
- formatted string
-
printError
public final void printError(@ScriptParameter(defaultValue="") Object text, @ScriptParameter(defaultValue="false") boolean printOnce) Write a message to the error stream of the script engine.- Parameters:
text
- message to writeprintOnce
- Iftrue
, the text in parameter will be printed only once and ignored in other calls of this method. Iffalse
, it will be printed in all cases.
-
addModuleListener
- Specified by:
addModuleListener
in interfaceIEnvironment
-
removeModuleListener
- Specified by:
removeModuleListener
in interfaceIEnvironment
-
readInput
Read a single line of data from the default input stream of the script engine. Depending on the blocking parameter this method will wait for user input or return immediately with any available data.- Parameters:
blocking
-true
results in a blocking call until data is available,false
returns in any case- Returns:
- string data from input stream
- Throws:
IOException
- when reading on the input stream fails
-
wrap
public Object wrap(Object toBeWrapped, @ScriptParameter(defaultValue="false") boolean useCustomNamespace) throws ExecutionException Description copied from interface:IEnvironment
Wrap a java instance. Will create accessors in the target language for methods and constants defined by the java instance toBeWrapped. If the instance contains annotations of typeWrapToScript
only these will be wrapped. If no annotation can be found, all public methods/constants will be wrapped. As some target languages might not support method overloading this might result in some methods not wrapped correctly.- Specified by:
wrap
in interfaceIEnvironment
- Parameters:
toBeWrapped
- instance to be wrappeduseCustomNamespace
- set totrue
if functions and constants should not be stored to the global namespace but to the return value only- Returns:
- wrapped object instance or java class when put to global namespace
- Throws:
ExecutionException
- when execution of wrapped code fails
-
execute
Execute script code. This method executes script code directly in the running interpreter. Execution is done in the same thread as the caller thread.- Parameters:
data
- code to be interpreted- Returns:
- result of code execution
- Throws:
ExecutionException
- when execution of data fails
-
exit
public final void exit(@ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object value) throws ExitException Terminates script execution immediately. Code following this command will not be executed anymore.- Parameters:
value
- return code- Throws:
ExitException
- always
-
include
Include and execute a script file. Quite similar to eval(Object) a source file is opened and its content is executed. Multiple sources are available: "workspace://" opens a file relative to the workspace root, "project://" opens a file relative to the current project, "file://" opens a file from the file system. All other types of URIs are supported too (like http:// ...). You may also use absolute and relative paths as defined by your local file system.- Parameters:
resource
- name or instance of resource to be included- Returns:
- result of include operation
- Throws:
ExecutionException
- when included code fails
-
getScriptEngine
Get the current script engine instance.- Specified by:
getScriptEngine
in interfaceIEnvironment
- Returns:
IScriptEngine
instance
-
loadJar
Add a jar file to the classpath. Contents of the jar can be accessed right after loading. location can be an URL, a path, a File or an IFile instance. Adding a jar location does not necessary mean that its classes can be accessed. If the source is not accessible, then its classes are not available for scripting, too.- Parameters:
location
-URL
,Path
,File
orIFile
- Returns:
true
when input could be converted to a URL- Throws:
MalformedURLException
- invalid URL detected
-
help
public void help(@ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String topic) Open help page on addressed topic. If the given topic matches a method or field from a loaded module, the definition will be opened. If the topic is unknown, a search in the whole eclipse help will be launched.- Parameters:
topic
- help topic to open (typically a function name)
-
toInt
Convert input to a Java int.- Parameters:
element
- element to convert- Returns:
- converted value
-
toDouble
Convert input to a Java double.- Parameters:
element
- element to convert- Returns:
- converted value
-
toString
Convert input to a JavaString
.- Parameters:
element
- element to convert- Returns:
- converted value
-
addModuleCallback
Description copied from interface:IEnvironment
Register a callback provider for module functions.- Specified by:
addModuleCallback
in interfaceIEnvironment
- Parameters:
callbackProvider
- callback provider instance
-
hasMethodCallback
Check if java callbacks are registered for a module method. This method get called on each module function invocation.ATTENTION: needed by dynamic script code, do not alter synopsis!
- Parameters:
methodToken
- unique method token- Returns:
true
when callbacks are registered
-
preMethodCallback
-
postMethodCallback
-
registerMethod
Description copied from interface:IEnvironment
Register a method in the environment to allow for callbacks.- Specified by:
registerMethod
in interfaceIEnvironment
- Parameters:
method
- method to be registered- Returns:
- unique ID identifying the method
-