Any way to group methods in Java/Eclipse?

前端 未结 6 2131
感动是毒
感动是毒 2021-02-12 15:11

I would like to be able to group similar methods and have them appear in my Outline view in Eclipse. This makes navigating large swaths of code a little easier on the eye, and e

相关标签:
6条回答
  • 2021-02-12 15:21

    I use the Coffee Bytes plugin for code folding, specifically configuring it for folding code that has start and end tags.

    Although the plugin is not downloadable off the page listed on the Google Code page, it has been recompiled against Eclipse 3.5 and made available elsewhere; the version appears to work against Eclipse 3.6 and 3.7 as well. It is also available in the Yoxos marketplace.

    I use the following notation to group getters and setters of properties along with declaration of the property, although the same notation could be extended for your use.

    // {{ Id
    private String id;
    
    public String getId() {
        return id;
    }
    
    public void setId(final String id) {
        this.id = id;
    }
    // }}
    

    Configuration of the same needs to be done by setting appropriate preference in the code folding section available via Windows > Preferences > Java > Editor > Folding. Remember to choose Coffee Bytes Java Folding, and enable support for User Defined Regions.

    Although the support for grouping/folding is restricted to the editor, the natural order of the methods within the fold can be retain in the outline view. I'm afraid that I'm unaware of any grouping capabilities beyond this plug-in.

    0 讨论(0)
  • 2021-02-12 15:26

    I really miss it from the days of Smalltalk.

    The best way to denote these categories would be adding annotations to the code itself. You would then need to create a specialized outline view which uses these annotations. Sounds like a good Google Summer of Code project.

    0 讨论(0)
  • 2021-02-12 15:27

    As to your original question, I believe that is not possible with Java/Eclipse.

    However, if you have a group of similar methods that you need to distinguish from another group of methods within the same class, why not create a new class with the group of similar methods?

    0 讨论(0)
  • 2021-02-12 15:31

    To help with Vineet Reynolds answer, I would also like to offer this:

    USAGE:

    in User Defined Regions tab use e.g.:
    
    Start identifier: region    
    End identifier: endregion
    

    In code:

    //region SomeName
    your code
    //endregion SomeName
    

    Installation instructions:

    1. Install plugin
    2. Unpack the downloaded file eclipse-folding-plugin.tar.gz
    3. Copy the contents of the:
    4. features folder => eclipse features folder
    5. plugins folder => eclipse plugins folder
    6. Configure plugin in Eclipse:
    7. Select "Windows->Preferences"
    8. Select "Java->Editor->Folding"
    9. Check the "Enable folding" option
    10. Select "Coffee Bytes Java Folding" in the "Select folding to use:" option
    11. Check "User Defined Regions" in the "General Fold Setting:" option

    This is from: http://kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html

    0 讨论(0)
  • 2021-02-12 15:33

    I just ran across this and thought I should point out that while this doesn't exactly meet your original statement you can add a @category directive in the documentation block of each method then filter for just the method categories you would like to see. I have used this to only look at those groups of methods I am interested in at any one time.

    0 讨论(0)
  • 2021-02-12 15:37

    If you mean group by name, click the button 'Sort' on 'Outline view'.

    Moreover, there are some filters in the view to 'hide static', 'hide non-public'...

    0 讨论(0)
提交回复
热议问题