本文整理汇总了Java中javax.script.ScriptException类的典型用法代码示例。如果您正苦于以下问题:Java ScriptException类的具体用法?Java ScriptException怎么用?Java ScriptException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ScriptException类属于javax.script包,在下文中一共展示了ScriptException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: timeOut
import javax.script.ScriptException; //导入依赖的package包/类
public void timeOut(final long delay, final EventInstanceManager eim) {
if (disposed || eim == null) {
return;
}
eventTimer = EventTimer.getInstance().schedule(new Runnable() {
@Override
public void run() {
if (disposed || eim == null || em == null) {
return;
}
try {
em.getIv().invokeFunction("scheduledTimeout", eim);
} catch (NoSuchMethodException | ScriptException ex) {
System.out.println("Event name" + em.getName() + ", Instance name : " + name + ", method Name : scheduledTimeout:\n" + ex);
}
}
}, delay);
}
示例2: lexicalScopeTest
import javax.script.ScriptException; //导入依赖的package包/类
/**
* Make sure lexically defined variables are accessible in other scripts.
*/
@Test
public void lexicalScopeTest() throws ScriptException {
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
final ScriptEngine e = factory.getScriptEngine(LANGUAGE_ES6);
e.eval("let x; const y = 'world';");
assertEquals(e.eval("x = 'hello'"), "hello");
assertEquals(e.eval("typeof x"), "string");
assertEquals(e.eval("typeof y"), "string");
assertEquals(e.eval("x"), "hello");
assertEquals(e.eval("y"), "world");
assertEquals(e.eval("typeof this.x"), "undefined");
assertEquals(e.eval("typeof this.y"), "undefined");
assertEquals(e.eval("this.x"), null);
assertEquals(e.eval("this.y"), null);
}
示例3: testNonWrapping
import javax.script.ScriptException; //导入依赖的package包/类
/**
* Ensure that the old behaviour (every object is a Map) is unchanged.
*/
@Test
public void testNonWrapping() throws ScriptException {
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
final Object val = engine.eval("({x: [1, {y: [2, {z: [3]}]}, [4, 5]]})");
final Map<String, Object> root = asMap(val);
final Map<String, Object> x = asMap(root.get("x"));
assertEquals(x.get("0"), 1);
final Map<String, Object> x1 = asMap(x.get("1"));
final Map<String, Object> y = asMap(x1.get("y"));
assertEquals(y.get("0"), 2);
final Map<String, Object> y1 = asMap(y.get("1"));
final Map<String, Object> z = asMap(y1.get("z"));
assertEquals(z.get("0"), 3);
final Map<String, Object> x2 = asMap(x.get("2"));
assertEquals(x2.get("0"), 4);
assertEquals(x2.get("1"), 5);
}
示例4: classFilterTest2
import javax.script.ScriptException; //导入依赖的package包/类
@Test
public void classFilterTest2() throws ScriptException {
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final ScriptEngine e = fac.getScriptEngine(new String[0], Thread.currentThread().getContextClassLoader(),
new ClassFilter() {
@Override
public boolean exposeToScripts(final String fullName) {
// don't allow anything that is not "java."
return fullName.startsWith("java.");
}
});
assertEquals(e.eval("typeof javax.script.ScriptEngine"), "object");
assertEquals(e.eval("typeof java.util.Vector"), "function");
try {
e.eval("Java.type('javax.script.ScriptContext')");
fail("should not reach here");
} catch (final ScriptException | RuntimeException se) {
if (! (se.getCause() instanceof ClassNotFoundException)) {
fail("ClassNotFoundException expected");
}
}
}
示例5: globalPerEngineTest
import javax.script.ScriptException; //导入依赖的package包/类
@Test
public void globalPerEngineTest() throws ScriptException {
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final String[] options = new String[] { "--global-per-engine" };
final ScriptEngine e = fac.getScriptEngine(options);
e.eval("function foo() {}");
final ScriptContext newCtx = new SimpleScriptContext();
newCtx.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
// all global definitions shared and so 'foo' should be
// visible in new Bindings as well.
assertTrue(e.eval("typeof foo", newCtx).equals("function"));
e.eval("function bar() {}", newCtx);
// bar should be visible in default context
assertTrue(e.eval("typeof bar").equals("function"));
}
示例6: action
import javax.script.ScriptException; //导入依赖的package包/类
public final void action(final MapleClient c, final byte mode, final byte type, final int selection) {
if (mode != -1) {
final NPCConversationManager cm = cms.get(c);
if (cm == null || cm.getLastMsg() > -1) {
return;
}
final Lock lock = c.getNPCLock();
lock.lock();
try {
if (cm.pendingDisposal) {
dispose(c);
} else {
c.setClickedNPC();
cm.getIv().invokeFunction("action", mode, type, selection);
}
} catch (final ScriptException | NoSuchMethodException e) {
System.err.println("Error executing NPC script. NPC ID : " + cm.getNpc() + ":" + e);
dispose(c);
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
示例7: variableArityInterfaceTest
import javax.script.ScriptException; //导入依赖的package包/类
@Test
/**
* Tests whether invocation of a JavaScript method through a variable arity
* Java method will pass the vararg array. Both non-vararg and vararg
* JavaScript methods are tested.
*
* @throws ScriptException
*/
public void variableArityInterfaceTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.eval(
"function test1(i, strings) {"
+ " return 'i == ' + i + ', strings instanceof java.lang.String[] == ' + (strings instanceof Java.type('java.lang.String[]')) + ', strings == ' + java.util.Arrays.toString(strings)"
+ "}"
+ "function test2() {"
+ " return 'arguments[0] == ' + arguments[0] + ', arguments[1] instanceof java.lang.String[] == ' + (arguments[1] instanceof Java.type('java.lang.String[]')) + ', arguments[1] == ' + java.util.Arrays.toString(arguments[1])"
+ "}");
final VariableArityTestInterface itf = ((Invocable) e).getInterface(VariableArityTestInterface.class);
Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]");
Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]");
}
示例8: initJS
import javax.script.ScriptException; //导入依赖的package包/类
/**
* Setup javascript shortcuts such as 'server'.
*/
private void initJS() {
// Allow JS to load java classes.
Thread currentThread = Thread.currentThread();
ClassLoader previousClassLoader = currentThread.getContextClassLoader();
currentThread.setContextClassLoader(Core.getInstance().getClass().getClassLoader());
try {
this.engine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.put("engine", engine); // Allow JS to do consistent eval.
engine.eval(new InputStreamReader(Core.getInstance().getResource("boot.js"))); // Run JS startup.
MechanicManager.getMechanics().forEach(this::bindObject); // Create shortcuts for all mechanics.
bindClass(Utils.class);
bindClass(KCPlayer.class);
bindClass(ReflectionUtil.class);
} catch (ScriptException ex) {
ex.printStackTrace();
Core.warn("Failed to initialize JS shortcuts.");
} finally {
// Set back the previous class loader.
currentThread.setContextClassLoader(previousClassLoader);
}
}
示例9: getIntegerVector
import javax.script.ScriptException; //导入依赖的package包/类
@Override
public int[] getIntegerVector(final String variable) {
if (isNull(variable)) {
return null;
} else {
try {
final Vector sexp = (Vector) engine.unwrap().eval(variable);
final int[] array = new int[sexp.length()];
for (int i = 0; i < array.length; i++) {
array[i] = sexp.getElementAsInt(i);
}
return array;
} catch (final ScriptException e) {
throw new RuntimeException(e);
}
}
}
示例10: getBooleanMatrix
import javax.script.ScriptException; //导入依赖的package包/类
@Override
public boolean[][] getBooleanMatrix(final String variable) {
if (isNull(variable)) {
return null;
} else {
try {
final Matrix sexp = new Matrix((Vector) engine.unwrap().eval(variable));
final boolean[][] matrix = new boolean[sexp.getNumRows()][];
for (int row = 0; row < matrix.length; row++) {
final boolean[] vector = new boolean[sexp.getNumCols()];
for (int col = 0; col < vector.length; col++) {
vector[col] = sexp.getElementAsInt(row, col) > 0;
}
matrix[row] = vector;
}
return matrix;
} catch (final ScriptException e) {
throw new RuntimeException(e);
}
}
}
示例11: setJsSentence
import javax.script.ScriptException; //导入依赖的package包/类
protected void setJsSentence(INlpSentence s) throws CoreCriticalException {
try {
jsEngine.eval("var sentence = [];");
if ((Boolean) jsEngine.eval(String.format("sentence.positionInSentence == %d", s.getSentenceIndexInCorpus())))
return;
jsEngine.eval(String.format("sentence.positionInSentence = %d", s.getSentenceIndexInCorpus()));
for (int i = 0; i< s.getTokenCount(); i++) {
Token token = s.getToken(i);
jsEngine.eval(String.format("sentence[%d] = %s;", token.getTokenIndexInSentence(), tokenToJson(token)));
}
jsEngine.eval("sentence.spanAnnotations = []");
for (SpanAnnotation annotation : s.getSpanAnnotations()) {
jsEngine.eval(String.format("sentence.spanAnnotations.push(%s);", annotationToJson(annotation)));
for (int i = annotation.getStartTokenIndex(); i <= annotation.getEndTokenIndex(); i++) {
jsEngine.eval(String.format("sentence[%s].parentAnnotations.push(sentence.spanAnnotations[sentence.spanAnnotations.length - 1]);", i));
}
}
} catch (ScriptException e) {
throw new CoreCriticalException(e);
}
}
示例12: evalReader
import javax.script.ScriptException; //导入依赖的package包/类
private Object evalReader(Reader in, String filename) {
try {
engine.put(ScriptEngine.FILENAME, filename);
return engine.eval(in);
} catch (ScriptException sexp) {
System.err.println(sexp);
printError(sexp.toString(), sexp);
} finally {
try {
in.close();
} catch (IOException ioe) {
printError(ioe.toString(), ioe);
}
}
return null;
}
示例13: schedule
import javax.script.ScriptException; //导入依赖的package包/类
public final void schedule(final String methodName, final long delay) {
if (disposed) {
return;
}
EventTimer.getInstance().schedule(new Runnable() {
@Override
public void run() {
if (disposed || EventInstanceManager.this == null || em == null) {
return;
}
try {
em.getIv().invokeFunction(methodName, EventInstanceManager.this);
} catch (NullPointerException npe) {
} catch (NoSuchMethodException | ScriptException ex) {
System.out.println("Event name" + em.getName() + ", Instance name : " + name + ", method Name : " + methodName + ":\n" + ex);
}
}
}, delay);
}
示例14: scriptObjectAutoConversionTest
import javax.script.ScriptException; //导入依赖的package包/类
@Test
public void scriptObjectAutoConversionTest() throws ScriptException {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.eval("obj = { foo: 'hello' }");
e.put("Window", e.eval("Packages.jdk.nashorn.api.scripting.test.Window"));
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello");
assertEquals(e.eval("Window.funcScriptObjectMirror(obj)"), "hello");
assertEquals(e.eval("Window.funcMap(obj)"), "hello");
assertEquals(e.eval("Window.funcJSObject(obj)"), "hello");
}
示例15: accessStaticFieldString
import javax.script.ScriptException; //导入依赖的package包/类
@Test
public void accessStaticFieldString() throws ScriptException {
e.eval("var ps_string = SharedObject.publicStaticString;");
assertEquals(SharedObject.publicStaticString, e.get("ps_string"));
assertEquals("string", e.eval("typeof ps_string;"));
e.eval("SharedObject.publicStaticString = 'changedString';");
assertEquals("changedString", SharedObject.publicStaticString);
}