Default imports in Eclipse - java

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:

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: Source → Organize 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.

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

Related

How to remove all unused imports from a Java file in VS code

I use VS Code with the Extension Pack for Java.
When I have unused imports like
import java.util.ArrayList;
import java.util.stream.Collectors;
in my .java file I can remove each import individually via the "Quick Fix" action (Ctrl+.).
But how can I remove all unused imports from a Java file in VS Code?
UPDATA
Now that vs code Java has added a code action, you can use Remove all unused imports directly in quickfix to remove all unused imports.
You can use the shortcut key Shift+Alt+O
. This will format your import code and of course remove unused imports.
For example, the original import code is like this
After using the Shift+Alt+O to format the import code
Fix them on just SAVING the file, the easiest way
Follow these steps to setup settings in VS Code.
Press [Ctrl + , ] to open setting
Click on that top-right icon (Open settings JSON) as shown in the below image marked in red.
Add this below code in settings.json file.
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
Now just save it and its done

Intellij class Import being added to object not the top of class

I've just started a new IntelliJ project. When I try and import a java class with the IntelliJ import shortcut, the import is being added to the object and not to the top of the class like it normally would.
Where in the settings can I change this?
See Preferences/Settings > Editor > Code Style > Java if this option ...
Use fully qualified class names
... is ticked then IntelliJ will always use fully qualified class names. If you disable this option then Intellij will include the import statement and refer to the class by its 'simple' name.
Here's a screenshot showing this configuration item:
If the accepted answer is not working for anyone, Try the below points too.
Check whether the class file is in correct package and resolve if there is any issues in the class file.
Click File -> Invalidate Caches/ Restart and Click Invalidate and Restart button, Once it restarted U'll be able to import properly.
I was facing this issue with my custom / generated(protobuf) java packages.
Solution (TDLR)
Whenever you face this issue, just check the package or class that getting imported doesn't have any error(red marking).
If so try to fix that.
I was having an issue with package naming and its actual location. By generated-sources -> Right Click -> Mark Directory As -> Sources Root corrected my issue.
Check whether generated-sources or folder under the generated sources need to mark us Sources Root.
My issue was with generated java files (Adding this just to help someone else with same issue)
Intellij shows red mark for Import with wildchar(import pkg1.subpkg1.*;). Like editor was unable to recognizing 'import all' under a package.
But direct import to the inner package was fine.
On top of that an resolving issue with import class add full package to the Class itself, instead of adding import statement on top the java file.
This issue didn't affect the code compilation, only in editor it was showing red mark.

Intellij Access static variables from another class within package without their classes' names?

When I was in Android Studio and typed whatever static variables in other classes, they were automatically imported and there were no class names in front of them. However, now I am in a Java project in IntelliJ and it does not recognize whatever such variables I type. How do I make it behave like Android Studio?
What's more frustrating, when I do ClassName.staticVariable, it sometimes does not work. Did I miss some settings or something?
Here is an image about the problem.
image
IntelliJ (from v16 onwards) had had this configuration option:
Add unambiguous imports on the fly
Select this check box to have IntelliJ IDEA automatically add imports that can be added without user intervention.
You can select this from Preferences > Editor > General > Auto Import. Here's a screenshot:
With this and static methods and fields enabled IntelliJ will auto import statics. For example, hitting return in the following screenshot adds an import for the referenced static and the resulting code includes that static without its qualifying class e.g. AN_UNAMBIGUOUS_STATIC instead of Statics.AN_UNAMBIGUOUS_STATIC:

Intellij idea auto import across files

I have auto import enabled in idea, but it requires me to open the file in the editor (like it should). Now, i have done some regex magic, which means across 100+ classes i am using new classes that need to be imported. Since its all done with find/replace, those files have never been opened in the editor, and therefore the new classes havent been auto imported. Is there any way to run auto import unambiguous references across all files? cause currently, i have to compile, and then open all the files from the errors window? Optimize imports aparently doesnt do new imports.
I am not sure in which version of IntelliJ this feature became available (I am currently using 2017.1.1) but you can select a package from the project browser and in the context menu optimise imports for that package or use the keyboard shortcut Ctrl+Alt+o.
I just used this to update the imports of all my classes after changing my import settings and it worked like charm.

How to delete specific import from each file in application?

In eclipse, how can I delete a specific import from each file in a package?
For example, I need to remove the following from an entire package:
import android.R;
I am NOT asking how to remove unused imports, just how to remove a specific line of code..in this case, an import.
Select the package in your Package Explorer View.
Choose Menu->Search->File
in the dialog set Containing text to import android.R; and File name patterns to *.java
for Scope - choose "Selected resources"
Click on Replace
The Search view will show all matches and you get a new dialog where you'll have to enter the replacement (leave it empty in your case)
Note, that this is not a refactoring. Replacing text may lead to dozens of compiletime errors, that have to be fixed manually.
Preferences->Java->Editor->Save Actions->Configure...
You can configure the removal of unused imports when saving from the Unnecessary Code tab.
Adding imports would normally be done as you are writing the code otherwise it won't compile.
ctrl-Shift-O-I is the shortcut to organize import in a particular file in eclipse..
Also you can right the entire package and select the Organize Import option available there to rmove unsed imports in entire package.

Categories