本文整理汇总了Java中javax.script.ScriptContext.getAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptContext.getAttribute方法的具体用法?Java ScriptContext.getAttribute怎么用?Java ScriptContext.getAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptContext
的用法示例。
在下文中一共展示了ScriptContext.getAttribute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyVars
import javax.script.ScriptContext; //导入方法依赖的package包/类
protected Map<String, Object> applyVars ( final ScriptContext context, final Map<String, Object> scriptObjects )
{
if ( scriptObjects == null || scriptObjects.isEmpty () )
{
return null;
}
final Map<String, Object> replaced = new HashMap<String, Object> ();
for ( final Map.Entry<String, Object> entry : scriptObjects.entrySet () )
{
final Object original = context.getAttribute ( entry.getKey (), ScriptContext.ENGINE_SCOPE );
replaced.put ( entry.getKey (), original );
context.setAttribute ( entry.getKey (), entry.getValue (), ScriptContext.ENGINE_SCOPE );
}
return replaced;
}
示例2: getScriptName
import javax.script.ScriptContext; //导入方法依赖的package包/类
private static String getScriptName(final ScriptContext ctxt) {
final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
return (val != null) ? val.toString() : "<eval>";
}
示例3: multiGlobalTest
import javax.script.ScriptContext; //导入方法依赖的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());
}
}
示例4: testScriptContext_NPE_IAE
import javax.script.ScriptContext; //导入方法依赖的package包/类
@Test
public void testScriptContext_NPE_IAE() throws Exception {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final ScriptContext c = e.getContext();
try {
c.getAttribute("");
throw new AssertionError("should have thrown IAE");
} catch (final IllegalArgumentException iae1) {}
try {
c.getAttribute(null);
throw new AssertionError("should have thrown NPE");
} catch (final NullPointerException npe1) {}
try {
c.getAttribute("", ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown IAE");
} catch (final IllegalArgumentException iae2) {}
try {
c.getAttribute(null, ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown NPE");
} catch (final NullPointerException npe2) {}
try {
c.removeAttribute("", ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown IAE");
} catch (final IllegalArgumentException iae3) {}
try {
c.removeAttribute(null, ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown NPE");
} catch (final NullPointerException npe3) {}
try {
c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown IAE");
} catch (final IllegalArgumentException iae4) {}
try {
c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE);
throw new AssertionError("should have thrown NPE");
} catch (final NullPointerException npe4) {}
try {
c.getAttributesScope("");
throw new AssertionError("should have thrown IAE");
} catch (final IllegalArgumentException iae5) {}
try {
c.getAttributesScope(null);
throw new AssertionError("should have thrown NPE");
} catch (final NullPointerException npe5) {}
}