[groovy-user] Enum inside a class

1,302 views
Skip to first unread message

Xiaoping Jia

unread,
Jan 7, 2010, 11:03:03 AM1/7/10
to us...@groovy.codehaus.org
Hi,

There seems to be a problem with enum inside a class (in 1.7.0). In the
following example

class EnumTest2 {

enum Direction { North, East, South, West }

static void main(args) {
for (d in Direction) {
println d
}

}

}

An exception is thrown:

Caught: groovy.lang.MissingMethodException: No signature of method:
static EnumTest2$Direction.values() is applicable for argument types: ()
values: []

If I move the enum outside the class to its own file, the same code
works fine.

Cheers,

Xiaoping


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Weiler-Thiessen,David,SASKATOON,Engineering

unread,
Jan 7, 2010, 11:55:52 AM1/7/10
to us...@groovy.codehaus.org
Hi

I don't know that that would have ever worked in Groovy. Groovy does not support nested classes. Instead, you can define multiple classes in the same source file, no nesting, like you are forced to do with Java.

What you did to fix the issue is what I would expect to be done.


David Weiler-Thiessen
Nestlé Purina PetCare
phone: 306-933-0232
cell: 306-291-9770

This e-mail, its electronic document attachments, and the contents of its website linkages may contain confidential information. This information is intended solely for use by the individual or entity to whom it is addressed. If you have received this information in error, please notify the sender immediately and promptly destroy the material and any accompanying attachments from your system.

Roshan Dawrani

unread,
Jan 7, 2010, 11:59:51 AM1/7/10
to us...@groovy.codehaus.org
The following works in 1.6.7. I think nested enums have been supported by 1.5.x series too.

===================================
class EnumTest {

    enum Direction { North, East, South, West }
    static m() {

        for (d in Direction) {
            println d
        }
    }
}

EnumTest.m()
===================================

Roshan Dawrani

unread,
Jan 7, 2010, 12:05:50 PM1/7/10
to us...@groovy.codehaus.org
Jochen, what are the AIC related changes to EnumVisitor? It checks if the enum ClassNode is an anonymous inner class and if yes, doesn't add the standard enum methods like values/previous/next/valueOf and also fields like MAX_VALUE/MIN_VALUE.

I see that the changes were done as part of GROOVY-2443, but I can't really understand why inner enums have special handling because of that JIRA.

The current issue faced in iterating over enum values is coming from there - it is not able to iterate because enum inner class is not having "values" method.

Xiaoping Jia

unread,
Jan 7, 2010, 12:05:58 PM1/7/10
to us...@groovy.codehaus.org
Hi, David,

Anonymous Inner Classes and Nested Classes are added to 1.7 and enum
inside a class also works to some extent, except when values() of the
enum is needed like in the example.

Cheers,

Xiaoping

On Thu, 2010-01-07 at 10:55 -0600,

Roshan Dawrani

unread,
Jan 7, 2010, 12:09:51 PM1/7/10
to us...@groovy.codehaus.org
Jochen, in the example below, you wanted to skip adding enum stuff to ClassNode for "{String activity() { 'Relax' }}", is it?

enum Day {
    SUNDAY {
        String activity() { 'Relax' }
    }, MONDAY
    String activity() { 'Work' }
}

It seems to be having some side effects.

Xiaoping, could you please open a JIRA for this?

rgds,
Roshan

Weiler-Thiessen,David,SASKATOON,Engineering

unread,
Jan 7, 2010, 12:09:44 PM1/7/10
to us...@groovy.codehaus.org
Thx Guys

I learned something new today :-)

Xiaoping Jia

unread,
Jan 7, 2010, 12:43:00 PM1/7/10
to us...@groovy.codehaus.org
I have opened an issue in JIRA.

Xiaoping

Roshan Dawrani

unread,
Jan 7, 2010, 12:46:51 PM1/7/10
to us...@groovy.codehaus.org
Thanks. It seems to be a little regression issue that should get corrected.
Reply all
Reply to author
Forward
0 new messages