如何使用@GrailsTypeChecked和@GrailsCompileStatic

时间:2015-03-31 14:50:03

标签: grails

我有兴趣阅读Grails中的新注释,它理论上应该提供运行时性能改进,同时仍然允许动态调度,但它似乎没有任何效果。我错过了什么?

class MyService {

  @GrailsTypeChecked // or @GrailsCompileStatic
  def doSomething() {
    String name = missingVariable
  }
}

grails> clean
| Application cleaned.
grails> compile
| Compiling 1 source files.....

一切都在编译但在运行时爆炸?我必须遗漏一些东西,因为我真的不知道这些新的注释是如何起作用的。编译器如何能够计算出什么是坏调用以及什么是可以忽略的错误调用(即动态查找器)?

我使用的是Grails 2.5.0

2 个答案:

答案 0 :(得分:2)

该代码不应该编译。我已经构建了一个简单的应用程序,并将您的代码直接粘贴到该应用程序中,代码不会为我编译。请参阅https://github.com/jeffbrown/grailscompilestatic

| Compiling 8 source files
  [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException:     startup failed:
  [groovyc] /Users/jeff/t/grailscompilestatic/grails-    app/services/demo/MyService.groovy: 9: [Static type checking] - The variable     [missingVariable] is undeclared.
  [groovyc]  @ line 9, column 19.
  [groovyc]        String name = missingVariable
  [groovyc]                      ^
  [groovyc] 
| Compiling 8 source files.
| Error Compilation error: startup failed:
/Users/jeff/t/grailscompilestatic/grails-app/services/demo/MyService.groovy: 9:     [Static type checking] - The variable [missingVariable] is undeclared.
 @ line 9, column 19.
       String name = missingVariable
                     ^

1 error
  

编译器如何能够解决什么是错误的调用以及什么   是一个可以忽略的坏电话(即动态查找器)?

我们有类型检查扩展程序与类型检查程序协作,因此当类型检查程序认为它看到无效代码时,我们的扩展程序已启用,并且扩展程序有机会将该调用转换为动态调用,如果扩展程序可以识别它是像动态查找器这样的东西,它不能静态调度,但实际上在运行时是有效的。

答案 1 :(得分:0)

继杰夫的评论之后,似乎有一个错误导致@GrailsCompileStatic和@GrailsTypeChecked注释在类级别@Transactional注释也存在时无法工作。我相信现在已经在grails 3.0.1中修复了(谢谢大家!)