本文整理汇总了Java中jdk.nashorn.api.scripting.ScriptObjectMirror.setSlot方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptObjectMirror.setSlot方法的具体用法?Java ScriptObjectMirror.setSlot怎么用?Java ScriptObjectMirror.setSlot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.nashorn.api.scripting.ScriptObjectMirror
的用法示例。
在下文中一共展示了ScriptObjectMirror.setSlot方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jsobjectTest
import jdk.nashorn.api.scripting.ScriptObjectMirror; //导入方法依赖的package包/类
@Test
public void jsobjectTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }");
final ScriptObjectMirror obj = (ScriptObjectMirror) e.get("obj");
// try basic get on existing properties
if (!obj.getMember("bar").equals("hello")) {
fail("obj.bar != 'hello'");
}
if (!obj.getSlot(1).equals("world")) {
fail("obj[1] != 'world'");
}
if (!obj.callMember("func", new Object[0]).equals("hello")) {
fail("obj.func() != 'hello'");
}
// try setting properties
obj.setMember("bar", "new-bar");
obj.setSlot(1, "new-element-1");
if (!obj.getMember("bar").equals("new-bar")) {
fail("obj.bar != 'new-bar'");
}
if (!obj.getSlot(1).equals("new-element-1")) {
fail("obj[1] != 'new-element-1'");
}
// try adding properties
obj.setMember("prop", "prop-value");
obj.setSlot(12, "element-12");
if (!obj.getMember("prop").equals("prop-value")) {
fail("obj.prop != 'prop-value'");
}
if (!obj.getSlot(12).equals("element-12")) {
fail("obj[12] != 'element-12'");
}
// delete properties
obj.removeMember("prop");
if ("prop-value".equals(obj.getMember("prop"))) {
fail("obj.prop is not deleted!");
}
// Simple eval tests
assertEquals(obj.eval("typeof Object"), "function");
assertEquals(obj.eval("'nashorn'.substring(3)"), "horn");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}