Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Enum in grails (not in domain class)

I want to use an Enum to represent some selection values. In the /src/groovy folder, under the package com.test, I have this Enum:

package com.test

public  enum TabSelectorEnum {
  A(1), B(2)

  private final int value
  public int value() {return value}

}

Now, I am trying to access it from controller like:

TabSelectorEnum.B.value()

but it throws an exception:

Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: Could not initialize class com.test.TabSelectorEnum

What am I doing wrong?


Update: After I cleaned and recompiled, the error code changed to:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.test.TabSelectorEnum(java.lang.String, java.lang.Integer, java.lang.Integer)

It seems like there is something wrong in the way accessing the value of the Enum, but I don't know what.

like image 805
bsr Avatar asked Jun 16 '10 02:06

bsr


1 Answers

You didn't define a constructor for the int value:

package com.test

enum TabSelectorEnum {
   A(1),
   B(2)

   private final int value

   private TabSelectorEnum(int value) {
      this.value = value
   }

   int value() { value }
}
like image 155
Burt Beckwith Avatar answered Sep 17 '22 11:09

Burt Beckwith