3

Assume we have a class FooCollection which contains a somewhat long list of static nested classes*:

public class FooCollection {
    public static class FooA implements Foo {
        // ...
    }

    public static class FooB implements Foo {
        // ...
    }

    // ...
}

Assume now we have another class using all of these classes. Currently, Eclipse will auto-format this to import each class separately if we reference the class itself

import com.me.FooCollection.FooA;
import com.me.FooCollection.FooB;
import com.me.FooCollection.FooC;
import com.me.FooCollection.FooD;

// and then later something like
callBaz( FooA.class );

What I would prefer to avoid bloating imports and constant commits changing imports due to colleagues using IntelliJ, is having it imported as

import static com.me.FooCollection.*;

However, I can't seem to find anything to get Eclipse to do this. Is there something I am missing or any way to get Eclipse to do it this way?

Edit: I actually just checked and even new FooA() will still cause the imports to switch back to this, despite setting the start imports threshold.

*) I realize that this is not exactly a good design, but it's a legacy application and for the sake of it let's assume that the code cannot be changed.

Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100

2 Answers2

2

Dave Newton is referencing the setting under "Organize Imports" in Window -> Preferences. You can set the threshold for importing using a wildcard. Looks like the default is 99 classes before going to the wildcard. If you set it at 2, it looks like it would do what you needed!

darktrek
  • 132
  • 1
  • 9
  • Sorry, my question was not precise enough – my bad! I know this setting, but it won't apply if the class is directly referenced like `FooA.class`. – Ingo Bürk Jan 29 '14 at 20:04
  • "Edit:" This setting seems to only affect methods, not static classes. – Ingo Bürk Jan 29 '14 at 20:16
0

Not sure, if there is a way to make it to work globally, but there is a short cut to deal with one member at a time.

If you select FooCollection.FooA and press Ctrl + Shift + M will add the static import and also will update all other references with in that file.

I use this mostly to import Enums and constants.

RP-
  • 5,827
  • 2
  • 27
  • 46
  • This works fine for methods, but if I reference the class directly as `FooA.class` (my bad for not mentioning), it won't work. – Ingo Bürk Jan 29 '14 at 20:05
  • Yes, you are correct.. I thought it would work, just tested and its not working. – RP- Jan 29 '14 at 20:10
  • I just checked, it doesn't work for static classes at all, even if not referenced by class. – Ingo Bürk Jan 29 '14 at 20:16