Package javax.script

Examples of javax.script.Invocable


            Object result = null;

            String methodName = method.getName();
            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
            if (bindings.containsKey(methodName)) {
                Invocable invocable;
                try {
                    invocable = (Invocable)scriptEngine;
                } catch (ClassCastException exception) {
                    throw new SerializationException(exception);
                }

                result = invocable.invokeFunction(methodName, args);
            }

            // If the function didn't return a value, return the default
            if (result == null) {
                Class<?> returnType = method.getReturnType();
View Full Code Here


    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByExtension("rb");
    assertNotNull("Engine should not be null",engine);
    engine.eval("def hello(s)\n   return \"Hello \" + s\nend" );
    assertTrue(engine instanceof Invocable);
    Invocable invocableScript = (Invocable) engine;
    assertEquals("Hello petra", invocableScript.invokeFunction("hello", new Object[]{"petra"}));
  }
View Full Code Here

      objectName = functionName.substring(0, dotIdx);
      functionName = functionName.substring(dotIdx+1);
    }
    for(ScriptEngine engine : engines.values()) {
      if (engine instanceof Invocable) {
        Invocable invocable = (Invocable)engine;
        try {
          if (isMethod) {
            Object target = engine.get(objectName);
            if (target != null) {
              try {
                return invocable.invokeMethod(target, functionName, args);
              } catch (NoSuchMethodException e) {
                throw new ApiException("UnknownFunction", "Method \""+functionName+"\" not defined on object \""+objectName+"\"");
              }
            }
          } else {
View Full Code Here

        } catch (ScriptException e) {
          scriptManager.getErrorMessage(e, funcStr);
        }
      }
      if (scriptEngine instanceof Invocable) {
        final Invocable invocableEngine = (Invocable) scriptEngine;
        Object[] EMPTY = new Object[0];
        result = (String) invocableEngine.invokeFunction(func.getName(), EMPTY);
      }
    } catch (ScriptException e) {
      throw new OCommandScriptException("Error on execution of the script", func.getName(), e.getColumnNumber(), e);
    } catch (NoSuchMethodException e) {
      throw new OCommandScriptException("Error on execution of the script", func.getName(), 0, e);
View Full Code Here

   
  }
 
  private void invokeFunctionOrMethod(Shell shellState, ScriptEngine engine, CommandLine cl, Object[] args) {
    try {
      Invocable inv = (Invocable) engine;
      if (cl.hasOption(function.getOpt())) {
        inv.invokeFunction(cl.getOptionValue(function.getOpt()), args);
      } else if (cl.hasOption(object.getOpt())) {
        String objectMethod = cl.getOptionValue(object.getOpt());
        String[] parts = objectMethod.split(":");
        if (!(parts.length == 2)) {
          shellState.printException(new Exception("Object and Method must be supplied"));
          return;
        }
        String objectName = parts[0];
        String methodName = parts[1];
        Object obj = engine.get(objectName);
        inv.invokeMethod(obj, methodName, args);
       
      }
    } catch (Exception e) {
      shellState.printException(e);
    }
View Full Code Here

  public void testInvokeFunction() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.eval("function hello(s) { return 'Hello ' + s; }" );
    assertTrue(engine instanceof Invocable);
    Invocable invocableScript = (Invocable) engine;
    assertEquals("Hello petra", invocableScript.invokeFunction("hello", new Object[]{"petra"}));
  }
View Full Code Here

  public void testInvokeMethod() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.eval("function hello(s) { return 'Hello ' + s; }" );
    assertTrue(engine instanceof Invocable);
    Invocable invocableScript = (Invocable) engine;
   
    Object thiz = engine.eval("this;");
    assertEquals("Hello petra", invocableScript.invokeMethod(thiz, "hello", new Object[]{"petra"}));
  }
View Full Code Here

    public static void main(String[] args) throws Exception {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
        engine.eval("load('res/nashorn6.js')");

        Invocable invocable = (Invocable) engine;

        Product product = new Product();
        product.setName("Rubber");
        product.setPrice(1.99);
        product.setStock(1337);

        ScriptObjectMirror result = (ScriptObjectMirror)
                invocable.invokeFunction("calculate", product);
        System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
    }
View Full Code Here

    @Test public void testWrappedException2() throws Exception {
      ScriptEngineManager manager = new ScriptEngineManager(
          ScriptingContainer.class.getClassLoader());
      ScriptEngine engine = manager.getEngineByName("jruby");
      try {
        Invocable inv = (Invocable)engine;
        inv.invokeFunction("thisMethodDoesNotExist", new Object());
      } catch (ScriptException se) {
        Throwable t = se;
        while (t != null && t.getCause() != t) {
          System.err.println("Hierarchy of causes " + t.getMessage() + " " + t.getClass().getSimpleName());
          t = t.getCause();
View Full Code Here

        if (StringUtil.matchWeak(urlReq.getUrl(), regexp) == null)
          return false;
      }
      if (!StringUtil.isEmpty(code)) {
        engine.eval(code);
        Invocable jsInvoke = (Invocable) engine;
        if (jsInvoke
            .invokeFunction("ismatch",
                new Object[] { urlReq.getUrl(), html })
            .toString().trim().equals("0"))
          return false; // js 代码 返回1 是,0否
      }
View Full Code Here

TOP

Related Classes of javax.script.Invocable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.