I am testing out Grails static compilation, specifically GrailsCompileStatic. The documentation is limited in explaining what Grails dynamic features aren't supported. My test Controller is very simple, but I'm running into problems already.
@GrailsCompileStatic
class UserController {
UserService userService
def list() {
def model = [:]
def model = request.JSON
withFormat {
json {
render(model as JSON)
}
}
}
}
When compiling the application I get two compile time errors. The first about a missing property for JSON
on the request
object, and a second error about a missing method for json
in the withFormat
closure.
Seems to me I'm either doing something wrong or GrailsCompileStatic
doesn't work with these features?
About
request.JSON
The
request
object'sgetJSON()
method is added via theConvertersPluginSupport
class. The exact lines are:As you can see it uses the dynamic dispatch mechanism, but fortunately it's not such a big deal. You can simply replicate it by executing
JSON.parse(request)
anywhere in your controller.Pay attention though!
JSON.parse(HttpServletRequest)
returns anObject
, which is either aJSONObject
or aJSONArray
, so if you plan on using them explicitly, and you are compiling statically, you will have to cast it.You might create a common base class for your controllers:
Then in your controller you can simpy invoke
getJSONObject()
orgetJSONArray
. It's a bit of a workaround, but results in a staticly compileable code.About
withFormat
This is a bit more complicated. The
withFormat
construct is really a method, which has aClosure
as it's first parameter. The internal implementation then figures out based on the current request or response content type which part of the argument closure is to be used.If you want to figure out how to do this statically, take a look at the source code.
You could extend this class, then use it's protected methods, but I don't know if it's worth all the hussle, you would loose much of Grails' conciseness. But if you really want to do it, you can. Don't you just love open source projects ? :)