Package javax.script

Examples of javax.script.Invocable


    System.out.println(msg);
    System.out.println(name + ":" + hb);

    // 2
    engine.eval("function add (a, b) {c = a + b; return c; }");
    Invocable jsInvoke = (Invocable) engine;

    Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 });
    System.out.println(result1);

    // 3
    Adder adder = jsInvoke.getInterface(Adder.class);
    int result2 = adder.add(10, 35);
    System.out.println(result2);

    // 4
    engine.eval("function run() {print('www.java2s.com');}");
    Invocable invokeEngine = (Invocable) engine;
    Runnable runner = invokeEngine.getInterface(Runnable.class);
    Thread t = new Thread(runner);
    t.start();
    t.join();

    // 5
View Full Code Here


    }

    public void testInvoke() throws ScriptException, NoSuchMethodException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine pythonEngine = manager.getEngineByName("python");
        Invocable invocableEngine = (Invocable) pythonEngine;

        assertNull(pythonEngine.eval("def f(x): return abs(x)"));
        assertEquals(Integer.valueOf(5), invocableEngine.invokeFunction("f", Integer.valueOf(-5)));
        assertEquals("spam", invocableEngine.invokeMethod(new PyString("  spam  "), "strip"));
        assertEquals("spam", invocableEngine.invokeMethod("  spam  ", "strip"));
    }
View Full Code Here

        assertEquals("spam", invocableEngine.invokeMethod("  spam  ", "strip"));
    }

    public void testInvokeFunctionNoSuchMethod() throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        Invocable invocableEngine = (Invocable) manager.getEngineByName("python");

        try {
            invocableEngine.invokeFunction("undefined");
        } catch (NoSuchMethodException e) {
            return;
        }
        assertTrue("Expected a NoSuchMethodException", false);
    }
View Full Code Here

        assertTrue("Expected a NoSuchMethodException", false);
    }

    public void testInvokeMethodNoSuchMethod() throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        Invocable invocableEngine = (Invocable) manager.getEngineByName("python");

        try {
            invocableEngine.invokeMethod("eggs", "undefined");
            fail("Expected a NoSuchMethodException");
        } catch (NoSuchMethodException e) {
            assertEquals("undefined", e.getMessage());
        }
    }
View Full Code Here

    }

    public void testGetInterface() throws ScriptException, IOException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine pythonEngine = manager.getEngineByName("python");
        Invocable invocableEngine = (Invocable) pythonEngine;

        assertNull(pythonEngine.eval("def read(cb): return 1"));
        Readable readable = invocableEngine.getInterface(Readable.class);
        assertEquals(1, readable.read(null));

        assertNull(pythonEngine.eval(
                "class C(object):\n"
                + "    def read(self, cb): return 2\n"
                + "c = C()"));
        readable = invocableEngine.getInterface(pythonEngine.get("c"), Readable.class);
        assertEquals(2, readable.read(null));
    }
View Full Code Here

    }

    public void testInvokeMethodNoSuchArgs() throws ScriptException, NoSuchMethodException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine pythonEngine = manager.getEngineByName("python");
        Invocable invocableEngine = (Invocable) pythonEngine;

        Object newStringCapitalize = invocableEngine.invokeMethod("test", "capitalize");
        assertEquals(newStringCapitalize, "Test");
    }
View Full Code Here

   }

   public void testGetInterfaceCharSequence2() throws ScriptException, IOException {
           ScriptEngineManager manager = new ScriptEngineManager();
           ScriptEngine pythonEngine = manager.getEngineByName("python");
           Invocable invocableEngine = (Invocable) pythonEngine;

           assertNull(pythonEngine.eval(
                   "from java.lang import StringBuilder\r\n" +
                   "c = StringBuilder(\"abc\")\r\n"));
           CharSequence seq = invocableEngine.getInterface(pythonEngine.get("c"), CharSequence.class);
           assertEquals("abc", seq.toString());
   }
View Full Code Here

    }

   public void testGetInterfaceCharSequence1() throws ScriptException, IOException {
           ScriptEngineManager manager = new ScriptEngineManager();
           ScriptEngine engine = manager.getEngineByName("python");
           Invocable invocableEngine = (Invocable) engine;

           assertNull(engine.eval(
                   "from java.lang import CharSequence\n" +
                   "class MyString(CharSequence):\n" +
                   "   def length(self): return 3\n" +
                   "   def charAt(self, index): return 'a'\n" +
                   "   def subSequence(self, start, end): return \"\"\n" +
                   "   def toString(self): return \"aaa\"\n" +
                   "c = MyString()"));
           CharSequence seq = invocableEngine.getInterface(engine.get("c"), CharSequence.class);
           assertEquals("aaa", seq.toString());
   }
View Full Code Here

        // tag::jsr223_invocable[]
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");
        String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }";
        engine.eval(fact);
        Invocable inv = (Invocable) engine;
        Object[] params = {5};
        Object result = inv.invokeFunction("factorial", params);
        assertEquals(new Integer(120), result);
        // end::jsr223_invocable[]
    }
View Full Code Here

        this.baseFolder = ".";
    }

    public EntryTransformer getEntryTransformer(String scriptName) {
        String content = load("entry", scriptName);
        Invocable invocable = (Invocable) engine;
        try {
            engine.eval(content);
        } catch (ScriptException ex) {
            throw new IllegalStateException("Cannot evaluate script", ex);
        }
        return invocable.getInterface(EntryTransformer.class);
    }
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.