为什么在构建 Grails 项目时 @Override 注解不起作用?

Why does the @Override annotation not work when building a Grails project?

我正在使用 Grails 2.3.8 构建系统来构建我的 Grails 项目(即,在 Gant 之上构建的默认系统)。

当我用@[=18= 注释我的方法时,Grails 不会编译失败,即使该方法在父 类.

中没有覆盖任何内容

当我直接使用 groovyc 编译时,一切正常。

是否有我没有启用的编译选项? :)

Grails 2.3.8 使用 Groovy 2.1.9。在 Groovy 的那个版本中,@Override 注释在(至少)我使用它的情况(最基本的情况)中不被尊重:

class A {
        def foo() {}
}


class B extends A {
        @Override
        def foo(String s) {}

}

在 Groovy (2.1.9) 的那个版本中,上面的代码编译得很好。

然后我下载了最新版本的 Groovy(截至目前为 2.4.1)并尝试编译相同的 class。编译器如我所料抛出错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
foo.groovy: 7: Method 'foo' from class 'B' does not override method from its superclass or interfaces but is annotated with @Override.
 @ line 7, column 2.
        @Override
    ^

1 error

更新: 实际上有两种方式甚至 Groovy 2.1.9 荣誉@Override:

  1. 如果访问修饰符不匹配(public, protected, private)
  2. 如果方法return类型不匹配

在升级 Grails 时,通过扩展,Groovy 在遗留系统上,这个错误开始在很多地方发生。泛型 class 有一个方法在代码中被匿名覆盖。问题是在实例化时没有填充泛型。由于 @Override 之前没有受到尊重,所以它起作用了。当我升级时,这个错误开始发生。我修复了它指定泛型 class.

的类型

示例:

class Base<T> {
  void foo(T t) {}
}

对象是这样实例化的:

def base = new Base() {
  @Override
  void foo(String string) {}
}

修复了为泛型指定类型 class:

def base = new Base<String>() {
  @Override
  void foo(String string) {}
} 

此外,如果您在使用 @Override 时遇到问题,请检查版本是否没有 this bug