Details
-
Type:
Wish
-
Status:
Closed
-
Priority:
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 :
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
- is duplicated by
-
GROOVY-3816
Should be possible to declare a class in a script and use it in another script with JSR223
-
I think it says to execute the script whatever this means. The main method is called from within Groovy code. InvokerHelper#createScript is responsible for it in the end. But here basically is a problem. Should a class be executed? or should the class be returned? A script is a class to. It behaves a bit different, but it is in fact a class, one with a run method, that will be called directly. The current code does anything that is no script in a script, that executes a main method of the wrapped class. Well, maybe wrapper is so good not, proxy is probably better.
What for example if your class is a junit test. Do you expect it to be executed, or the class being returned? Executing it means to run the test, returning the class means not executing it. You probably say the script returns a class, but there is no script, just the class. The script is a short form of creating a certain kind of class. So for Groovy the basic concept is the class, not the script. From that point of view it behaves absolutely correct.