Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Eclipse to automatically import a class with multiple options

Tags:

If a class is used in a Java project in Eclipse, and it's not imported already, and there is only one class with that name, Eclipse will automatically import it.

If there are two or more classes with the same simple name, Eclipse will ask the user to select the desired one. I would like to avoid having to select one and instead prefer having a default class imported.

For example, I commonly use List and ArrayList. And each time I use them in a new class, I have to select java.util.ArrayList and java.util.List from the suggestions of Eclipse, because there is another class with the name List (java.awt.List).

Is there some way to set java.util.List and java.util.ArrayList as a default import if List and ArrayList is used?


For now, I created an Eclipse template that triggers on the word List. Are there any other ideas or improvements?

${:import(java.util.List,java.util.ArrayList)} List<> list = new ArrayList<>(); 
like image 233
Nick Siderakis Avatar asked Feb 02 '11 04:02

Nick Siderakis


People also ask

When using Eclipse whichever classes are needed for the present class can be imported automatically?

In above situation, Eclipse comes with a nice feature called “Organize Imports” to imports all the classes that are used, automatically.

Why import option is not working in Eclipse?

Try this: Go to Preferences » Java » Editor » Content Assist » Advanced. Make sure Other Java Proposals is ticked/checked. If that does not work simply close the project and reopen it.

How do I get all imports in Eclipse?

Press: CTRL + SHIFT + O and you should see below dialog boxes. Choose your desired import package and click next. It will prompt you for your next import and thats it. You are done.


1 Answers

You can also use the "Type Filters" from the Eclipse Preferences menu. This allows you to 'forbid' java.awt.* for all of your projects rather than just one.

Java -> Appearance -> Type Filters (or search for "Type Filters")

like image 126
Damo Avatar answered Jan 01 '23 21:01

Damo