當前位置: 首頁>>代碼示例>>Java>>正文


Java ScriptEngine.setContext方法代碼示例

本文整理匯總了Java中javax.script.ScriptEngine.setContext方法的典型用法代碼示例。如果您正苦於以下問題:Java ScriptEngine.setContext方法的具體用法?Java ScriptEngine.setContext怎麽用?Java ScriptEngine.setContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.script.ScriptEngine的用法示例。


在下文中一共展示了ScriptEngine.setContext方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: invokeMethodDifferentContextTest

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
/**
 * Check that we can call invokeMethod on an object that we got by
 * evaluating script with different Context set.
 */
public void invokeMethodDifferentContextTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        // define an object with method on it
        final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");

        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        e.setContext(ctxt);

        // invoke 'func' on obj - but with current script context changed
        final Object res = ((Invocable) e).invokeMethod(obj, "hello");
        assertEquals(res, "Hello World!");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:InvocableTest.java

示例2: getInterfaceDifferentContext

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
/**
 * Check that we can get interface out of a script object even after
 * switching to use different ScriptContext.
 */
public void getInterfaceDifferentContext() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final Object obj = e.eval("({ run: function() { } })");

        // change script context
        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        e.setContext(ctxt);

        final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class);
        r.run();
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:InvocableTest.java

示例3: invokeFunctionDifferentContextTest

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
/**
 * Check that invokeFunction calls functions only from current context's
 * Bindings.
 */
public void invokeFunctionDifferentContextTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        // define an object with method on it
        e.eval("function hello() { return 'Hello World!'; }");
        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        // change engine's current context
        e.setContext(ctxt);

        ((Invocable) e).invokeFunction("hello"); // no 'hello' in new context!
        fail("should have thrown NoSuchMethodException");
    } catch (final Exception exp) {
        if (!(exp instanceof NoSuchMethodException)) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:InvocableTest.java

示例4: contextOverwriteTest

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
public static void contextOverwriteTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = new SimpleBindings();
    b.put("context", "hello");
    b.put("foo", 32);
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    e.setContext(newCtxt);
    assertEquals(e.eval("context"), "hello");
    assertEquals(((Number)e.eval("foo")).intValue(), 32);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:ScopeTest.java

示例5: engineOverwriteTest

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
public static void engineOverwriteTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = new SimpleBindings();
    b.put("engine", "hello");
    b.put("foo", 32);
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
    e.setContext(newCtxt);
    assertEquals(e.eval("engine"), "hello");
    assertEquals(((Number)e.eval("foo")).intValue(), 32);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:ScopeTest.java

示例6: invokeFunctionInGlobalScopeTest2

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
public void invokeFunctionInGlobalScopeTest2() throws Exception {
     final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

     // create a new ScriptContext instance
     final ScriptContext ctxt = new SimpleScriptContext();
     // set it as 'default' ScriptContext
     engine.setContext(ctxt);

     // create a new Bindings and set as ENGINE_SCOPE
     ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);

     // define a function called "func"
     engine.eval("func = function() { return 42 }");

     // move ENGINE_SCOPE Bindings to GLOBAL_SCOPE
     ctxt.setBindings(ctxt.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.GLOBAL_SCOPE);

     // create a new Bindings and set as ENGINE_SCOPE
     ctxt.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);

     // define new function that calls "func" now in GLOBAL_SCOPE
     engine.eval("newfunc = function() { return func() }");

     // call "newfunc" and check the return value
     final Object value = ((Invocable)engine).invokeFunction("newfunc");
     assertTrue(((Number)value).intValue() == 42);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:ScopeTest.java

示例7: multiGlobalTest

import javax.script.ScriptEngine; //導入方法依賴的package包/類
@Test
public void multiGlobalTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    try {
        final Object obj1 = e.eval("Object");
        final Object obj2 = e.eval("Object", newCtxt);
        Assert.assertNotEquals(obj1, obj2);
        Assert.assertNotNull(obj1);
        Assert.assertNotNull(obj2);
        Assert.assertEquals(obj1.toString(), obj2.toString());

        e.eval("x = 'hello'");
        e.eval("x = 'world'", newCtxt);
        Object x1 = e.getContext().getAttribute("x");
        Object x2 = newCtxt.getAttribute("x");
        Assert.assertNotEquals(x1, x2);
        Assert.assertEquals(x1, "hello");
        Assert.assertEquals(x2, "world");

        x1 = e.eval("x");
        x2 = e.eval("x", newCtxt);
        Assert.assertNotEquals(x1, x2);
        Assert.assertEquals(x1, "hello");
        Assert.assertEquals(x2, "world");

        final ScriptContext origCtxt = e.getContext();
        e.setContext(newCtxt);
        e.eval("y = new Object()");
        e.eval("y = new Object()", origCtxt);

        final Object y1 = origCtxt.getAttribute("y");
        final Object y2 = newCtxt.getAttribute("y");
        Assert.assertNotEquals(y1, y2);
        final Object yeval1 = e.eval("y");
        final Object yeval2 = e.eval("y", origCtxt);
        Assert.assertNotEquals(yeval1, yeval2);
        Assert.assertEquals("[object Object]", y1.toString());
        Assert.assertEquals("[object Object]", y2.toString());
    } catch (final ScriptException se) {
        se.printStackTrace();
        fail(se.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:49,代碼來源:ScopeTest.java


注:本文中的javax.script.ScriptEngine.setContext方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。