Bug 140971 - [override method] generate in declaration order [code generation]
Summary: [override method] generate in declaration order [code generation]
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC All
: P3 normal with 5 votes (vote)
Target Milestone: 3.6 M4   Edit
Assignee: Mateusz Wenus CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 165863 166601 209290 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-05-09 23:53 EDT by Thomas Pollinger CLA
Modified: 2009-12-08 07:14 EST (History)
9 users (show)

See Also:
markus.kell.r: review+


Attachments
proposed patch (16.21 KB, patch)
2009-06-30 10:21 EDT, Mateusz Wenus CLA
markus.kell.r: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Pollinger CLA 2006-05-09 23:53:50 EDT
It is disorienting that the override/implement method dialog box changes the order of the methods to be implemented. The alphabetical ordering may be an option, but definitely *not* the default.

The order for override/implement should by default be the order in which the methods and interface/class extend/implement are declared.

Example:

interface Shape {
  int getX();
  int getY();
  int getEdges();
  int getArea();
}

interface Circle extends Shape {
  int getR();
}

I want to see this when clicking on override/implement methods (aside from formatting):
class DefaultCircle implements Circle {
  public int getX() {}
  public int getY() {}
  public int getEdges() {}
  public int getArea() {}
  public int getR() {}
}

I have little or no preference for the below:
class DefaultCircle implements Circle {
  public int getArea() {}
  public int getEdges() {}
  public int getR() {}
  public int getX() {}
  public int getY() {}
}

From a readability perspective, the latter is nothing but random order.

-Thomas
Comment 1 Martin Aeschlimann CLA 2006-05-10 12:14:38 EDT
Only in the outline view and the quick outline we show the actual order. We always sort members in views. So I guess it would be nice to have an option everywhere, but this isn't planed at the moment.
Comment 2 Thomas Pollinger CLA 2006-05-10 12:31:22 EDT
Hi Martin,

thanks for your quick answer.

What you are saying is beyond the point. I agree that views *can* show methods in sorted order. When I *generate* code, I do *not* (I emphasize not) want my code to be reordered (this is in my editor, not in a view!). Code generation has to use the default method ordering as it shows in my code. If I want reordering, then I want to explicitly say so.

This is not a nice to have feature - the current behaviour is just simply not acceptable.

I reopened the bug.
Comment 3 Martin Aeschlimann CLA 2006-05-10 12:54:28 EDT
Sorry, I understood from the bug request that you meant the order how it is presented in the dialog.
Comment 4 Markus Keller CLA 2006-11-27 06:21:07 EST
*** Bug 165863 has been marked as a duplicate of this bug. ***
Comment 5 Nobody - feel free to take it CLA 2006-12-02 17:21:16 EST
I agree with the bug reporter and want to point out that the same thing also applies for other cases of method generation:

"Generate Getters and Setters" should follow the order of the field declarations.
"Generate Delegate Methods" should follow the order given in the delegate.
Comment 6 Nobody - feel free to take it CLA 2006-12-02 17:46:53 EST
*** Bug 166601 has been marked as a duplicate of this bug. ***
Comment 7 Martin Aeschlimann CLA 2007-11-09 03:30:30 EST
*** Bug 209290 has been marked as a duplicate of this bug. ***
Comment 8 Borsos Laszlo CLA 2008-07-02 11:00:47 EDT
I also agree with the bug reporter.
I keep reorganizing the methods after "Add unimplemented methods".
In Ganymede this has become easier to do, still I never understood the logic behind alphabetical order within the class instead of the logical method order defined in the interface.
Comment 9 Mateusz Wenus CLA 2009-06-29 09:56:57 EDT
I see that this bug has 'bugday' and 'helpwanted' keywords but is also in
state 'assigned'.

I'm not sure about its status, but if no one is currently working on it,
I'd like to give it a try.
Comment 10 Dani Megert CLA 2009-06-29 10:06:31 EDT
>I see that this bug has 'bugday' and 'helpwanted' keywords but is also in
>state 'assigned'.
JDT does not participate in the bugday but help is always welcome :-)

>I'm not sure about its status, but if no one is currently working on it,
>I'd like to give it a try.
It's assigned to the inbox. Once we've looked at a bug we assign it to a real owner or the inbox.

I've assigned the bug to you. Looking forward to a fix.
Comment 11 Mateusz Wenus CLA 2009-06-30 10:21:56 EDT
Created attachment 140504 [details]
proposed patch
Comment 12 Mateusz Wenus CLA 2009-06-30 10:26:06 EDT
(In reply to comment #11)
> Created an attachment (id=140504) [details]
> proposed patch
> 

Attached is proposed patch.

I modified the classes which generate the code and now:
"Generate Getters and Setters" follows the order of fields
"Generate Delegate Methods" follows the order of fields and
methods among single field
"Override/Implement Methods" and "Add unimplemented methods" follow
the order of methods in source code (the exact ordering is more 
complicated bacause you can implement methods from more than one
supertype at once - it's described in javadoc)

I didn't change ui classes - the dialogs for these operations still
show methods in alphabetical order. Do you think this should be 
changed too?
Comment 13 Thomas Pollinger CLA 2009-06-30 19:45:56 EDT
(In reply to comment #12)
> 
> I modified the classes which generate the code and now:
> "Generate Getters and Setters" follows the order of fields
> "Generate Delegate Methods" follows the order of fields and
> methods among single field
> "Override/Implement Methods" and "Add unimplemented methods" follow
> the order of methods in source code (the exact ordering is more 
> complicated bacause you can implement methods from more than one
> supertype at once - it's described in javadoc)
> 
> I didn't change ui classes - the dialogs for these operations still
> show methods in alphabetical order. Do you think this should be 
> changed too?
> 

It might be nice to show in the UI the order of what is being
generated, but as it is in a view, it might be fine to leave
it in the alphabetical ordering - I don't have a strong preference
either way.

I am more than happy to have my code in the editor in the order in
which I typed my methods in the interface. Thanks Mateusz for fixing :)
Comment 14 Mateusz Wenus CLA 2009-07-01 12:33:35 EDT
Ok, then I am marking this bug as resolved. Please verify.
Comment 15 Dani Megert CLA 2009-07-06 05:24:51 EDT
>Ok, then I am marking this bug as resolved. Please verify.
You should not mark a bug FIXED until it is really fixed in the code - which is not the case here.
Comment 16 Mateusz Wenus CLA 2009-07-06 08:36:01 EDT
(In reply to comment #15)
> >Ok, then I am marking this bug as resolved. Please verify.
> You should not mark a bug FIXED until it is really fixed in the code - which is
> not the case here.
> 

Sorry - I guess I didn't get the process right.

So I'm looking forward to a review.
Comment 17 Markus Keller CLA 2009-11-09 08:05:37 EST
Thanks for the patch. Released with a few fixes:

- Used !SourceRange.isAvailable(..) in a few places instead of a null check.
- Comparators should return 0 if they don't know what to do (returning -1 breaks general contract of compare(..)).
- The change in AddGetterSetterOperation caused a test failure in org.eclipse.jdt.ui.tests.core.source.GenerateGettersSettersTest.test6(), and it didn't respect the "Sort by:" setting from the dialog. Fixed by restructuring the code to look more similar to the old implementation.
Comment 18 Deepak Azad CLA 2009-12-08 03:12:47 EST
- "Generate Getters and Setters" : Verified
- "Generate Delegate Methods" : Verified
- "Override/Implement Methods" : Verified
- "Add unimplemented methods" :  In the example above the following code is generated. That is methods from immediate super class comes first and then the methods from super super class.

class DefaultCircle implements Circle {
  public int getR() {}  ---> this should be in the end
  public int getX() {}
  public int getY() {}
  public int getEdges() {}
  public int getArea() {}
}
Comment 19 Markus Keller CLA 2009-12-08 04:50:55 EST
(In reply to comment #18)
I think both orders (the one in comment 0 and the one implemented) are reasonable. Unless someone has a strong preference, I'd leave it as it is.
Comment 20 Deepak Azad CLA 2009-12-08 05:41:48 EST
- "Override/Implement Methods" : For this case the super super class method comes first and then super class methods.
- "Add unimplemented methods" :  Methods from immediate super class comes first and then the methods from super super class.

I prefer the implementation for "Override/Implement Methods" case. And I think that both cases should follow the same ordering of methods.
Comment 21 Markus Keller CLA 2009-12-08 07:14:15 EST
You're right, we should be consistent. Filed bug 297183 for that.