Issue Details (XML | Word | Printable)

Key: GROOVY-2213
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Jason Dillon
Reporter: Jörg Staudemeyer
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

Instantiation exception in Groovysh when creating an enum type

Created: 15/Oct/07 12:17 PM   Updated: 17/Oct/07 12:05 PM
Component/s: Groovysh
Affects Version/s: 1.1-rc-1
Fix Version/s: 1.1-rc-2

Time Tracking:
Not Specified

Environment: JVM 1.6.0, Windows Vista


 Description  « Hide
When I try to create an enum in Groovysh an exception occurs. Apparently, the enum type is being properly created, but groovysh uselessly tries to instantiate it.

Session transcript:

Groovy Shell (1.1-rc-1, JVM: 1.6.0-b105)
Type 'help' or '\h' for help.
------------------------------------------------------
groovy:000> enum E {A,B,C}
ERROR java.lang.InstantiationException: E
groovy:000> println E.A
A
===> null

BTW you get a similar problem when defining a class with a private default constructor that also cannot be instantiated:

groovy:000> class K { private K(){} }
ERROR java.lang.IllegalAccessException: Class org.codehaus.groovy.runtime.InvokerHelper can not access a member of class K with modifiers "private"



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Jason Dillon added a comment - 17/Oct/07 11:51 AM
I'm not really sure what the proper fix for this puppy is.... but I have found that if we insert an expression into the bufferer when evaluating (after imports, before user code) that things seem to be much happier.

Jason Dillon added a comment - 17/Oct/07 12:05 PM
This may be fixed now in trunk. I was not really sure what the best solution was... and the wee hack I added might actually cause other strangeness... I'm not sure. Seems to be harmless so far. Basically, I just added a dummy true statement to the evaluated buffer after imports and before the user's code when evaluating (when parsing to detect a complete buffer this isn't used).

So, for example if you entered:

groovy:000> enum E {A,B,C}

Internally, it will parse out:

enum E {A,B,C}

and then since this is complete it will evaluate:

true
enum E {A,B,C}

And if imports were added, then a enum, like:

groovy:000> import java.*
groovy:000> enum E {A,B,C}

It will parse out:

import java.*
enum E {A,B,C}

And then it will evaluate:

import java.*
true
enum E {A,B,C}