Automatic imports in Eclipse - java

In Eclipse, I have to press ctrl+space each time I refer to the type of an unimported class. Having to go back and press ctrl+space feels rather unnecessary. Is there a way to make Eclipse behave like IntelliJ in this case? It should not really be hard to know it should import the UserFactory and User class when I say:
User user = UserFactory.makeUser();
I've seen that you can press Ctrl+1 (quick fix) or Ctrl+Shift+O (Organize imports) to solve this problem, but I would like this to go automatically as it does in IntelliJ. Does anyone know a plugin or a setting that enables this type of behavior?

Open Eclipse's preferences and then go to "Java > Editor > Save Actions". There, enable "Organize Imports".
Now, whenever you save your file (yes, in Eclipse you still need to save the file, not as in IntelliJ where this happens automatically), Eclipse will try to figure out if it needs to add imports to compile the code. It will do so whenever the class name is unique. If it is not (e.g. List in java.util and in java.awt), it will not import it.
You can configure the "organise imports" action even more to ignore certain packages (e.g. java.awt if you never to gui-stuff), so you have fewer name-conflicts and more auto-imports. Look at question Exclude packages from Eclipse's organize imports for infos how to do that.

So I noticed that Eclipse had started to seemingly do all the necessary imports completely automatically.
And I started to look for how Eclipse can automatically add imports.
But all I could find was to use the Ctrl+Shift+O shortcut to Organize imports.
And that could probably be quite efficient if you want to do many at a time.
However there are at least two (or maybe three) other methods to invoke this.
Window, preferences, Java, Editor, Save actions. You will need to check: "Perform the selected actions on save" and "Organize imports".
Window, preferences, Java, Editor, Typing: Under "When pasting" you can select "Update imports".
I am not sure if this is a way of it's own or if it is a variant of what happens when pasting. But if you use ctrl + space to help with auto completing what you are writing it will also Organize imports.
So for example if I have no imports in a class and I start typing: JP and then I press ctrl + space to help me complete and I choose JPanel it will apparently automatically add: import javax.swing.JPanel; for me.

Related

Eclipse Organize Imports Shortcut (Ctrl+Shift+O) is not working

Eclipse used to import missing packages when I press Ctrl+Shift+O.
The shortcut key has stopped working when used in Java files but the same shortcut is working in Python files (importing missing packages).
Any thoughts on how to fix the issue.
Below are couple of snapshots for your reference.
The Java Organize Import command is overwritten by another Ctrl+Shift+O command: in Window > Preferences: General > Keys the Java Organize Import command (where When is In Windows) is bound to Ctrl+Shift+O, but in the main and right-click menu Source > Organize Imports the command is displayed without the shortcut.
There are two solutions for this problem:
Change or remove the shortcut of the command that overwrites the Java Organize Import command:
Find the command: In the Java editor press Ctrl+Shift+L and see which command is bound to Ctrl+Shift+O. Probably, it is the Beans Quick Outline command.
In Window > Preferences: General > Keys change or remove the binding for the found command.
Restricts the scope of the shortcut of Java Organize Import to the Java editor:
In Window > Preferences: General > Keys select the Java Organize Import command and change When from In Windows to Editing Java Source. This solution restricts Ctrl+Shift+O to the Java editor and disables the shortcut in the Package/Project Explorer.
See also: Spring IDE issue #160: organize imports is not working
To fix this issue:
Go to Preferences -> General -> Keys
Click on Filters... and de-select Filter uncategorized commands then Ok.
Then look for the command Go To Symbol in File and select it.
Then click on Unbind and then Apply and Close
Ctrl+Shift+O. should now work.
EDIT:
Also unbind any other conflicting commands and leave only one command for Ctrl+Shift+O and set the When to Editing Java Source.
Please keep in mind that if you are using an AMD GPU, your Radeon Driver could block Ctrl+Shift+O which is used to toggle an ingame-overlay. It can be changed to other keys as follows: Games → global settings → performance monitoring
Window -> Preferences -> General -> Keys
Click on Binding to sort by Binding then scroll and find Ctrl+Shift+O
If several Organized imports are present edit one bind and choose WHEN: EDITING JAVA SOURCE.
Remove( UNBIND COMMAND) for other existing bindings.
Restart Eclipse
Above shortcut resulted in GPU details being popped up on my screen. SO it was bound to some other app which was overriding it. What worked for me is
-> check.
You need to go to Gaming > Global Settings > Performance Monitoring > Toggle Performance Logging Hotkey
You will see this is bound to Ctrl + Shift + O (so the shortcut is used - just buried).
To remove the shortcut (not an obvious process at all!) you need to click the Toggle Performance Logging Hotkey button, it will then ask to enter your hotkey, you then press delete - that is Del (not backspace, not esc or anything else - the Del key).
i did all but not worked so i checked the AMD forum (https://community.amd.com/thread/227221) and found out my solution which is;
"Now I find that all I need to do is to go to Radeon Settings -> Preferences and uncheck the Show Overlay box."

How to ignore packages in netbeans 8

I inherited a big project, of which only a few packages are relevant to my work. I accidentally edited the wrong package today, and to avoid this in the future, would like to exclude them from netbean's project viewer (but not change them on disk).
Looks like there was an ignored folders property in older versions of netbeans (https://blogs.oracle.com/netbeansphp/entry/ignored_folders_sure) but I can't find this anywhere in netbeans8.1. Does it still exist?
Netbeans does not actually let you hide any packages that you have opened as a part of your project. One thing you can do is to select a package you do not want and choose to close it instead. After closing, you can "delete" it from your perspective. A dialogue will pop up and what you DO NOT want to do is also delete project contents on the disk, so be sure to unselect that option. :)

Any way to get IntelliJ-like autocompletion in Eclipse?

I'm stuck with Eclipse in one project and I would like to have autocompletion similar to that of IntelliJ - two major problems are:
Start typing and Eclipse would automatically suggest all possibilities
When there is item selected in dropdown list you can accept it not only with enter key but with dots, spacebars...
Go to Window → Preferences → Java → Editor → Content Assist.
In the column "Auto activation triggers for Java", enter this:
.qwertyuioplkjhgfdsazxcvbnm_QWERTYUIOPLKJHGFDSAZXCVBNM
Add any other keys you want. You're done. That answers your first query.
As far as second one goes, I think it already exists.
Take a look at Eclipse Code Recommenders [1] or Codetrails Connect [2]. They make Eclipse' code completion intelligent.
[1] http://eclipse.org/recommenders/
[2] http://marketplace.eclipse.org/content/codetrails-connect-community-edition/
I have to say NO. I moved from Eclipse to IntelliJ because of the intellisense and inability to make Eclipse behave like IntelliJ (or if you come from the .NET environment: inability to make Eclipse intellisense behave like ReSharper in .NET).
Do you HAVE TO work in Eclipse? I do 98% of work in IntelliJ, then convert the project into Eclipse project (IntelliJ can still access it) and finish it in Eclipse. Maybe you could try this solution.
Since Eclipse Juno, Code Recommenders Intelligent Java Proposals replaced the old JDT's Java Proposals.
It afford "Intelligent Code Completion" function, like IntelliJ did in a way.
You can get something useful through the link below:
http://code-recommenders.blogspot.jp/2012/07/code-recommenders-10-code-completion-on.html
There is something like that in Eclipse I think, certainly in the Eclipse Helios. When typing you press CTRL+SPACE, or when you typed variable name '.' will start auto-complete options.
Also here someone suggests to change auto complete delay settings to 0.
BTW you can also accept your selection with space bar, for sure.
EDIT:
Why not consider using NetBeans, my personal favourite. There you have an option to show auto-completion on start typing any Java Identifier Part. This would then look exactly as you want it. (in NetBeans Tools->Options->Editor->Code Completion, there in Language box select Java and finally check Auto Popup on Typing Any Java Identifier Part).

Intellij IDEA, format all code in a project

I really like IDEA's code formatting, but how do I get it to reformat all the code in a particular project without going through each file? I've found the option to tidy / optimise imports on code before committing it to subversion which is great, but it only seems to apply to files that have otherwise been modified (which is sensible in the majority of cases!)
Is there a way to auto-format all the source files in a project without going through them all by hand?
In the tree-structure, right click on the folder you like to reformat, choose "Reformat code" from the menu.
Choose the "whole project" radio button instead of the individual file when you ask to reformat.
Just be aware of one problem: If you're working on a larger project with other developers who don't conform to your style, and you're using IntelliJ to transform all their code every time you edit it, you might be causing a problem with your version control system. It will long a huge volume of changes when you check in, and few of them will have anything to do with changing functionality. Better to conform to the group style in that case.
According the iDea: Editor basics documentation:
You can use the shortcut Ctrl+ALT+L (Windows/Linux) or ⌥⌘+L (MAC OS X) and select the Rearrange entries option to reformat the code in the current file or reformat a module or directory (after selecting more than one file).
You can also Right-click a module, file, or directory from the context menu and select Reformat Code and also select the Rearrange entries option.
You can also optimize the imports of your files by selecting the Optimize imports option.
Is some Linux versions the shortcut Ctrl+ALT+L can lock the screen, so you will need to Right-click a file or directory from the context menu and select Reformat Code and also select the Rearrange entries option.
This works for most of the Jetbrains IDES (iDea, PyCharm, WebStorm, RubyMine, and so on.)
"CTRL + ALT + L" work only windows.
"CTRL + ALT + L" is Ubuntu Lock screen shortcut key so its not working for this.
Try ,
Right click on the folder you like to reformat, choose "Reformat code" from the menu or click "Code" button on navigation bar in top, choose "Reformat code"
Thank you

Remove redundant casts in Java

I've been generifying some Java code that used lots of casts, and now most of them are redundant and unnecessary.
It could be very tedious to inspect all the usages of the code to remove them, so: are there any tools to help to identify (and remove) superfluous casts?
This can be automatically done using Eclipse Helios. In your project, go to Properties -> Java code Style -> Clean Up. There, edit a profile, go to the "Unnecessary Code" Tab and check "Remove unnecessary casts". Then right-click your source root and klick Source-> Clean up. Job done.
If you're using Android Studio it's an easy fix: Analyze > Code Cleanup
In Eclipse, under "warnings and errors" you can set "Unnecessary cast or instanceof operations" to be "Warn" or "Error" rather than "Ignore". That should help you find them very quickly, although it won't clean them up for you.
But you can also set a "Save" action configured with an "Additional action" of "Remove unnecessary casts".
And you can right-click on an area of Package Explorer, select "Clean up..." and configure that with a "Remove unnecessary casts" action.
So basically this should be simple :)
You should probably use a tool to analyze your Java source code for potential problems (such as unnecessary casting).
Perhaps you can try PMD: http://pmd.sourceforge.net
If you use Eclipse, this should be fairly easy. Right click the class/package/project and select Source - Clean Up....

Categories

Resources