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
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 …
Pingback: Grails 2.2.x and the problem with inner classes | tOMPSON's blog
Just what I needed. Didn’t have the problem in development mode only in production. This fixed it. Tnx!
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 :(
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!