Bug 108782 - [1.5][compiler] inconsistent @Override error
Summary: [1.5][compiler] inconsistent @Override error
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 3.1.2   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-05 06:07 EDT by Tom Hofmann CLA
Modified: 2006-01-10 09:15 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Hofmann CLA 2005-09-05 06:07:17 EDT
M20050831-1200

Consider below snippet.

--------- SubSup.java ----------
class Sup2<E> {
	E foo(E e) { // m2
		return null;
	}
}

class Sub2<T> extends Sup2<T> {
	@Override
	Object foo(T arg0) { // m1
		return null;
	}
}
--------------------------------

- m1's signature clearly is a subsignature of m2, hence m1 overrides m2.
- this is supported by
  - the compiler warning ("the return type is incompatible")
  - the override indicator
  - no "name clash" error
- However, there is also an error that m1 "must override a superclass method"
stemming from the '@Override' annotation. 

I think that no matter whether the return type is or is not compatible, the
@Override annotation is correct and should not be flagged, since m1 does indeed
override m2.

Notes:
- javac shows the same behavior (flags @Override with "method does not override
a method from its superclass")
- whether the m1 is return-type-substitutable to m2 or not is not clear to me,
since Object = |E| (see 8.4.5). However, this is both flagged by javac and
eclipse and may be a spec issue. Markus has raised the issue on another mailing
list.
Comment 1 Philipe Mulet CLA 2005-09-08 03:55:36 EDT
This indeed feels inconsistent.
Comment 2 Kent Johnson CLA 2005-10-11 14:20:54 EDT
These are the errors I get from javac (same as eclipse):

X.java:16: foo(T) in Sub2 cannot override foo(E) in Sup1; attempting to use 
incompatible return type
found   : java.lang.Object
required: T
        Object foo(T arg0) { // m1
               ^
X.java:15: method does not override a method from its superclass
        @Override
         ^
2 errors


This PR is the same spec issue as bug 105808 so we'll track it there.

*** This bug has been marked as a duplicate of 105808 ***
Comment 3 Markus Keller CLA 2005-10-12 04:52:39 EDT
Comment 0 is discussed in http://forum.java.sun.com/thread.jspa?threadID=659964
, and it looks like it's a spec bug and eclipse should wait with changing the
compiler until that issue has been resolved.

I don't think it's too much connected to bug 105808 (which is just about the API
method IMethodBinding#overrides(..)). This bug complains about a superfluous
error for @Override when an overriding method does not meet all additional
requirements.

Below is a standalone example of "inconsistent @Override error". That javac
flags Sub#foo()'s @Override with "method does not override a method from its
superclass" is just as wrong as eclipse's "The method foo() of type Sub must
override a superclass method". Sub#foo() *does* override according to JLS3
ยง8.4.8.1. Therefore, there should not be any error for the @Override annotation.

A similar case (but here, both compilers are correct) is given in SubException:
SubException#foo() does override, but does not fulfill the additional
requirements. Therefore, @Override is OK and the invalid exception is correctly
flagged as error.

class Top {
    Integer foo() { // m2
        return null;
    }
}

class Sub extends Top {
    @Override
    Object foo() { // m1
        return null;
    }
}

class SubException extends Top {
    @Override
    Integer foo() throws IOException { //m1
        return null;
    }
}
Comment 4 Kent Johnson CLA 2005-10-12 11:23:34 EDT
Ok - we'll wait for resolution of http://forum.java.sun.com/thread.jspa?
threadID=659964
Comment 5 Markus Keller CLA 2005-10-12 14:14:10 EDT
OK, so you agree with the main issue of this bug (which was not the spec issue
in the example from comment 0, but the wrong message from the @Override
indication, as elaborated in comment 3)?
Comment 6 Kent Johnson CLA 2005-10-12 17:09:48 EDT
I changed my mind.

We will no longer complain about the @Override when the return types are not 
compatible while the parameters match.
Comment 7 Kent Johnson CLA 2005-10-13 12:56:04 EDT
Chnaged several tests in MethodVerifer (see 27 & 44).

Released into HEAD and the 3.1.2 branch
Comment 8 Frederic Fusier CLA 2006-01-10 07:54:30 EST
Verified for 3.1.2 using build M20060109-1200.
Comment 9 Frederic Fusier CLA 2006-01-10 09:15:31 EST
Verified for 3.2 M4 using build I20051215-1506.