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


Java ScriptContext.getAttribute方法代碼示例

本文整理匯總了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;
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:17,代碼來源:ScriptExecutor.java

示例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>";
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:5,代碼來源:NashornScriptEngine.java

示例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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:49,代碼來源:ScopeTest.java

示例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) {}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:56,代碼來源:ScopeTest.java


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