Search code examples
grailsannotationsgrails-2.5

Why does @GrailsCompileStatic allow calling dynamic GORM finders but not domainClass.withTransaction()?


Adding the @GrailsCompileStatic annotation to a method or class allows the usage of dynamic GORM finders, e.g. findAllByIdAndProperty(). However, adding the annotation does not allow for domainClass.withTransaction(), which is also a GORM AST addition. Why?

(Using grails-2.5.3)

UPDATE (05/10/16 - 10:25AM) @jeff-scott-brown is right, it does work in general, so here is the code that fails with @GrailsCompileStatic:

...
RestfulApiService service = ServiceUtils.getService(resourceName)
service.resourceClass.withTransaction { /* do something */ }

(resourceClass is of type Class)

The error:

Compilation error: startup failed:
C:\...\myfile.groovy: 100: [Static type checking] - Cannot find matching method java.lang.Class#withTransaction(groovy.lang.Closure). Please check if the declared type is right and if the method exists.
@ line 100, column 13.
           service.resourceClass.withTransaction {
           ^

Why does withTransaction() fail in this case once the annotation is added?


Solution

  • Why does @GrailsCompileStatic allow calling dynamic GORM finders but not domainClass.withTransaction()?

    It does. The code at https://github.com/jeffbrown/withtx/blob/master/grails-app/controllers/demo/DemoController.groovy compiles.

    import grails.compiler.GrailsCompileStatic
    
    @GrailsCompileStatic
    class DemoController {
    
        def index() {
            Person.withTransaction {
                // ...
            }
            render 'Success!'
        }
    }