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

2

6

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? :)

Kantian answered 16/1, 2015 at 2:7 Comment(0)
K
7

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
Kantian answered 4/3, 2015 at 19:37 Comment(2)
Am I mistaken or does class B's foo not actually override class A's? B#foo has a parameter but A#foo does not.Insufflate
@CharlesWood you are correct and that is how this code shows that at least one version of Groovy (2.1.9) does not handle the @Override annotation correctly. Compilation should always fail (but Groovy 2.1.9 does not) since B#foo does not override A#foo, yet B#foo has the @Override annotation set. Version 2.4.1 of Groovy handles it correctly: compilation fails.Kantian
R
0

While upgrading Grails and, by extension, Groovy on a legacy system this error started to happen in lots of places. The generic class had a method which was being overridden anonymously in the code. The problem was that on instantiation the generic type wasn't filled. As @Override wasn't honored before, it worked. When I upgraded this error started to happen. I fixed it specifying the type for the generic class.

Example:

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

Object was being instantiated like this:

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

Fixed specifying the type for the generic class:

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

Also, if you are having problems with @Override, check if the version doesn't have this bug.

Resurgent answered 6/8, 2021 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.