code: private ScriptEngine setupEngine() throws ProxyEvaluationException {
      ScriptEngineManager mng = new ScriptEngineManager();
      ScriptEngine newEngine = mng.getEngineByMimeType("text/javascript");
      assert newEngine != null : "Javascript engine not available";
      newEngine.put(SCRIPT_METHODS_OBJECT, new PacScriptMethods());
  
      Class<?> scriptMethodsClazz = ScriptMethods.class;
      Method[] scriptMethods = scriptMethodsClazz.getMethods();
  
      for (Method method : scriptMethods) {
        String name = method.getName();
        int args = method.getParameterTypes().length;
        StringBuilder toEval = new StringBuilder(name).append(" = function(");
        for (int i = 0; i < args; i++) {
          if (i > 0) {
            toEval.append(",");
          }
          toEval.append("arg").append(i);
        }
        toEval.append(") {return ");
  
        String functionCall = buildFunctionCallCode(name, args);
  
        // If return type is java.lang.String convert it to a JS string
        if (String.class.isAssignableFrom(method.getReturnType())) {
          functionCall = "String(" + functionCall + ")";
        }
        toEval.append(functionCall).append("; }");
        try {
          newEngine.eval(toEval.toString());
        } catch (ScriptException e) {
          log.log(Level.INFO, "JS evaluation error when creating alias for "
              + name + ".", e);
          throw new ProxyEvaluationException("Error setting up script engine", e);
        }
      }
  
      return newEngine;
    }
docs: ***********************************************************************
  Initializes the JavaScript engine and adds aliases for the functions
  defined in ScriptMethods.
  
  @throws ProxyEvaluationException
  on error.
  **********************************************************************