简体   繁体   中英

Get Eclipse to prefer static imports of nested classes

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.

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!

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.

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