Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grails

groovy

I'm using the Grails 2.3.8 build system to build my Grails project (i.e., the default system built on top of Gant).

When I annotate my methods with @java.lang.Override, Grails doesn't fail compilation even if the method overrides nothing in the parent classes.

When I compile directly using groovyc, things work fine.

Is there a compilation option I haven't enabled? :)

like image 266
Vahid Pazirandeh Avatar asked Jan 16 '15 02:01

Vahid Pazirandeh


1 Answers

Grails 2.3.8 uses Groovy 2.1.9. In that version of Groovy the @Override annotation is not honored in (at least) the situation I was using it (the most basic case):

class A {
        def foo() {}
}


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

}

In that version of Groovy (2.1.9) the above code compiles just fine.

Then I downloaded the latest version of Groovy (as of now, 2.4.1) and tried compiling the same class. The compiler threw an error as I expected:

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

UPDATE: There actually are two ways in which even Groovy 2.1.9 honors @Override:

  1. If the access modifier does not match (public, protected, private)
  2. If the method return type does not match
like image 117
Vahid Pazirandeh Avatar answered Oct 13 '22 11:10

Vahid Pazirandeh