Reputation: 525
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.*;
Upvotes: 15
Views: 2001
Reputation: 100
In Netbeans you can navigate through this , Tools-->templates-->java Folder--> you can give what you need to want while opening a page Example :there will be Java class,interface,enum,exception etc
Upvotes: -6
Reputation: 101
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.
Upvotes: 5
Reputation: 13696
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:
Upvotes: 12
Reputation: 72854
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.
Upvotes: 7
Reputation: 29285
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.
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: Source
→ Organize Imports
IDE search through your codes and lookup each class and add their corresponding import statements. Also unused imported classes will be removed.
Upvotes: 0