groovy
  1. groovy
  2. GROOVY-3707

I wish i could parse a class using eval function of JSR223

    Details

    • Type: Wish Wish
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 1.6.4
    • Fix Version/s: None
    • Component/s: JSR / TCK / GLS
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      Verbose version of the test passes, but it is a little ugly.

          public static final String FOO_CLASS = "class Foo { int getResult() { return 1; } } ; Foo.class";
          public static final String FOO = "class Foo { int getResult() { return 1; } }";
      
          public void testClassEvalVerbose() throws ScriptException {
              ScriptEngineManager factory = new ScriptEngineManager();
              ScriptEngine engine = factory.getEngineByName("groovy");
              assertEquals("Foo", ((Class) engine.eval(FOO_CLASS)).getCanonicalName());
          }
      
          public void testClassEvalBrief() throws ScriptException {
              ScriptEngineManager factory = new ScriptEngineManager();
              ScriptEngine engine = factory.getEngineByName("groovy");
              assertEquals("Foo", ((Class) engine.eval(FOO)).getCanonicalName());
          }
      

      Brief version throws:
      javax.script.ScriptException: javax.script.ScriptException: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: Foo.main() is applicable for argument types: ([Ljava.lang.String values: [[]]
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:119)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
      at com.jtstand.TestStepScriptTest.testClassEvalBrief(TestStepScriptTest.java:42)
      Caused by: javax.script.ScriptException: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: Foo.main() is applicable for argument types: ([Ljava.lang.String values: [[]]
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:307)
      at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:113)
      ... 28 more
      Caused by: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: Foo.main() is applicable for argument types: ([Ljava.lang.String values: [[]]

      Why?
      Is this what JSR223 specifies to do?
      To sad if yes!

        Issue Links

          Activity

          Hide
          blackdrag blackdrag added a comment -

          Would I say BAR is not a script, just classes? yes.
          What if the Class is a Thread but it also has a static void main(String[] args)? It would execute the main method.

          What if the class you write extends Script? in Current Groovy the class would be executed through its run method. In your version it should return the class too. The problem now is, that at that point we don't know the difference between a written class and a normal script anymore.

          Show
          blackdrag blackdrag added a comment - Would I say BAR is not a script, just classes? yes. What if the Class is a Thread but it also has a static void main(String[] args)? It would execute the main method. What if the class you write extends Script? in Current Groovy the class would be executed through its run method. In your version it should return the class too. The problem now is, that at that point we don't know the difference between a written class and a normal script anymore.
          Hide
          Albert Kurucz added a comment -

          Yes, this is how Groovy Script is designed.
          I think JSR223 should specify the ScriptEngine's behavior to either yours or to mine.
          You agree?

          Show
          Albert Kurucz added a comment - Yes, this is how Groovy Script is designed. I think JSR223 should specify the ScriptEngine's behavior to either yours or to mine. You agree?
          Hide
          blackdrag blackdrag added a comment -

          I can't agree, since letting the class being returned means the language must have a concept of a class. Anything we specify here would be specific to Groovy and only Groovy.

          Show
          blackdrag blackdrag added a comment - I can't agree, since letting the class being returned means the language must have a concept of a class. Anything we specify here would be specific to Groovy and only Groovy.
          Hide
          Albert Kurucz added a comment -

          I agree that part: JSR223 should not allow those Engines to return a class which has no such concept as Class.
          But how about the others?

          Show
          Albert Kurucz added a comment - I agree that part: JSR223 should not allow those Engines to return a class which has no such concept as Class. But how about the others?
          Hide
          blackdrag blackdrag added a comment -

          You got me wrong. I said the JSR223 spec cannot require returning a class, because there might be no concept of a class. Therefore it is a language or script engine decision. If you try to execute your BAR script using the groovy command on the command line, then it won't work too. Doing it different for the script engine would mean to have it different than GroovyShell and other places.

          Show
          blackdrag blackdrag added a comment - You got me wrong. I said the JSR223 spec cannot require returning a class, because there might be no concept of a class. Therefore it is a language or script engine decision. If you try to execute your BAR script using the groovy command on the command line, then it won't work too. Doing it different for the script engine would mean to have it different than GroovyShell and other places.

            People

            • Assignee:
              Guillaume Laforge
              Reporter:
              Albert Kurucz
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: