5

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?

user339108
  • 12,613
  • 33
  • 81
  • 112
  • There is in IntelliJ (free edition) so I imagine it should be in eclipse as well. It is likely to be an auto-fix if you can do it. – Peter Lawrey Dec 16 '10 at 08:12
  • 1
    for idea it's alt+Enter on Flowers.ROSE. – Vladimir Ivanov Dec 16 '10 at 08:15
  • 1
    @Vladimir: Alt+ENTER? That's for properties...? – Lukas Eder Dec 16 '10 at 08:18
  • For what it's worth, I don't think it's a good idea to overuse static imports. Depending on the extent you're using them, it can increase the likelihood of a compiler error. (Say you had Color.ROSE and Flowers.ROSE). Obfuscating the enum makes it harder to read when there's a lot more logic. This similar to implementing an interface of constants (which is allowed but not conventional). – Lam Chau Dec 22 '10 at 11:47

3 Answers3

8

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

Jasper
  • 2,166
  • 4
  • 30
  • 50
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • The above shortcut works fine, is there a way to automate this across all java files within the eclipse project. – user339108 Dec 16 '10 at 09:08
  • I have so many constants like this and would love to see an automated solution, if possible – user339108 Dec 16 '10 at 09:15
  • 1
    @user339108, if I realy realy want to do something like this, I typically write a short perl script to handle the change. – rsp Dec 16 '10 at 09:46
  • Thanks for the tip, but would love to see if there is any other way to achieve this. – user339108 Dec 16 '10 at 09:52
  • 3
    An other workaround is to define favorites in Eclipse for the most commonly used holders of static objects. That's a good solution, if you have a few enum classes with lots of enums and lots of references. See also here: http://stackoverflow.com/questions/288861/eclipse-optimize-imports-to-include-static-imports – Lukas Eder Dec 16 '10 at 09:54
  • Hi Lukas - Thanks for the link its really helpful, but I am still not able to modify all the existing enum imports as static imports. How do I achieve this? – user339108 Dec 16 '10 at 18:08
  • The biggest problem which I have is that I have a large number of enums which needs to be migrated as static imports. All of the above solutions help you to have a strategy of using static imports but not about migrating the existing ones. Kindly clarify – user339108 Dec 17 '10 at 05:10
  • Well, why don't you search and replace? Write a very elaborate regular expression and then: good luck! :) I can't tell you more than I did. That's why I wrote `I'm interested too in other ideas, though` – Lukas Eder Dec 17 '10 at 08:16
0

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).
Community
  • 1
  • 1
jd.
  • 4,057
  • 7
  • 37
  • 45
  • I have so many references like this in my project (its not just the NAME reference alone). I would like to know if there is a way to do this across all such references. – user339108 Dec 18 '10 at 18:23
0

Simply hit Ctrl + Shift + M on the word Rose and you will see that it is statically imported.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Jinesh Parekh
  • 2,131
  • 14
  • 18
  • I have many references like this in my project (its not just the NAME reference alone). I would like to know if there is a way to do this across all such references automatically – user339108 Dec 29 '10 at 05:32