Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generating of type and reference to returned object for method using hot keys in Intellij IDEA

How can I get the type automatically of returned object for method using hotkeys in Intellij IDEA?

Quite often there are times when you need to modify such line:

myinstance.getMyMethod();

in the following:

IMySomeObject mysomeobject = myinstance.getMySomeObject();

I wish that IDEA did it itself for me to save time. I do not wish to explore method's signature, find its returned type and manually create this reference with a specific type of returned object. It's not convenient.

like image 732
Mike Brown Avatar asked Jan 03 '13 20:01

Mike Brown


People also ask

What is the IntelliJ shortcut to got to method declaration?

Choose Navigate | Go to Declaration in the main menu, press F12 , or click the symbol while holding the Ctrl key.

What is Ctrl Shift O in IntelliJ?

In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.


1 Answers

Using the Introduce Variable refactor.

Select

myinstance.getMyMethod(); 

press <ctrl>+<alt> + V and you will see a selections of names to give it like

IMySomeObject mySomeObject = myinstance.getMySomeObject();

I then select <Enter> as the first option is usually fine.

I suggest you have a look at all the refactoring tools in the Refactor Menu and learn what they all do.


You can type m

enter image description here

then type .

enter image description here

then <Enter>

enter image description here

then press <ctrl> + <alt> + V

enter image description here

lastly press <Enter> to accept the default name.

like image 171
Peter Lawrey Avatar answered Sep 19 '22 02:09

Peter Lawrey