简体   繁体   中英

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?

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:

    在此处输入图片说明

  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:

    在此处输入图片说明 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.

    在此处输入图片说明

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