Eclipse remove a static import
Asked Answered
C

2

6

With eclipse i can easily transform the static invocation to

import java.util.Arrays;
import java.util.List;

public class StaticImport {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello", "world");
        System.out.println(list);
    }
}

to a static import:

import static java.util.Arrays.asList;

import java.util.List;

public class StaticImport {
    public static void main(String[] args) {
        List<String> list = asList("hello", "world");
        System.out.println(list);
    }
}

I put the cursor on the method name (asList) and press Ctrl-Shift-M (Add Import).

Now, for some refactoring reasons, i want to remove the static import and get back to the first code:

List<String> list = Arrays.asList("hello", "world");

Is there a shorcut quickly do that ?

Canzonet answered 30/7, 2014 at 10:6 Comment(0)
V
2

You can't remove an (static) import statement with a shortcut, unless it's unused.

So, first comment out the statement:

//List<String> list = asList("hello", "world");

Then, activate the shortcut for Organizing Import Statements (Ctrl + Shift + O) and the unused import statements will be automatically removed.

Finally, uncomment the line you commented first and refactor it so it compiles:

List<String> list = Arrays.asList("hello", "world");
Vanbuskirk answered 30/7, 2014 at 10:10 Comment(1)
Thanks, this is the trick i use to do. Note that you have to import java.util.Arrays again at the end.Canzonet
P
3

Even if it's a former question:

You can do that using Eclipse Cleanup or Eclipse Save Action.

Warning: It looks like a bug to me but unchecking the options doesn't perform the opposite action.

Cleanup:

  • Go to Preferences > Java > Code Style > Cleanup
  • Click on Edit
  • Go to member accesses tab
  • In Static accesses section, check Qualify field accesses, qualify methode accesses
  • Right Click on the file > Source > Cleanup

Puntual cleanup:

  • Right click on the file > Source > CLeanup
  • Use custom profile
  • Go to member accesses tab
  • In Static accesses section, check Qualify field accesses, qualify methode accesses
  • Right Click on the file > Source > Cleanup
  • Apply > OK

Save action:

  • Go to Preferences > Java > Editor > Save action
  • Check "Additional actions"
  • Go to member accesses tab
  • In Static accesses section, check Qualify field accesses, qualify methode accesses
  • Just save the file
Patagonia answered 30/11, 2017 at 10:29 Comment(0)
V
2

You can't remove an (static) import statement with a shortcut, unless it's unused.

So, first comment out the statement:

//List<String> list = asList("hello", "world");

Then, activate the shortcut for Organizing Import Statements (Ctrl + Shift + O) and the unused import statements will be automatically removed.

Finally, uncomment the line you commented first and refactor it so it compiles:

List<String> list = Arrays.asList("hello", "world");
Vanbuskirk answered 30/7, 2014 at 10:10 Comment(1)
Thanks, this is the trick i use to do. Note that you have to import java.util.Arrays again at the end.Canzonet

© 2022 - 2024 — McMap. All rights reserved.