JReader
JReader

Reputation: 340

Is it possible to have Eclipse automatically insert imports for common classes

When I am writing out skeleton classes or just creating new classes in large quantities, I find it tedious to run Organize Imports or Ctrl-space Enter to import extremely common classes like List, ArrayList, BigDecimal. I never use any of the other List implementations and have blocked them from the content assist, which helps, but I'd rather they just silently import themselves. Aside from the fact of whether this is a good idea or not, is it possible in Eclipse to do this?

Upvotes: 2

Views: 854

Answers (1)

E-Riz
E-Riz

Reputation: 33034

I can think of a couple of ways to accomplish what you want, or at least get close:

  1. Change the New Java File template to include the imports you want. Go to Preferences > Java > Code Style > Code Templates, select the New Java files template in the list and Edit... it, inserting the imports between the ${package_declaration} and ${typecomment}. Here's an example:

    enter image description here

  2. Add a editor Template that you can invoke via content-assist (Ctrl+Space) to insert the imports where you want them. Got to Preferences > Java > Editor > Templates, then use New... to create a new template. Give it a name you can remember and type easily, and enter the code you want to insert. For example:

    enter image description here With that, you can place the cursor in any file where you want the imports, use Ctrl+Space to bring up Content-assist, and select the template (Imports in my example) to insert them at the cursor position.

  3. You can set up a Save Action that automatically invokes Organize Imports every time you save a Java file. I always do this, but you might find it too invasive since it applies to the entire project or workspace.

    enter image description here

Upvotes: 3

Related Questions