You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/05/24 00:59:00 UTC

[jira] [Created] (GROOVY-10105) Groovy Shell having trouble evaluating script

Eric Milles created GROOVY-10105:
------------------------------------

             Summary: Groovy Shell having trouble evaluating script
                 Key: GROOVY-10105
                 URL: https://issues.apache.org/jira/browse/GROOVY-10105
             Project: Groovy
          Issue Type: Bug
          Components: Groovysh
    Affects Versions: 4.0.0-beta-1, 3.0.8, 2.5.14
            Reporter: Eric Milles


Groovy Shell is giving the following exception when evaluating a simple script like "print 'hello'":
{code:java}
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)
{code}
I am starting Groovy Shell from within the {{GroovyStarter}} like this:
{code:java}
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
{code}
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}}|https://github.com/apache/groovy/commit/7a56d7130aaf7b5244fa5a82fa0505fb7509f51f], 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()}}?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)