Bug 149382 - [getter setter] Generated getters and setters in order of fields instead of alphabetical
Summary: [getter setter] Generated getters and setters in order of fields instead of a...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Linux
: P3 enhancement with 1 vote (vote)
Target Milestone: 3.3 M1   Edit
Assignee: Tobias Widmer CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed
: 56069 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-07-01 17:48 EDT by Stefan Reuter CLA
Modified: 2008-01-14 05:42 EST (History)
2 users (show)

See Also:


Attachments
Naive patch based on 3.2 Release (3.2.0.v20060605-1400.) (1.51 KB, patch)
2006-07-03 17:44 EDT, Philippe Ombredanne CLA
no flags Details | Diff
Naive patch based on HEAD (1.51 KB, patch)
2006-07-03 17:44 EDT, Philippe Ombredanne CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Reuter CLA 2006-07-01 17:48:16 EDT
Currently if generating getters and setters for multiple attributes they are always in alphabetical order.
It would be nice if they would be in the same order as the corresponding attribtutes.
Comment 1 Jerome Lanneluc CLA 2006-07-01 18:32:53 EDT
Moving to JDT UI
Comment 2 Philippe Ombredanne CLA 2006-07-03 17:42:57 EDT
I find it quite annoying too.
The generation sort order is driven by what is being displayed in the dialog tree.
What is being displayed in the tree is based on the content provider which returns the source order of the fields.
First problem:
gettter/setter entries are stored in a hashmap, which has an undertermined iteration ordering.
The simplest solution there would be to use a LinkedHashMap which by default has a defined itertaion order based on insertion.

Second problem:
Regardless of what the content provider order is, the viewer gets attached to a view sorter (JavaElementSorter) that sorts elements by category (not really relevant in that case) and then by element name. Therefore what is being viewed is always alpha sorted.
The simplest solution would be to not attach a JavaElementSorter to the viewer.

Those two simple changes make that:
- the view is guaranteed to be sorted by sourec order
- the genration of getter/setters which only follows what the view order is is made in source order

Now this is a very naive implementation (I have 

I think there could be a few avenues to investigate have something decent: (I am listing what comes up to mind, but there are surely other ways, and not all apply together)
- a preference setting (or a persistent dialog setting) to drive if getters setters generation dialog should be alpha or source sorted by default.
- a button in the dialog to sort by alpha or source order, or additional options in the sort order drop down (that would also change the displayed tree order) to have:
-- First getters then setters (alpha order)
-- First getters then setters (source order)
-- Fields in getter/setter pairs (alpha order)
-- Fields in getter/setter pairs (source order)
- Implement a JavaElementSorter that would respect the source order.
- update the generation operation to take into account this new sort ordering and be a bit independent of what is being passed from the dialog.

Or just do something ultra simple based on the what the tree order is.

 

Comment 3 Philippe Ombredanne CLA 2006-07-03 17:44:04 EDT
Created attachment 45681 [details]
Naive patch based on 3.2 Release (3.2.0.v20060605-1400.)
Comment 4 Philippe Ombredanne CLA 2006-07-03 17:44:31 EDT
Created attachment 45682 [details]
Naive patch based on HEAD
Comment 5 Stefan Reuter CLA 2006-07-03 18:33:35 EDT
Thanks for that very quick patch! It works great
Comment 6 Martin Aeschlimann CLA 2006-07-04 11:03:01 EDT
Adding more options to the combo makes sense, I was hoping you're patch adds this... :-)
To use a Hashmap looks like a bug. But the elements in the view should better be shown in alphabetical order. If you have lots of field you can't find them anymore. But I'm fine if we make this optional and also offer that the creation order can be configured. 
Comment 7 Tobias Widmer CLA 2006-07-05 08:21:31 EDT
Fixed in HEAD > 20060705

The view order is alphabetical and the generation order according to the declaration order of the fields.
Comment 8 Philippe Ombredanne CLA 2006-07-14 03:17:35 EDT
Tobias, Martin: Thanks!
I reckon that I was a bit lazy, but the patcher rule #1 is to make as few changes as possible to the code to make it easier on the recieving end. ;-)

Getting the combination of alpha order in the view and declaration order for the generation sounds like an excellent compromise, if is not configurable.
Comment 9 Dani Megert CLA 2008-01-14 05:42:52 EST
*** Bug 56069 has been marked as a duplicate of this bug. ***