简体   繁体   中英

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, eg 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?

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!'
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM