[ https://issues.apache.org/jira/browse/GROOVY-7150?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15076376#comment-15076376 ]
ASF GitHub Bot commented on GROOVY-7150: ---------------------------------------- GitHub user jwagenleitner opened a pull request: https://github.com/apache/groovy/pull/230 GROOVY-7150 - Redirection of output in javax.script.ScriptEngine.invokeFunction() doesn't work Instead of adding `context` and `out` to the engine context this change provides access to those in the `groovy.lang.Binding` to the script class. This should allow a user to provide their own variables if they wish to override the behavior. The problem was caused because the `out` variable was being removed from the context (for cleanup reasons), so the cached `MethodClosure` no longer had access to it since it was executed in another method after the `eval` call. I also added another commit to ensure the factory returned by the engine is the same factory that created it. I couldn't see a reason why each engine instance should return a new factory. You can merge this pull request into a Git repository by running: $ git pull https://github.com/jwagenleitner/groovy GROOVY-7150 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/230.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #230 ---- commit b40cdf3f86b23b6ce4765676f41e73f01d79a78b Author: John Wagenleitner <jwagenleit...@apache.org> Date: 2016-01-01T20:25:37Z GROOVY-7150 - Redirection of output in javax.script.ScriptEngine.invokeFunction() doesn't work commit 3515a0f5384a4abfce6811911ca0aff01bfa80e3 Author: John Wagenleitner <jwagenleit...@apache.org> Date: 2016-01-01T21:15:03Z ScriptEngineFactory returned from ScriptEngine should be same factory that created engine ---- > Redirection of output in javax.script.ScriptEngine.invokeFunction() doesn't > work > -------------------------------------------------------------------------------- > > Key: GROOVY-7150 > URL: https://issues.apache.org/jira/browse/GROOVY-7150 > Project: Groovy > Issue Type: Bug > Components: groovy-jdk > Affects Versions: 2.3.6 > Environment: Java, Windows, Linux > Reporter: Shaul Eliyahu > > The Java code below should return a string "Hello World!". Instead of that it > returns an empty string and prints "Hello World!" in the console the > application runs. The problem exists only inside functions; texts printed > outside functions are redirected properly: > {code:java} > javax.script.ScriptEngineManager manager = new ScriptEngineManager(); > javax.script.ScriptEngine engine = manager.getEngineByName("groovy"); > StringWriter stdOut = new StringWriter(); > engine.getContext().setWriter(new PrintWriter(stdOut)); > engine.eval("def myFunction() { print("Hello World!"); }"); > Invocable invoker = (Invocable) engine; > invoker.invokeFunction("myFunction", new Object[0]); > return stdOut.getBuffer().toString(); > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)