简体   繁体   中英

Default imports in Eclipse

Is there a way to customize the default imports in Eclipse?

For example if I open a new JUnit Test Class by default I get these imports:

import static org.junit.Assert.*;
import org.junit.Test;

What I'd like to get:

import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.Matchers.*;

Unfortunately, Eclipse is quite lacking in the customizability of code generation when refactoring and creating new entities.

You might want to check out Eclipse Optimize Imports to Include Static Imports for information how to make content assist find static methods in predefined classes. That might be what you actually want. In the accepted answer Joey Gibson writes that you can add org.hamcrest.Matchers to Window » Preferences » Java » Editor » Content Assist » Favorites .


Another solution to the specific problem of statically importing Hamcrest methods, is to configure a Code Template named hamcrest instead. That way you can simply type ham and follow up with ctrl + space to get the import at the top.

The template should look like

${staticImport:importStatic('org.hamcrest.Matchers.*')}${cursor}

An even more convenient hack is to add this template to the already existing test code template which generates a new test method. If you change this template to:

@${testType:newType(org.junit.Test)}
public void ${testName}() throws Exception {
    ${staticImport1:importStatic('org.hamcrest.Matchers.*')}
    ${staticImport2:importStatic('org.junit.Assert.*')}${cursor}
}

and use this each time you make a new test method you will never have to care about adding the hamcrest import manually again.

Image to show where you configure it: hamcrest代码模板

The closest preference I can find is the one under Window --> Preferences --> Java --> Code Templates . Expand the Code section and select the New Java files option to view the pattern for newly created Java files. You can then click Edit to add the import, for instance:

${filecomment}
${package_declaration}

import org.hamcrest.*;

${typecomment}
${type_declaration}

In all cases you still need to write the code that uses the org.hamcrest package. Alternatively, just organize the imports by pressing Ctrl + Shift + O after adding the code that uses the package.

I recommend you to add org.hamcrest.Matchers.* into “Favorites” ( Window -> Preferences -> Java -> Editor -> Content Assist -> Favorites ).

This way content assist will propose static members even if the import is missing and add corresponding import when you use the member. It means you can type the method/field you'd like to use and let content assist add the import automatically.

Organize Imports

Modern IDEs offer a feature called Organize Imports . Using this feature you can no longer be worried about those import statements, the IDE itself manage those imports.

How should you do

As you write your codes, whenever you want to make the IDE to organize your imports, you should just press its shortcut keys.

Keyboard: Ctrl + Shift + O

Menu: SourceOrganize Imports

How it works

IDE search through your codes and lookup each class and add their corresponding import statements. Also unused imported classes will be removed.

在Netbeans中,您可以浏览这个, 工具 - >模板 - > java文件夹 - >您可以在打开页面时提供您需要的内容示例:将有Java类,接口,枚举,异常等

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