Auto Import With Eclipse Snippets - eclipse

I started using snippets recently. The most common one I use inserts this at the cursor:
private Logger log = LoggerFactory.getLogger(getClass());
However, I still have to do a CTRL-SHIFT-O to import Logger and LoggerFactory.
Out of pure laziness I ask this: Is there a way to do an automatic import when I insert this snippet?

I don't think a Snippet can automatically insert an import statement (or do anything other than insert text at the current cursor location). But you can set up an automatic Save Action to do the organize imports whenever you save. Open the project properties and navigate to Java Editor > Save Actions.
I usually set up a few different things in the Additional Actions including Organize Imports, removed trailing whitespace, insert missing annotations, etc.

Related

How to keep import * for specific package when organizing imports in Eclipse

For some imports, I may want to keep a * import even I am currently using only 1-2 classes/methods. For example, I may want to import org.mockito.Mockito.* even I am currently using only a few of them, because when my test grows, I will need to use more static method under Mockito. However Organize Imports function in Eclipse always break down my import to import individual methods (which is the preferred behavior in most case).
Is there a way to let me control such behavior in package basis? i.e. I want to keep importing * for some packages (or disabling auto-break-down feature), but for other packages, I would want to use Eclipse's default behavior (deciding * base on number of classes/methods used)
You can't change the policy for individual imported packages or classes, but you can set the overall threshold lower so that Organize Imports won't convert the wildcard import to individual ones. Go to Preferences > Java > Code Style > Organize Imports and then edit the Number of static imports needed... value to something small, like 2.
With it set to a small number, you can manually add a wildcard import and as long as the class uses at least the threshold number of methods, Eclipse will leave it alone.
This doesn't appear to be possible at present, but as it's clearly a useful idea I've added a feature request to eclipse bugzilla for it: https://bugs.eclipse.org/bugs/show_bug.cgi?id=506826

Prevent eclipse from putting static imports on top

Is there anyway I can stop eclipse from moving static imports to the top? We have code that doesn't have static imports on top and every time i hit organize imports it causes a long diff in the code.
You can define the preferred sort order in the "Organize Imports" preferences. This tab also allows you to specify where you want specific static imports.
If you want Eclipse to not change the order of imports at all, then you must never use "Organize Imports." Use "Remove Unused Imports" instead.
You can add both normal and static imports with *, and then choose the order.

Eclipse wraps NOPMD Tag after imports

I have a little problem with Eclipse. When i set a //NOPMD tag behind an import it wraps the tag to the end of the imports. How can I avoid that eclipse wraps any //NOPMD tags during beautifying the java program?
Example before beautifying:
import java.io.IOException;//NOPMD
Example after beautifying:
import java.io.IOException;
//NOPMD
How about using #SupressWarnings("PMD.NameOfRule"). This will suppress all instances of that rule in the class which may or may not be what you want. But it is immune to code reformatting and organize imports.
As a reference, here are the options for suppressing.
Found a better answer once I stopped looking at this as a PMD problem! The accepted answer to this question shows how to turn off formatting on sections of the code by writing:
// #formatter:off
...
// #formatter:on
You'd need to do this on each class around your imports. Or each class that has //NOPMD that is. And you'd need to turn on the setting in your workspace. And have your teammates to this same if you are on a team.

Is there a way to tell Eclipse how to resolve ambiguous java names?

My eclipse is configured so that when I save the java source file, it automatically inserts the missing import declarations. Except when the reference is ambiguous. Fpr instance, the most annoying ambiguity is with List<T> - both java.util and java.awt declare it. Here eclipse demands manual resolution.
I was wondering if it was possible to configure somewhere that whenever List<T> is used then java.util should be imported. Or alternatively, since I am not using java.awt, I could just remove it from the list of possible suggestions.
Any ideas?
Thanks.
This sounds like a possible duplicate of Exclude packages from Eclipse's organize imports.
Basically, you want to change your Type Filters preference to exclude java.awt.* packages. Keep in mind that doing so will make things harder/confusing if you ever try to write AWT/Swing code.
One thing that pops into my mind is altering the class template in the preferences to include the line:
import java.util.List
Unless you are going to use the AWT version of a List, or care about unused import statements, that should solve this annoying issue ..
Manually invoke Organize Imports and it will ask you when an ambiguity is found.

Eclipse/FDT closes imports when typing in a new import

For example I type the following:
import shared.
And on the dot, it closes the imports forcing me to reopen the imports and continue typing. Is there a way to disable this? What is the point of this?
This cannot be disabled without completely disabling autocomplete.
With autocomplete, when a selection is made, FDT will use that and then return you to the editor so you can enter another keyword.
You shouldn't ever need to type an import with FDT. You can:
1) Use autocomplete on a class / type and have FDT auto import the class
2) Use Organize Imports
3) Use a quick fix to import the class.
See more here:
http://fdt.powerflasher.com/docs/Main_Page