Java Code Examples for javax.script.ScriptException#printStackTrace()

The following examples show how to use javax.script.ScriptException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TrustedScriptEngineTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test that we can use same script name in repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error" as name was derived from script name.
 */
public void noLoaderPerCompilerWithSameNameTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            e.put(ScriptEngine.FILENAME, "test.js");
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 2
Source File: TrustedScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error".
 */
public void noLoaderPerCompilerTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 3
Source File: TrustedScriptEngineTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error".
 */
public void noLoaderPerCompilerTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 4
Source File: TrustedScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test that we can use same script name in repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error" as name was derived from script name.
 */
public void noLoaderPerCompilerWithSameNameTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            e.put(ScriptEngine.FILENAME, "test.js");
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 5
Source File: DevCardTools.java    From metastone with GNU General Public License v2.0 6 votes vote down vote up
private static void prettyPrintFile(File file) throws IOException {
	Path path = Paths.get(file.getPath());
	String content = new String(Files.readAllBytes(path));

	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");

	scriptEngine.put("jsonString", content);
	try {
		scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, \"\t\")");
	} catch (ScriptException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	String prettyPrintedJson = (String) scriptEngine.get("result");
	Files.write(path, prettyPrintedJson.getBytes());
}
 
Example 6
Source File: ScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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());
    }
}
 
Example 7
Source File: TrustedScriptEngineTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error".
 */
public void noLoaderPerCompilerTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 8
Source File: TrustedScriptEngineTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * Test repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error".
 */
public void noLoaderPerCompilerTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
 
Example 9
Source File: TrustedScriptEngineTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void factoryClassLoaderTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final MyClassLoader loader = new MyClassLoader();
            // set the classloader as app class loader
            final ScriptEngine e = nfac.getScriptEngine(loader);
            try {
                e.eval("Packages.foo");
                // check that the class loader was attempted
                assertTrue(loader.reached(), "did not reach class loader!");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }

    fail("Cannot find nashorn factory!");
}
 
Example 10
Source File: ScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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());
    }
}
 
Example 11
Source File: ScriptEngineTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessGlobalTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        e.eval("var x = 'hello'");
        assertEquals(e.get("x"), "hello");
    } catch (final ScriptException exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
Example 12
Source File: MiaoshaServiceImpl.java    From goods-seckill with Apache License 2.0 5 votes vote down vote up
private static int calc(String verifyCode) {
	try {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		return (int) engine.eval(verifyCode);
	} catch (ScriptException e) {
		e.printStackTrace();
		return 0;
	}
}
 
Example 13
Source File: ScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessGlobalTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        e.eval("var x = 'hello'");
        assertEquals(e.get("x"), "hello");
    } catch (final ScriptException exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
Example 14
Source File: ScriptEngineTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessGlobalTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        e.eval("var x = 'hello'");
        assertEquals(e.get("x"), "hello");
    } catch (final ScriptException exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
Example 15
Source File: ArithmeticCaptchaAbstract.java    From EasyCaptcha with Apache License 2.0 5 votes vote down vote up
/**
 * 生成随机验证码
 *
 * @return 验证码字符数组
 */
@Override
protected char[] alphas() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < len; i++) {
        sb.append(num(10));
        if (i < len - 1) {
            int type = num(1, 4);
            if (type == 1) {
                sb.append("+");
            } else if (type == 2) {
                sb.append("-");
            } else if (type == 3) {
                sb.append("x");
            }
        }
    }
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {
        chars = String.valueOf(engine.eval(sb.toString().replaceAll("x", "*")));
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    sb.append("=?");
    arithmeticString = sb.toString();
    return chars.toCharArray();
}
 
Example 16
Source File: ScriptEngineFactory.java    From doov with Apache License 2.0 5 votes vote down vote up
public static ScriptEngine create() {
    ScriptEngineManager sem = new ScriptEngineManager();            // creation of an engine manager
    ScriptEngine engine = sem.getEngineByName(ENGINE_NAME);         // engine creation based on nashorn
    try {
        InputStream stream = ScriptEngineFactory.class.getResourceAsStream(MOMENT_JS_SRC);
        InputStreamReader momentJS = new InputStreamReader(stream);
        engine.eval(momentJS);                                      // evaluating moment.js
    } catch (ScriptException se) {
        se.printStackTrace();
    }
    return engine;
}
 
Example 17
Source File: ScopeTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@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 {
        Object obj1 = e.eval("Object");
        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);

        Object y1 = origCtxt.getAttribute("y");
        Object y2 = newCtxt.getAttribute("y");
        Assert.assertNotEquals(y1, y2);
        Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
        Assert.assertEquals("[object Object]", y1.toString());
        Assert.assertEquals("[object Object]", y2.toString());
    } catch (final ScriptException se) {
        se.printStackTrace();
        fail(se.getMessage());
    }
}
 
Example 18
Source File: ScopeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@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());
    }
}
 
Example 19
Source File: ScopeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@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());
    }
}
 
Example 20
Source File: ScopeTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@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 {
        Object obj1 = e.eval("Object");
        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);

        Object y1 = origCtxt.getAttribute("y");
        Object y2 = newCtxt.getAttribute("y");
        Assert.assertNotEquals(y1, y2);
        Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
        Assert.assertEquals("[object Object]", y1.toString());
        Assert.assertEquals("[object Object]", y2.toString());
    } catch (final ScriptException se) {
        se.printStackTrace();
        fail(se.getMessage());
    }
}