简体   繁体   中英

How do I re-factor references to static enum members

My code consists of references to enumeration in the following fashion.

Flowers { ROSE, SUNFLOWER }

import com.mycompany.Flowers;

class A {
    public void foo(...) {
        Flowers flower = Flowers.ROSE;
    }
}

I would like the above code to use static references to Flowers, the code would then look like

import static com.mycompany.Flowers.ROSE;

Flowers flower = ROSE;

How can I re-factor my code (using Eclipse) to use static references of enums instead of the normal referencing mechanism. Is there a way to tell Eclipse to modify all regular enum references to static references?

That's probably not as proficient as you're looking for, but Ctrl + Shift + M on the reference of a static object will statically import it (works for members and methods alike)... That way you can achieve your static imports one-by-one.

I'm interested too in other ideas, though

Here's how you can do it in two simple steps:

  • Use a find and replace maybe with a regular expression to change all the instances Flowers.NAME to just NAME .
  • Then do a project wide 'Organize imports' like so: Select the project in the package explorer and press Ctrl + Shift + O (same keystroke as the single class version). Should work for packages, etc.. Bobs your uncle. (From this answer ).

只需在单词Rose按Ctrl + Shift + M ,您将看到它是静态导入的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM