Issue with inner classes in Grails 2.2.0

I was working on a controller in a Grails 2.2.0 project, and it was a Groovy class structured something like this:

package mypackage;

class MyController {
  // controller methods/closures/etc.
  // uses MyControllerCommand in a POST
}

class MyControllerCommand {
  // properties/methods/constraints/etc.
}

I have used this pattern previously in Grails 1.3.7 and Grails 2.0.1, however when I invoked the closure which used the command object, I got the following error:

errors.GrailsExceptionResolver ERROR VerifyError occurred when processing request: [POST] /MyApp/myController/myMethod
(class: mypackage/MyController$MyControllerCommand, method: jsonHeader signature: (Ljava/lang/Object;)V) Incompatible object argument for function call. Stacktrace follows: ...

After some head scratching and then remembering something my colleague said, I moved the command object out to it’s own Groovy file and the problem went away. After some more searching, I found this error in Grails related to using an anonymous inner class in a controller.

But how is this related to the code above? That’s not an inner class, right? Ah, but it is. Remember how I said this was a Groovy class that housed the controller? When there are multiple class definitions in a single Groovy file, groovyc creates the equivalent of Java inner classes. You can verify this by checking the target directory after compiling your Grails project. In this instance, you would find:
target/classes/mypackage/MyController$MyControllerCommand.class

This entry was posted in Grails, Groovy and tagged , , , , , , , , . Bookmark the permalink.

5 Responses to Issue with inner classes in Grails 2.2.0

  1. tompson says:

    I am just stumbling across a similar problem in Grails 2.2.1 when upgrading – the solution to this can not be to create separate files for all the classes, a command object class should be allowed to be included in the controller …

  2. Pingback: Grails 2.2.x and the problem with inner classes | tOMPSON's blog

  3. Wytze says:

    Just what I needed. Didn’t have the problem in development mode only in production. This fixed it. Tnx!

  4. Aleksandr says:

    And this behaves differently on different platforms. I’ve got the same grails version (2.2.0) on my dev machine (JDK 1.7.0_13) and ubuntu production box (OpenJDK 1.6.0_27). In development everything is nice and clean, but in production it fails :(

  5. JDogg says:

    Wow I was trying to use an anon and an innerclass for a test. Seems to be one of those things I am not sure I dig about groovy. Thanks for the help though!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.