Java Utililty Methods Script Engine

List of utility methods to do Script Engine

Description

The list of methods to do Script Engine are organized into topic(s).

Method

ScriptEngineManagergetScriptEngineManager()
get Script Engine Manager
if (scriptEngineManager == null) {
    scriptEngineManager = new ScriptEngineManager();
return scriptEngineManager;
StringgetStringOrExcept(ScriptEngine e, String vname)
get String Or Except
Object r = e.get(vname);
if (r == null || !(r instanceof String)) {
    throw new IllegalArgumentException("No variable named " + vname + " in script engine that is a String");
return (String) r;
StringgetVersionString(ScriptEngineFactory factory)
get Version String
return factory.getLanguageVersion();
voidimportClass(ScriptEngine e, Class pkg, Bindings... b)
import Class
try {
    if (b.length == 0) {
        e.eval("importClass(Packages." + pkg.getName() + ")");
    } else {
        e.eval("importClass(Packages." + pkg.getName() + ")", b[0]);
} catch (ScriptException e1) {
    throw new RuntimeException(e1);
...
booleanisJsEngineAvailable(String engineName, Logger logger)
Checks if a JS engine is available
boolean engineExists = new ScriptEngineManager().getEngineByName(engineName) != null;
if (!engineExists) {
    logger.warn("The JS engine '" + engineName + "' is not available for tests.");
return engineExists;
voidlistGems(ScriptEngine jruby)
Used to print a list of install gems to stdout
StringBuilder script = new StringBuilder();
script.append("require 'rubygems'\n");
script.append("require 'rubygems/gem_runner'\n");
script.append("Gem::GemRunner.new.run [\"list\"]\n");
jruby.eval(script.toString());
voidlistScriptEngines()
list Script Engines
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
int num = 0;
for (ScriptEngineFactory factory : factories) {
    System.out.println("ScriptEngineFactory Info");
    String engName = factory.getEngineName();
    String engVersion = factory.getEngineVersion();
    String langName = factory.getLanguageName();
...
voidregisterFactoryToManager(ScriptEngineFactory factory, ScriptEngineManager scriptEngineManager)
register Factory To Manager
for (String name : factory.getNames()) {
    scriptEngineManager.registerEngineName(name, factory);
for (String extension : factory.getExtensions()) {
    scriptEngineManager.registerEngineExtension(extension, factory);
for (String mime : factory.getMimeTypes()) {
    scriptEngineManager.registerEngineMimeType(mime, factory);
...
ScriptEngineretrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName)
Retrieve a ScriptEngine from the given ScriptEngineManager by name, delegating to ScriptEngineManager#getEngineByName but throwing a descriptive exception if not found or if initialization failed.
ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
if (engine == null) {
    Set<String> engineNames = new LinkedHashSet<>();
    for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) {
        List<String> factoryNames = engineFactory.getNames();
        if (factoryNames.contains(engineName)) {
            try {
                engine = engineFactory.getScriptEngine();
...
ObjectscriptEngine(String epStr, Map per)
script Engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
for (Iterator iterator = per.keySet().iterator(); iterator.hasNext();) {
    String type = (String) iterator.next();
    engine.put(type, per.get(type));
return engine.eval(epStr);