Bug 156736 - [compiler] Add compiler option to warn overriding methods that do not call super
Summary: [compiler] Add compiler option to warn overriding methods that do not call super
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.3 M3   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 161475 392327
  Show dependency tree
 
Reported: 2006-09-08 13:19 EDT by Frederic Fusier CLA
Modified: 2012-10-29 09:37 EDT (History)
4 users (show)

See Also:


Attachments
Proposed fix (20.36 KB, patch)
2006-10-16 17:24 EDT, Olivier Thomann CLA
no flags Details | Diff
New patch (21.78 KB, patch)
2006-10-17 10:39 EDT, Olivier Thomann CLA
no flags Details | Diff
Last patch + regression tests (33.69 KB, patch)
2006-10-18 15:03 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Frederic Fusier CLA 2006-09-08 13:19:26 EDT
JDT/Summit Compiler topic.
Comment 1 Olivier Thomann CLA 2006-10-16 17:24:27 EDT
Created attachment 52077 [details]
Proposed fix
Comment 2 Philipe Mulet CLA 2006-10-17 04:22:21 EDT
You shouldn't tag the method binding, but rather the method declaration using an ASTNode.flag instead.

Also overridingMethodWithoutSupercall
--> overridingMethodWithoutSuperInvocation

Should also cache scope.methodScope(), this isn't a free op.
Comment 3 Philipe Mulet CLA 2006-10-17 04:23:22 EDT
Batch command line argument should be "super" (shorter). 
Should also be suppressable using @SuppressWarnings("super")
Comment 4 Olivier Thomann CLA 2006-10-17 10:39:34 EDT
Created attachment 52122 [details]
New patch
Comment 5 Philipe Mulet CLA 2006-10-17 14:44:22 EDT
Additional testcase:

abstract class Y { 
  void foo();
}
public class X extends Y {
  void foo() {
    // should not complain for missing super call, since overriding 
    // abstract method
  }
}

same issue with implementing interface method, though I suspect this is covered by the fact we do not set AccOverriding in this case.
Comment 6 Philipe Mulet CLA 2006-10-17 14:46:50 EDT
for implementing abstract superclass method, you want to check that AccImplementing is not set.

i.e. (modifiers & AccOverriding|AccImplementing) == AccOverriding
Comment 7 Philipe Mulet CLA 2006-10-17 14:47:49 EDT
other than that, patch looks good. bit5 is fine, no collision.
Comment 8 Olivier Thomann CLA 2006-10-18 15:03:25 EDT
Created attachment 52261 [details]
Last patch + regression tests
Comment 9 Olivier Thomann CLA 2006-10-18 15:04:09 EDT
Released for 3.3M3.
Regression tests added in org.eclipse.jdt.core.tests.compiler.regression.MethodVerifyTest#113/119
Comment 10 Martin Aeschlimann CLA 2006-10-20 13:38:46 EDT
Maybe I msised this at the JDT summit, but I don't think this really makes sense. I don't think you can say that every method that override should call super. There are contract that say that you have to override and provide an own implemenation. In that case calling super is not required and maybe not even allowed if the super implemenation also has side effects.

What I always suggested was a new annotation that would allow me to specify that super needs to be called.
Comment 11 Olivier Thomann CLA 2006-10-20 13:41:04 EDT
It is up to the user to decide if this needs to be enabled. By default it is disabled.
Comment 12 Dani Megert CLA 2006-10-23 05:10:27 EDT
It was discussed at the summit under the label "more advanced warnings:"
    * NotNull, Nullable
    * overriding methods that do not call super
i.e. we discussed means to spec such advanced warnings in the code via some sort of "annotation" and then be able to check those. Without this mechanism I agree with Martin that it does not make much sense (too many false positives).
Comment 13 Markus Keller CLA 2006-10-23 06:09:24 EDT
+1 for comment 10 and comment 12. I would remove this warning again, until we have something like @OverrideCallSuper.
Comment 14 Olivier Thomann CLA 2006-10-23 11:19:34 EDT
As long as an annotation like @OverrideCallSuper is not standardized, we cannot use it. The actual code is fine and has almost no impact on the batch compiler. So I propose to keep the code inside the batch compiler (since the implementation simply needs to be tuned once the @OverrideCallSuper is defined) and close the one relative to the addition of the UI option as RESOLVED/LATER.
Same idea for the null checks. @Null or @NotNull will help to improve it.
See jsr305 for this.
Comment 15 Philipe Mulet CLA 2006-10-24 05:05:52 EDT
Re: need for annotation.
@OverrideCallSuper should be placed on overriden method, and force overriding ones to invoke super. Would need to figure if it propagates down for free (likely not).
Comment 16 David Audel CLA 2006-10-30 04:09:53 EST
Verified for 3.3 M3 using build I20061030-0010
Comment 17 Martin Aeschlimann CLA 2006-11-07 07:17:47 EST
Instead of waiting for something like 'java.lang.OverrideCallSuper' or 'java.lang.NotNull' to go into the JDK libraries, we should allow the user to create such an annotation by himself in his project and add a preference where the user can tell the compiler the qualified type name of that annotation.
Same for NotNull ect. As soon as Java 7 gets the annotation added, we will simple set its qualified name as the preferences default value.
So for example we might at some point add such an annotation for Eclipse 'org.eclipse.core.apis.SuperCallRequired'.

If we want to go a step further, we let clients provide a AnnotationEvaluator that does the lookup for annotations, providing something like isAnnotated(methodOrTypeOrField, "SuperCallRequired"). A default implementation of an AnnotationEvaluator would test for a certain Java 5 annotation. A client's implementation might consider Javadoc tags or even use information from a metadata file. This then of course would work also for 1.4 or less compliance.

So I really think we either evolve this feature further or remove the option again. It just doesn't make sense in the current state. It gives people the impression that we don't know the diffenerce between 'extending' and 'overriding' a method or gives a bad example for Java novices. Do you know of any projects that can use this setting?
Comment 18 Philipe Mulet CLA 2006-11-08 05:35:08 EST
JSR305 is about standardizing more annotation types.
We are not removing the support from the compiler, simply not surfacing it in UI for now.

As to providing a custom pref to define user marker annotations, we thought of it already; still thinking about the investment value add vs. waiting on JSR305 (where we will need to redo it). I think when JSR305 occurs, we will need more than just default to its values. We would rather need to support both (standard+user custom ones). 
Comment 19 Dani Megert CLA 2006-11-08 05:55:15 EST
>still thinking about the investment value add vs. waiting on JSR305
>(where we will need to redo it).
We should not only look at the investment on our side but also on the benefit for the users that are locked into using 1.4 or even older.