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


Java ScriptEngineManager.getEngineByName方法代碼示例

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


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

示例1: windowLocationTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
public void windowLocationTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Window window = new Window();

    try {
        e.put("window", window);
        e.eval("print(window.location)");
        final Object locationValue = e.eval("window.getLocation()");
        assertEquals(locationValue, "http://localhost:8080/window");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:ScriptEngineTest.java

示例2: windowEventTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
public void windowEventTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Window window = new Window();

    try {
        e.put("window", window);
        e.eval("window.onload = function() { print('window load event fired'); return true }");
        assertTrue((Boolean)e.eval("window.onload.loaded()"));
        final WindowEventHandler handler = window.getOnload();
        assertNotNull(handler);
        assertTrue(handler.loaded());
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ScriptEngineTest.java

示例3: main

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
public static void main(final String[] args) throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName("nashorn");

    // JavaScript code in a String
    final String script = "function run() { print('run called'); }";

    // evaluate script
    engine.eval(script);

    final Invocable inv = (Invocable) engine;

    // get Runnable interface object from engine. This interface methods
    // are implemented by script functions with the matching name.
    final Runnable r = inv.getInterface(Runnable.class);

    // start a new thread that runs the script implemented
    // runnable interface
    final Thread th = new Thread(r);
    th.start();
    th.join();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:RunnableImpl.java

示例4: getInterfaceNonScriptObjectThizTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
/**
 * Check that getInterface on non-script object 'thiz' results in
 * IllegalArgumentException.
 */
public void getInterfaceNonScriptObjectThizTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        ((Invocable) e).getInterface(new Object(), Runnable.class);
        fail("should have thrown IllegalArgumentException");
    } catch (final Exception exp) {
        if (!(exp instanceof IllegalArgumentException)) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:InvocableTest.java

示例5: invokeMethodNonScriptObjectThizTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
/**
 * Check that calling method on non-script object 'thiz' results in
 * IllegalArgumentException.
 */
public void invokeMethodNonScriptObjectThizTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        ((Invocable) e).invokeMethod(new Object(), "toString");
        fail("should have thrown IllegalArgumentException");
    } catch (final Exception exp) {
        if (!(exp instanceof IllegalArgumentException)) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:InvocableTest.java

示例6: getInterfaceDifferentContext

import javax.script.ScriptEngineManager; //導入方法依賴的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

示例7: testJS

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
    public void testJS() {
        // https://stackoverflow.com/questions/22492641/java8-js-nashorn-convert-array-to-java-array
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("javascript");
        try {
            engine.put("line", "劉長炯 微信號weblogic (10.3.2) [46a5432f8fdea99a6186a927e8da5db7a51854ac]");
//            engine.put("regex", )
            String regex = "/(.*?) \\((.*?)\\) \\[(.*?)\\]/";
            String[] value = (String[])engine.eval("Java.to(line.match(" + regex + "),\"java.lang.String[]\" );");
            System.out.println(value.length);
            System.out.println(value[1]);
            String[] result = {"劉長炯 微信號weblogic (10.3.2) [46a5432f8fdea99a6186a927e8da5db7a51854ac]",
                    "劉長炯 微信號weblogic", "10.3.2", "46a5432f8fdea99a6186a927e8da5db7a51854ac"};
            Assert.assertArrayEquals("result shold match", result, value);
//            Collection<Object> val = value.values();
//            if(value.isArray()) {
//                System.out.println(value.getMember("1"));
//            }
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
 
開發者ID:beansoftapp,項目名稱:react-native-console,代碼行數:24,代碼來源:RNPathUtilTest.java

示例8: testScriptEngine

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
public static void testScriptEngine() throws Exception {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine js = sem.getEngineByName("JavaScript");
    js.eval("blubb = 1\n" +
            "muh = blubb + 2\n" +
            "// just a comment...\n" +
            "crap = \"asd\"");
    System.out.println(js.get("muh").getClass());

    System.out.println("Testing ScriptEngine Performance...");
    long start = System.currentTimeMillis();
    Config c = Config.getInstance(new FileInputStream("res/customConfig.js"));
    double[] d = new double[]{1.0, 7.5, 1.3+2.5+6.3+3.1, 6.0};;
    for (int i = 0; i < 100000; i++) {
        //c.pReassignVnf(i);
        //d = new double[]{d[0] + 1.0, d[1] - 0.5, d[0] + d[1], d[3] * 1.1};
        c.objectiveVector(new double[Solution.Vals.values().length]);
    }
    System.out.println(Arrays.toString(d));
    long dur = System.currentTimeMillis() - start;
    System.out.println("Duration: " + dur + "ms");
    System.out.println(c.topologyFile.toAbsolutePath().toString());
}
 
開發者ID:lsinfo3,項目名稱:mo-vnfcp,代碼行數:24,代碼來源:Main.java

示例9: windowAlertTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
public void windowAlertTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Window window = new Window();

    try {
        e.put("window", window);
        e.eval("print(window.alert)");
        e.eval("window.alert('calling window.alert...')");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:ScriptEngineTest.java

示例10: putGlobalFunctionTest

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

    e.put("callable", new Callable<String>() {
        @Override
        public String call() throws Exception {
            return "callable was called";
        }
    });

    try {
        e.eval("print(callable.call())");
    } catch (final ScriptException exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:ScriptEngineTest.java

示例11: engineOverwriteInScriptTest

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
@Test
public static void engineOverwriteInScriptTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    e.put("foo", 32);

    assertEquals(((Number)e.eval("foo")).intValue(), 32);
    assertEquals(e.eval("engine = 'bar'"), "bar");
    assertEquals(((Number)e.eval("foo")).intValue(), 32);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:ScopeTest.java

示例12: buildInvocable

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
/**
 * Builds the script invocable
 */
private void buildInvocable() {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    try {
        if (hasScript()) {
            engine.eval(script.getValue());
            inv = (Invocable) engine;
        }
    } catch (ScriptException e) {
        ExceptionHandler.get().handle(e);
    }
}
 
開發者ID:VISNode,項目名稱:VISNode,代碼行數:16,代碼來源:ScriptRunner.java

示例13: main

import javax.script.ScriptEngineManager; //導入方法依賴的package包/類
public static void main(String args[]) {

		ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
		ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

		try {
			nashorn.eval("var aaa = 'www'; print(aaa);");
			Integer result = (Integer) nashorn.eval("10 + 2");
			System.out.println(result.toString());
		} catch (ScriptException e) {
			System.out.println("執行腳本錯誤: " + e.getMessage());
		}

		
	}
 
開發者ID:MinsxCloud,項目名稱:minsx-java-example,代碼行數:16,代碼來源:JavaScriptA.java

示例14: engineOverwriteTest

import javax.script.ScriptEngineManager; //導入方法依賴的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

示例15: exposeGlobalTest

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

    try {
        e.put("y", "foo");
        e.eval("print(y)");
    } catch (final ScriptException exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:ScriptEngineTest.java


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