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
public class EnvironmentModule extends AbstractScriptModule implements IEnvironment
The Environment provides base functions for all script interpreters. It is automatically loaded by any interpreter upon startup.
-
Field Summary
Fields Modifier and Type Field Description static String
MODULE_NAME
Fields inherited from interface org.eclipse.ease.modules.IEnvironment
EASE_CODE_PREFIX, MODULE_PREFIX
-
Constructor Summary
Constructors Constructor Description EnvironmentModule()
-
Method Summary
Modifier and Type Method Description void
addModuleCallback(IModuleCallbackProvider callbackProvider)
Register a callback provider for module functions.void
addModuleListener(IModuleListener listener)
static void
bootstrap()
Object
execute(Object data)
Execute script code.void
exit(Object value)
Terminates script execution immediately.Object
getModule(String name)
Resolves a loaded module and returns the Java instance.<T, U extends Class<T>>
TgetModule(U clazz)
Resolves a loaded module by its class.ModuleDefinition
getModuleDefinition(Object moduleInstance)
Retrieve a definition for a given module instance.Object
getModuleInstance(ModuleDefinition definition)
List<Object>
getModules()
Retrieve a list of loaded modules.IScriptEngine
getScriptEngine()
Get the current script engine instance.static String
getWrappedVariableName(Object toBeWrapped)
boolean
hasMethodCallback(String methodToken)
Check if java callbacks are registered for a module method.void
help(String topic)
Open help page on addressed topic.Object
include(String filename)
Include and execute a script file.boolean
loadJar(Object location)
Add a jar file to the classpath.Object
loadModule(String moduleIdentifier, boolean useCustomNamespace)
Load a module.void
postMethodCallback(String methodToken, Object result)
void
preMethodCallback(String methodToken, Object... parameters)
void
print(Object text, boolean lineFeed)
Write a message to the output stream of the script engine.void
printError(Object text, boolean printOnce)
Write a message to the error stream of the script engine.String
readInput(boolean blocking)
Read a single line of data from the default input stream of the script engine.String
registerMethod(Method method)
Register a method in the environment to allow for callbacks.void
removeModuleListener(IModuleListener listener)
Object
wrap(Object toBeWrapped, boolean useCustomNamespace)
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_NAME
- See Also:
- Constant Field Values
-
-
Constructor Details
-
EnvironmentModule
public EnvironmentModule()
-
-
Method Details
-
bootstrap
- Throws:
ExecutionException
-
getModuleInstance
-
loadModule
public final Object loadModule(String moduleIdentifier, @ScriptParameter(defaultValue="false") boolean useCustomNamespace) throws ExecutionExceptionDescription 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
-
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 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 or
null
- Throws:
IOException
- when reading on the input stream fails
-
wrap
public Object wrap(Object toBeWrapped, @ScriptParameter(defaultValue="false") boolean useCustomNamespace) throws ExecutionExceptionDescription 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
-
getWrappedVariableName
-
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 ExitExceptionTerminates 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:
filename
- name of file 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
- Overrides:
getScriptEngine
in classAbstractScriptModule
- 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)
-
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 invokation.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
-