Search code examples
grailsgroovyenumsrun-app

Groovy/Grails - Error at run-app with Enums


I'm facing this error

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: AnimalInfoType(java.lang.String, java.lang.Integer, java.lang.Integer) when

starting the app via "run-app" and I've created the constructor for Enum, as seen below:

    package jogoanimais

    public enum AnimalInfoType
    {
      ANIMAL(1), 
      ACTION(2)

        final int value
        private AnimalInfoType(int value) {
          this.value = value
        }

        int value() { value }    
    }

My domain class is like that:

    class AnimaisTreeMap {

       String nodeDescription
       AnimalInfoType nodeInfo
       AnimaisTreeMap yesAnswerNode
       AnimaisTreeMap noAnswerNode

        static constraints = {
            yesAnswerNode nullable:true
            noAnswerNode nullable:true        

        }
        static mappedBy = [ yesAnswerNode: "none", noAnswerNode: "none" ]   
        static mapping = {
            yesAnswerNode cascade: 'delete'
            noAnswerNode cascade: 'delete'
        }
    }

And at my BootStrap.groovy I fill the table like that:

    def noAnswer =  new AnimaisTreeMap(nodeDescription:"macaco", 
                                       nodeInfo: AnimalInfoType.ANIMAL,
                                       noAnswerNode:null, 
                                       yesAnswerNode:null)
    noAnswer.save(failOnError: true)
    def yesAnswer =  new AnimaisTreeMap(nodeDescription:"tubarão", 
                                        nodeInfo: AnimalInfoType.ANIMAL,
                                        noAnswerNode:null, 
                                        yesAnswerNode:null)
    yesAnswer.save(failOnError: true)           

    new AnimaisTreeMap(nodeDescription:"vive na água", 
                       nodeInfo: AnimalInfoType.ACTION,
                       noAnswerNode: noAnswer, 
                       yesAnswerNode:  yesAnswer).
                       save(failOnError: true)                  
}

What am I doing wrong at BootStrap.groovy?


Solution

  • Remove the enum constructor and its public definition. It's not necessary, all classes are public by default.

    And edit the class ended by Enum. AnimalInfoTypeEnum.

    enum AnimalInfoTypeEnum
    {
      ANIMAL(1), 
      ACTION(2)
    
        final int value
    
        int value() { value }    
    }