public final class Scripting extends Object
ScriptEngineManager
manager.
Rather than using JDK's ScriptEngineManager
manager directly,
instantiate it via Scripting.createManager()
method. This method is aware
of NetBeans specific runtime configurations. It uses the right classloader
as well as specific discovery mechanisms to locate additional
implementations of ScriptEngineFactory
. To execute a JavaScript
code use:
ScriptEngine
js =Scripting
.createManager().getEngineByMimeType("text/javascript"); assert js != null;Number
x = (Number
) js.eval("6 * 7"); assert x.intValue() == 42;
Consult scripting tutorial to learn more about advanced polyglot scripting topics.
Modifier and Type | Method and Description |
---|---|
Scripting |
allowAllAccess(boolean allAccess)
Allows the scripts to access JVM classes.
|
ScriptEngineManager |
build()
Create new
ScriptEngineManager configured for the NetBeans
environment. |
static ScriptEngineManager |
createManager()
Create new
ScriptEngineManager configured for the NetBeans
environment. |
static Scripting |
newBuilder()
A builder to configure and create new instance of
ScriptEngineManager . |
public static ScriptEngineManager createManager()
ScriptEngineManager
configured for the NetBeans
environment. The manager serves as an isolated environment -
engines created from the same manager are supposed to share the
same internals and be able to communicate with each other.public static Scripting newBuilder()
ScriptEngineManager
.Scripting.build()
methodpublic Scripting allowAllAccess(boolean allAccess)
Object
fn = engine.eval("(function(obj) {\n"
+ " var Long = Java.type('java.lang.Long');\n"
+ " return new Long(33);\n"
+ "})\n");
Such classloading is prevented by default. To allow it, specify true
in here.
ScriptEngineManager
created with all access on, has a boolean property
in its ScriptEngineManager.getBindings()
:
ScriptEngineManager
manager =Scripting
.newBuilder(). allowAllAccess(true).build(); assertEquals(Boolean
.TRUE, manager.getBindings().get("allowAllAccess"));
allAccess
- allow access to JVM internals from the scriptthis
builderpublic ScriptEngineManager build()
ScriptEngineManager
configured for the NetBeans
environment. The manager serves as an isolated environment -
engines created from the same manager are supposed to share the
same internals and be able to communicate with each other.