java.lang.InstantiationException: Planet
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:366)
at groovy.lang.GroovyShell.parse(GroovyShell.java:500)
at groovy.lang.GroovyShell.parse(GroovyShell.java:480)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:458)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:400)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:772)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:614)
at org.codehaus.groovy.runtime.Invoker.invokePogoMethod(Invoker.java:98)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:79)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:74)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:158)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:681)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:224)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:66)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:64)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:537)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:224)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:614)
at groovy.lang.Closure.call(Closure.java:292)
at groovy.lang.Closure.call(Closure.java:287)
at groovy.lang.Closure.run(Closure.java:368)
at java.lang.Thread.run(Thread.java:619)

Daniel.Sun
added a comment - 23/Sep/07 5:20 AM The code runs well with jdk6u2
the result of executing is:
3.303E23, 2439700.0
failed to run the following code with groovy1.1beta3 too, but run with jdk6u2 well :
System.out.println(Enum.valueOf(Planet.class, "MERCURY"));

As far as I can say the problem comes from the fact that the generated $INIT method is trying to invoke an enum constructor reflectively, and this is not allowed for enums.
And I think it is the last part of the $INIT implementation that is performing this op (just before the CHECKCAST).

Alexandru Popescu
added a comment - 05/Oct/07 4:44 PM As far as I can say the problem comes from the fact that the generated $INIT method is trying to invoke an enum constructor reflectively, and this is not allowed for enums.
And I think it is the last part of the $INIT implementation that is performing this op (just before the CHECKCAST).
./alex
–
.w( the_mindstorm )p.

Alexandru Popescu
added a comment - 05/Oct/07 6:40 PM Well, I have found a hack but it is for sure not the fix:
line: 1802 ACG:
original:
//load "this"
if (constructorNode!=null)
{
mv.visitVarInsn(ALOAD, 0);
} else {
mv.visitTypeInsn(NEW, BytecodeHelper.getClassInternalName(callNode));
}
new:
boolean newObj= false;
//load "this"
if (constructorNode!=null) {
mv.visitVarInsn(ALOAD, 0);
}
else
{
mv.visitTypeInsn(NEW, BytecodeHelper.getClassInternalName(callNode));
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 4);
newObj= true;
}
I am storing the new created object so I can load it at the end of this method.
At the end of the ACG.visitSpecialConstructorCall if(newObj) mv.visitVarInsn(ALOAD, 4);
(and as I said I am aware that this is just a hack).

ah, I guess now I get it... I was misleaded by the issue title... The problem isn't the enum itself, the problem is code trying the excute the enum as script. The problem would then be in InvokerHelper#createScript. It is no problem of the Enum implementation in Groovy at all, it is just a problem with the restriction of the reflection API for creating instances of Enum classes. Maybe I should go even one more step further and say that it is only a problem of GroovyShell, because GroovyShell seems to assume, that every class is convertable to a script. And that is not true for Enums. If you use your example for the groovy command from command line it will work without problem, the problem is only from GroovyShell.

We have now different choices...
1) let GroovyShell or InvokerHelper#createScript check for the enum class and let it react different in that case
2) say enums can't be used in a GroovyShell

blackdrag blackdrag
added a comment - 03/Nov/07 5:24 PM ah, I guess now I get it... I was misleaded by the issue title... The problem isn't the enum itself, the problem is code trying the excute the enum as script. The problem would then be in InvokerHelper#createScript. It is no problem of the Enum implementation in Groovy at all, it is just a problem with the restriction of the reflection API for creating instances of Enum classes. Maybe I should go even one more step further and say that it is only a problem of GroovyShell, because GroovyShell seems to assume, that every class is convertable to a script. And that is not true for Enums. If you use your example for the groovy command from command line it will work without problem, the problem is only from GroovyShell.
We have now different choices...
1) let GroovyShell or InvokerHelper#createScript check for the enum class and let it react different in that case
2) say enums can't be used in a GroovyShell

Jason Dillon
added a comment - 04/May/08 3:57 AM Is this still an issue? I just tried this with groovysh 1.6-beta-2-SNAPSHOT and it works with no issues.
Also not sure this is a groovysh bug...
Anyways, seems to be fixed to me, can you please close this issue?

Removed groovysh as component as I think this is a GroovyShell not groovysh issue. I still get the following on 1.5.6 and 1.6-beta1 when running the original example in GroovyConsole: Exception thrown: java.lang.InstantiationException: Planet.

Paul King
added a comment - 04/May/08 5:31 AM Removed groovysh as component as I think this is a GroovyShell not groovysh issue. I still get the following on 1.5.6 and 1.6-beta1 when running the original example in GroovyConsole: Exception thrown: java.lang.InstantiationException: Planet .

blackdrag blackdrag
added a comment - 08/May/08 12:03 PM doesn't groovysh use GroovyShell? I added that component, because of the comment from November 3rd... I would also like to reassign this issue to someone else