|
This patch gets rid of the exception.... Index: src/main/org/codehaus/groovy/runtime/InvokerHelper.java
===================================================================
--- src/main/org/codehaus/groovy/runtime/InvokerHelper.java (revision 8215)
+++ src/main/org/codehaus/groovy/runtime/InvokerHelper.java (working copy)
@@ -353,7 +353,7 @@
return invokeMethod(script, "run", EMPTY_ARGS);
}
- public static Script createScript(Class scriptClass, Binding context) {
+ public static Script createScript(final Class scriptClass, Binding context)
{
// for empty scripts
if (scriptClass == null) {
return new Script() {
@@ -363,11 +363,18 @@
};
}
try {
- final GroovyObject object = (GroovyObject) scriptClass.newInstance();
Script script = null;
- if (object instanceof Script) {
- script = (Script) object;
+ if (Script.class.isAssignableFrom(scriptClass)) {
+ script = (Script) scriptClass.newInstance();
+ } else if (Enum.class.isAssignableFrom(scriptClass)) {
+ // return the class object...
+ script = new Script() {
+ public Object run() {
+ return scriptClass;
+ }
+ };
} else {
+ final GroovyObject object = (GroovyObject) scriptClass.newInstance();
// it could just be a class, so lets wrap it in a Script wrapper
// though the bindings will be ignored
script = new Script() {
But then actual attempts to use the enum result in the following exception.... groovy> enum E {A,B,C} (JVM 1.5.0_11-b03 Windows Vista, if it matters) Hence the failures I didn't commit and will let the deeper language folks dig at it. enums created by Groovy are implementing GroovyObject. I think the problem is more inside the static initializer.. looks like a ClassCastException.. a trivial element is missing I guess the exception int the static initializer is fixed |
||||||||||||||||||||||||||||||||||||||||||||
The compiler returns an Enum object, whereas the InvokerHelper is expecting a Script or a GroovyObject.
I propose we return the Class object for the enum in this instance (IMHO the other sensible option being to return null). This will only be visible in scripts where the only expression is a single enum statement, so I don't think it can be tripped elsewhere accidentally.