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 StringINCLUDE_METHODUsed by code completion.static StringLOAD_JAR_METHODUsed by code completion.static StringLOAD_MODULE_METHODUsed by code completion.static StringMODULE_NAMEFields 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 voidaddModuleCallback(IModuleCallbackProvider callbackProvider)Register a callback provider for module functions.voidaddModuleListener(IModuleListener listener)static voidbootstrap()Objectexecute(Object data)Execute script code.voidexit(Object value)Terminates script execution immediately.ObjectgetModule(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.ModuleDefinitiongetModuleDefinition(Object moduleInstance)Retrieve a definition for a given module instance.ObjectgetModuleInstance(ModuleDefinition definition)List<Object>getModules()Retrieve a list of loaded modules.IScriptEnginegetScriptEngine()Get the current script engine instance.static StringgetWrappedVariableName(Object toBeWrapped)booleanhasMethodCallback(String methodToken)Check if java callbacks are registered for a module method.voidhelp(String topic)Open help page on addressed topic.Objectinclude(Object resource)Include and execute a script file.booleanloadJar(Object location)Add a jar file to the classpath.ObjectloadModule(String moduleIdentifier, boolean useCustomNamespace)Load a module.voidpostMethodCallback(String methodToken, Object result)voidpreMethodCallback(String methodToken, Object... parameters)voidprint(Object text, boolean lineFeed)Write a message to the output stream of the script engine.voidprintError(Object text, boolean printOnce)Write a message to the error stream of the script engine.StringreadInput(boolean blocking)Read a single line of data from the default input stream of the script engine.StringregisterMethod(Method method)Register a method in the environment to allow for callbacks.voidremoveModuleListener(IModuleListener listener)Objectwrap(Object toBeWrapped, boolean useCustomNamespace)Wrap a java instance.Methods inherited from class org.eclipse.ease.modules.AbstractScriptModule
initializeMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.eclipse.ease.modules.IScriptModule
initialize
-
Field Details
-
MODULE_NAME
- See Also:
- Constant Field Values
-
INCLUDE_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
- Constant Field Values
-
LOAD_JAR_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
- Constant Field Values
-
LOAD_MODULE_METHOD
Used by code completion. Keep in sync with method name in this class.- See Also:
- Constant Field Values
-
-
Constructor Details
-
EnvironmentModule
public EnvironmentModule()
-
-
Method Details
-
bootstrap
- Throws:
ExecutionException
-
getWrappedVariableName
-
getModuleInstance
-
loadModule
public final Object loadModule(String moduleIdentifier, @ScriptParameter(defaultValue="false") boolean useCustomNamespace) throws ExecutionExceptionDescription copied from interface:IEnvironmentLoad 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:
loadModulein interfaceIEnvironment- Parameters:
moduleIdentifier- name of module to loaduseCustomNamespace- set totrueif 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:
getModulein 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:
getModulein interfaceIEnvironment- Parameters:
clazz- module class to look resolve- Returns:
- resolved module instance or
null
-
getModules
Description copied from interface:IEnvironmentRetrieve a list of loaded modules. The returned list is read only.- Specified by:
getModulesin interfaceIEnvironment- Returns:
- list of modules (might be empty)
-
getModuleDefinition
Description copied from interface:IEnvironmentRetrieve a definition for a given module instance.- Specified by:
getModuleDefinitionin 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:
printin interfaceIEnvironment- Parameters:
text- message to writelineFeed-trueto 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:
addModuleListenerin interfaceIEnvironment
-
removeModuleListener
- Specified by:
removeModuleListenerin 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-trueresults in a blocking call until data is available,falsereturns 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 ExecutionExceptionDescription copied from interface:IEnvironmentWrap 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 typeWrapToScriptonly 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:
wrapin interfaceIEnvironment- Parameters:
toBeWrapped- instance to be wrappeduseCustomNamespace- set totrueif 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 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:
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:
getScriptEnginein interfaceIEnvironment- Returns:
IScriptEngineinstance
-
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,FileorIFile- Returns:
truewhen 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:IEnvironmentRegister a callback provider for module functions.- Specified by:
addModuleCallbackin 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:
truewhen callbacks are registered
-
preMethodCallback
-
postMethodCallback
-
registerMethod
Description copied from interface:IEnvironmentRegister a method in the environment to allow for callbacks.- Specified by:
registerMethodin interfaceIEnvironment- Parameters:
method- method to be registered- Returns:
- unique ID identifying the method
-