当前位置: 首页>>代码示例>>Java>>正文


Java Invocable.getInterface方法代码示例

本文整理汇总了Java中javax.script.Invocable.getInterface方法的典型用法代码示例。如果您正苦于以下问题:Java Invocable.getInterface方法的具体用法?Java Invocable.getInterface怎么用?Java Invocable.getInterface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.script.Invocable的用法示例。


在下文中一共展示了Invocable.getInterface方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import javax.script.Invocable; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest4\n");
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine e  = Helper.getJsEngine(m);
    if (e == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    e.eval(new FileReader(
        new File(System.getProperty("test.src", "."), "Test4.js")));
    Invocable inv = (Invocable)e;
    Runnable run1 = (Runnable)inv.getInterface(Runnable.class);
    run1.run();
    // use methods of a specific script object
    Object intfObj = e.get("intfObj");
    Runnable run2 = (Runnable)inv.getInterface(intfObj, Runnable.class);
    run2.run();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:Test4.java

示例2: main

import javax.script.Invocable; //导入方法依赖的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 = "var obj = new Object(); obj.run = function() { print('run method called'); }";

    // evaluate script
    engine.eval(script);

    // get script object on which we want to implement the interface with
    final Object obj = engine.get("obj");

    final Invocable inv = (Invocable) engine;

    // get Runnable interface object from engine. This interface methods
    // are implemented by script methods of object 'obj'
    final Runnable r = inv.getInterface(obj, 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,代码行数:26,代码来源:RunnableImplObject.java

示例3: main

import javax.script.Invocable; //导入方法依赖的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: defaultMethodTest

import javax.script.Invocable; //导入方法依赖的package包/类
@Test
public void defaultMethodTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Invocable inv = (Invocable) e;

    final Object obj = e.eval("({ apply: function(arg) { return arg.toUpperCase(); }})");
    @SuppressWarnings("unchecked")
    final Function<String, String> func = inv.getInterface(obj, Function.class);
    assertEquals(func.apply("hello"), "HELLO");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:InvocableTest.java

示例5: checkMirrorToObject

import javax.script.Invocable; //导入方法依赖的package包/类
@Test
public void checkMirrorToObject() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine engine = engineManager.getEngineByName("nashorn");
    final Invocable invocable = (Invocable)engine;

    engine.eval("function test1(arg) { return { arg: arg }; }");
    engine.eval("function test2(arg) { return arg; }");
    engine.eval("function compare(arg1, arg2) { return arg1 == arg2; }");

    final Map<String, Object> map = new HashMap<>();
    map.put("option", true);

    final MirrorCheckExample example = invocable.getInterface(MirrorCheckExample.class);

    final Object value1 = invocable.invokeFunction("test1", map);
    final Object value2 = example.test1(map);
    final Object value3 = invocable.invokeFunction("test2", value2);
    final Object value4 = example.test2(value2);

    // check that Object type argument receives a ScriptObjectMirror
    // when ScriptObject is passed
    assertEquals(ScriptObjectMirror.class, value1.getClass());
    assertEquals(ScriptObjectMirror.class, value2.getClass());
    assertEquals(ScriptObjectMirror.class, value3.getClass());
    assertEquals(ScriptObjectMirror.class, value4.getClass());
    assertTrue((boolean)invocable.invokeFunction("compare", value1, value1));
    assertTrue(example.compare(value1, value1));
    assertTrue((boolean)invocable.invokeFunction("compare", value3, value4));
    assertTrue(example.compare(value3, value4));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:ScriptObjectMirrorTest.java

示例6: mirrorUnwrapInterfaceMethod

import javax.script.Invocable; //导入方法依赖的package包/类
@Test
public void mirrorUnwrapInterfaceMethod() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine engine = engineManager.getEngineByName("nashorn");
    final Invocable invocable = (Invocable)engine;
    engine.eval("function apply(obj) { " +
        " return obj instanceof Packages.jdk.nashorn.api.scripting.ScriptObjectMirror; " +
        "}");
    @SuppressWarnings("unchecked")
    final Function<Object,Object> func = invocable.getInterface(Function.class);
    assertFalse((boolean)func.apply(engine.eval("({ x: 2 })")));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:ScriptObjectMirrorTest.java


注:本文中的javax.script.Invocable.getInterface方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。