Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 344196

Summary: Generate delegate methods should add @Override when applicable
Product: [Eclipse Project] JDT Reporter: Glenview Jeff <junk>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: dominik.gruntz, markus.kell.r
Version: 3.7   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Glenview Jeff CLA 2011-04-28 16:18:45 EDT
Build Identifier: M20100909-0800

When I am working on a class implementing an interface, the class has a field that also implements the interface.  When I use "generate delegate methods," Eclipse should notice that these newly generated methods implement my class's interface and add @Override annotations for me.


Reproducible: Always
Comment 1 Markus Keller CLA 2011-04-29 10:46:18 EDT
That's actually a feature. An intention of the @Override annotation is to allow a compiler to detect cases where a method is not marked with the annotation but still overrides (or since 1.6 also implements) another method.

In your scenario, the overriding could also be inadvertent, so it's good that Generate Delegate Methods doesn't automatically add the annotation.

You can use "Source > Clean Up... > Missing Code" to add the @Override annotations in batch if you're sure they should really be there.
Comment 2 Markus Keller CLA 2011-05-23 11:50:40 EDT
*** Bug 346872 has been marked as a duplicate of this bug. ***
Comment 3 Glenview Jeff CLA 2011-05-23 11:59:57 EDT
Oops, I forgot I had already submitted this!  I'm confused about your explanation that the overriding could be inadvertent.  

The scenario that I am describing is when the class implements an interface.  I just don't see how it would be wrong to generate the @Overrides for the generated methods that implement the interface.  How could it ever be correct/intentional behavior to omit @Overrides on an interface implementation method?
Comment 4 Markus Keller CLA 2011-05-23 12:41:25 EDT
Code that uses the @Override annotation typically has the "Missing '@Override' annotation" set to warning or error. Now, when a new method overrides another method, there are 2 possibilities:

a) the method should override the other method
   => solution: add @Override (e.g. using the quick fix)

b) the method should not override the other method, but it does so by accident
   => solution: great that the problem was flagged early on, now let's see if we can choose a different name or otherwise restructure the code to avoid a conflict

You're proposing that Eclipse should silently choose (a), thereby taking away the opportunity for you to find a problem early on. It's not always clear that your delegate method really should override the super method.


Example:

class Foo extends File implements Bar {
    // generate delegate for bar.getName() => should it override File#getName()?
    Bar bar;
}

interface Bar {
//  void doit();
    String getName();
}
Comment 5 Sarika Sinha CLA 2016-02-08 03:38:35 EST
*** Bug 482312 has been marked as a duplicate of this bug. ***