How to remove unused code in IntelliJ IDEA? [duplicate] - java

This question already has answers here:
Remove unused variable declarations automatically - Android Studio/Inteiij
(2 answers)
Closed 3 years ago.
In IntelliJ, in a .java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O. Is there any shortcut to remove those unused declarations?

As #Magnilex pointed out, hover over the unused variable / method and hit alt + enter to open the intentions menu (if that's how its called).
But in addition to this, don't just remove / safe delete this on variable, but choose "Fix all 'Unused declaration' problems in this file" which will not only get rid of this one, but of all.
See here:
An alternative approach might be to us the "Run inspections by name" and run the "Unused declaration" inspection for your whole project / module / Package, whatever.
This can be achieved by either right clicking on the editor, and chosing >Analyze >Run Inspection by Name from the context menu (or the keyboard short cut, which is ctrl+alt+shift+i by default). Then type "Unused declaration":
This will inspect your code for unused declarations and display them in an overview:
See also https://stackoverflow.com/a/56593832/2987273

Check the Code-Cleanup documentation, see if any of those solutions fit your needs. Creating a profile for that task might be a decent idea, as you can trigger it from anywhere, and hit all relevant occurrences.
Just be aware that his approach might "eat up" some new / uncompleted code, or even raise false-positives, e.g. due to a child project which is not in scope. So: handle with care.

There is not shortcut to remove unused code in a file. Just to make that sure: IntelliJ IDEA is only able to check if the code is currently used in your project.
If the declaration is used by another consumer, that is not part of your project, you may break the consumer when removing that piece of code.
Due to that fact, it's not possible to implement a general automatic remove unused declaration.
There may be cases where this is more safe like an unused private method, but there is no save automatic way to do that.
Therefore, there is no shortcut.
What you can do in order to check your code base and decide whether or not to remove code marked as unused is to run a Inspect Code analysis.
Just right click on you file or source code folder, got to Analyze and Inspect Code. Choose the scope, like a folder or a specific file and press Ok.
After that you can scroll through the warnings aso.

Related

Remove all unused code based off of a single Main method

Do you know any way to find (un)used code from the perspective of a specific main method (or what IntelliJ calls an entry point)?
The background is that we have a big java project in which we have custom code for several clients. Sometimes we give clients the current state of the project in source form but would obviously want to remove the parts which only concern other clients. We have a Main file for each client so it should be possible to follow imports from there to find exactly the files relevant to each client.
I was hoping to be able to run intellij's code inspection on a single entry point but was unable to find anything. I'm open to any solution though.
You could remove all other entry points and run intellij's code inspection again to detect unused code. Now that you have only one entry point it should be able to tell you what code is unused. With that knowledge you can revert you changes and remove this code.
Hope that helps.

Eclipse Content Assist ignoring spelling errors

I recently bought a new keyboard and keep making spelling errors, closing the Content Assist midway. Is there any option within Eclipse preferences that let it stay open if at least some of the members have been found and spelled correctly?
For example if it suggests "myMethod()" and I write "myMb", Content Assist closes.
Eclipse does not feature a spelling error correcting content assist. I am also not aware of any plugins that do this.
What comes somewhat close is Eclipse Code Recommenders’ Subwords completion, which allows you to put in substrings of the method name, e.g. myMd would complete to myMethod(). However, when there are characters not part of the method’s name in your search string, the method won’t be found.

content assist does not work in Eclipse

When I am typing Ctrl-Space in Java file in Eclipse June, I am getting "no proposals". Meanwhile, if I do Edit->Content Assist->Java Proposals I have normal proposals list.
I.e. default proposals are not Java by some reason.
What it can be and how to setup?
This happened to me at some point when I upgraded to Eclipse Kepler. Go to your Preferences. The appropriate page should be Java -> Editor -> Content Assist -> Advanced.
There are two lists of proposal types. Checking a type in the first list allows that proposal to appear automatically when it detects you pressing the trigger key. If it can't find any assist options in any of the checked proposal types, the Content Assist box will not appear.
Checking a type in the second list allows that proposal type to appear in the Content Assist box when it is already on screen, accessed by pressing the command to cycle the current proposal type. You can even change the order of appearance.
To solve your problem, at the very least, make sure Java Proposals checked in the first list (Technically, you can have it checked in only the second, but that means you'd need to manually bring up Content Assist and cycle to Java each time). Once checked, it should be automatically chosen as the default Content Assist for .java files.

Preventing Eclipse from making suggestions that would alter a certain file

Is there a way of forcing Eclipse not to come with auto-correct changes that would alter the current file I am in?
Usage scenario:
I have a script that automatically generates a .java file, which contains references to undefined methods, that I then use Ctrl+1 to generate stubs for. However, Eclipse also occasionally suggests "change to ..." in addition to "create method ...". Is there a way of forcing Eclipse to never make that suggestion for a particular file?

Eclipse recent files

Is there any list of recently opened files in eclipse?
If yes then is there also any shortcut for it?
Frequently I close classes in editor because there are too many of them opened. But in a few minutes I need them again. Maybe you solve this problem differently?
Thanks!
Probably your best option is to use "Quick Switch Editor" (Ctrl+E) but it displays the opened files only. There you have quick list along with filtering by starting to type first chars of file name. To make it useful, you may increase the size of opened files/windows before closing - Window->Preferences->General->Editors->Number of opened editors before closing. After you reach this threshold the oldest file open is closed in order to make room for newly open one.
The list of the files recently opened is displayed in File menu, just before the Exit menu option.
There you'll find the already open and last closed files.
If you want to use it extensively then you should probably increase the size of the items - Window->Preferences->General->Editors->Size of recently opened files list. You can quickly invoke one of last 9 filed with Alt+F,[Digit].
A drawback of third option "Open Resource" (Ctrl+Shift+R) is that the default list looks sorted, so it is not based on the most recent date of use. Also "Open Type" (Ctrl+Shift+T) has a long listing over time and you should enter part of the name to get a shorter match.
My initial comment is based on Eclipse 4.3 Kepler and is applicable to current latest Eclipse (4.5 Mars) too.
If you click on the File menu, you would see the list of recently used files. By default there are four of them. To increase the no, goto Window-> Preferences-> General -> Editors. You can set to as many as 15
If you know the class name you can use Ctrl+Shift+T (for classes) or Ctrl+Shift+R fo any other resource file. You don't have to type full name just the uppercase letters. Eclipse shows recently opened file in topmost section and other files in the lower section.
You can also use Ctrl+Q to goto last edit location.
For a complete list of shortcuts goto Help>KeyAssist. Also checkout Tips and Tricks under the same menu.
I'm using CTRL + F6.
Unfortunately it is not possible to use "regular expressions" as for CTRL + O, but I think this is what are you looking for.
You can either use the "last edit location" shortcuts on the toolbar (the yellow arrows, alt+left) if you wish to reopen a recently edited file, or use the short list of recently opened under the "File" menu (right above "exit")
I created a plugin called "RecentEditors" that shows a list of recently closed files, sorted by close time (so the last closed editor is at the top). There is also a command (and keyboard shortcut) to directly re-open the last closed editor.
You find the plugin on the Eclipse Marketplace, and GitHub.

Categories

Resources