Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Except perhaps for simple values, I would rather let the scripting engine coerce its values to Java types.</p> <pre><code>public class ScriptDemo { static class Result { private String[] words; public void setWords(String[] words) { this.words = words; } } static final String SCRIPT = "var foo = 'Hello World!';\n" + "result.setWords(foo.split(' '));"; public static void main(String[] args) throws ScriptException { Result result = new Result(); javax.script.ScriptEngineManager mgr = new ScriptEngineManager(); javax.script.ScriptEngine scripEngine = mgr .getEngineByExtension("js"); scripEngine.getContext().setAttribute("result", result, ScriptContext.ENGINE_SCOPE); scripEngine.eval(SCRIPT); System.out.println(Arrays.toString(result.words)); } } </code></pre> <p>Even if you can't edit the script, you could take the return value and pass it through your own generated script to do the coercion. This assumes you know something about the value being returned.</p> <hr> <p>EDIT: since nothing is known about the return value, I would first test it using Java (<a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#getClass()" rel="noreferrer">getClass()</a>) to see if it was one of the java.lang types. If the returned object is from some API private to the library, I would introspect it using the scripting language (in this case JavaScript), possibly coercing it to a Java type or pushing its properties into some Java data structure during the process.</p> <p>My JavaScript is rusty, but John Leach's tutorial looks quite good: <a href="http://www.syger.it/Tutorials/JavaScriptIntrospector.html" rel="noreferrer">JavaScript Introspection</a>.</p> <p>(You may be able to use Java reflection, but since the engine implementation could vary between Java versions/JREs/JavaScript engines, I wouldn't rely on it.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload