Details
Description
Groovy Shell is giving the following exception when evaluating a simple script like "print 'hello'":
ERROR java.lang.ClassCastException: groovysh_evaluate cannot be cast to groovy.lang.GroovyObject at org.apache.groovy.groovysh.Interpreter.evaluate (Interpreter.groovy:72) at org.apache.groovy.groovysh.Evaluator$evaluate.call (Unknown Source) at org.apache.groovy.groovysh.Groovysh.execute (Groovysh.groovy:203) at org.apache.groovy.groovysh.Shell.leftShift (Shell.groovy:121) at org.apache.groovy.groovysh.Shell$leftShift$0.call (Unknown Source) at org.apache.groovy.groovysh.ShellRunner.work (ShellRunner.groovy:93) at org.apache.groovy.groovysh.InteractiveShellRunner.super$2$work (InteractiveShellRunner.groovy) at org.apache.groovy.groovysh.InteractiveShellRunner.work (InteractiveShellRunner.groovy:136) at org.apache.groovy.groovysh.ShellRunner.run (ShellRunner.groovy:57) at org.apache.groovy.groovysh.InteractiveShellRunner.super$2$run (InteractiveShellRunner.groovy) at org.apache.groovy.groovysh.InteractiveShellRunner.run (InteractiveShellRunner.groovy:95) at java_lang_Runnable$run.call (Unknown Source) at org.apache.groovy.groovysh.Groovysh.run (Groovysh.groovy:635) at org.apache.groovy.groovysh.Groovysh$run$0.callCurrent (Unknown Source) at org.apache.groovy.groovysh.Groovysh.run (Groovysh.groovy:600) at org.apache.groovy.groovysh.Groovysh$run.call (Unknown Source) at org.apache.groovy.groovysh.Main.startGroovysh (Main.groovy:190) at org.apache.groovy.groovysh.Main.main (Main.groovy:156)
I am starting Groovy Shell from within the GroovyStarter like this:
java -cp xxx/lib/groovy-4.0.0.jar;xxx/lib/groovy-groovysh-4.0.0.jar;xxx/lib/jline-2.14.6.jar -Dgroovy.home="${groovy_home}" -Djava.system.class.loader=groovy.lang.GroovyClassLoader org.codehaus.groovy.tools.GroovyStarter --classpath xxx/lib/guava-30.1.1.jar;xxx/lib/org.eclipse.jdt.annotation_2.2.400.v20191120-1313.jar;xxx/lib/groovy-4.0.0.jar;xxx/lib/groovy-test-4.0.0.jar;xxx/lib/ivy-2.5.0.jar;yyy/bin;yyy/src --main org.apache.groovy.groovysh.Main --define jline.terminal=jline.UnsupportedTerminal --parameters
I use GroovyStarter so that I can supply separate classpaths, one for the tool itself (groovysh, jline, etc.) and one for the scripts that are evaluated (groovy, etc.). And the groovy jar may be different if the project sources it through gradle or maven or whatever.
I think the problem I am encountering is that when the script is to be evaluated, it is not assignable to the groovy.lang.Script class in the ClassLoader of the tool and so it goes the fallback route in InvokerHelper#createScript. I made a change there to stop casting to GroovyObject, which probably fails for the same reason. However, the main method is void and so null is displayed for any evaluation.
Is there a way for InvokerHelper to run the generated script without using Script and Binding from its classpath? Or to detect that the script implements groovy.lang.Script and safely create it and execute run()?