Eclipse refactoring

Move method to another class (alt+shift+v)
Given: Class A contains method m and we want to move the method to class B.
Solution: Create a reference to class B in class A (if not exists), select method m (move the cursor to the method name and press alt+shift+arrow), refactor (alt+shift+t) and choose move option (or directly alt+shift+v to move the selected method)
Note: if you want to move a method which implementation references class fields, then the default move refactoring will extend method signature to include ‘this’ reference of original class. Clean up of that can be tedious. I suggest in this case to do the following:
1) extend method sygnature with class fields via ‘introduce parameter’ refactoring so that state to the method to be moved is only passed via paramters
2) move method to the destination class
3) create class fields in the destination class named the same as method parameters added in 1)
4) use change method signature shortcut to remove method parameters added in 1)

Push (move) method/field down to subclasses (push up to super classes)
Given: Class Subclass extends Superclass, we want to move (push down) the method m from Superclass to the Subclass.
Solution: select the method and press ctrl+shift+t (refactor menu for the selected method), choose push down (to move the method to subclasses) or push up (to move the method to the super class)

Extract interface from class
Given: Class MyImpl has public method myMethod (only public ones), we want to create interface MyInterface with myMethod that will be implemented by MyImpl class.
Solution: press ctrl+shitf+t (refactor menu) and choose public methods to be included in the extracted interface

Change the type of an object in the declaration to one of its supertypes
Given: Class Aex extends A, we have a declaration Aex a; we want to refactor to A a.
Solution: Select the reference, press ctrl+shift+t and choose generalize type

Extract local variable (alt+shift+L)
Given: We want to extract into local variable what a method returns or a primitive or string used as a parameter, e.g. myM(), myM(1L) or myM(“text”)
Solution: Select the whole myM method expression, primitive 1L or string “text” and press alt+shift+L

Extract constant
The same as extract local variable but places it as a constant field (choose variable’s definition and press alt+shift+t and choose extract constant)

Inline method’s body or field’s value
Given:

String text="Hi";
myM(text);

we want to have myM("Hi"); only.
Solution: In order to insert method’s code or field’s value, select the reference where it it used (not defined) and press alt+shift+I

Extracting (moving) method’s block of code into a new separate method alt+shift+M
Given: if a method does more than one thing, is too long or we want to reuse some of its code, we can extract the block of the code into a separate helper method
Solution: Select the block of code and press alt+shift+M to extract it to the new separated method

Introduce parameter (move local variable to be used as a method’s parameter)
Given:
caller: ref.my()
callee:

void m(){
   String text = "Hi";
   System.out.println(text);
}

expected after refactoring:
caller: m("Hi");
callee:

void m(String text) {
   System.out.println(text);
}

Solution: Select local variable value (“Hi”), press alt+shift+t and choose introduce method parameter (and inline the local variable to println method)

Move the local variable declaration to the top of the class
Solution: Select the variable’s reference, refactor via choosing Convert local variable to field. In the dialog you can choose where it field will be initialized (method, constructor or at field declaration).

Create getters/setters for a field
Solution: Select field reference and refactor it via the encapsulate field option.

Create factory method
Solution: create conctructor, select in and refactor it with introduce factory option.

Shortcuts
Current class: (ctrl+)
Outline methods/fields ctrl+o
Go to next error/warning ctrl+.
Go to previous error/warning ctrl+,
Go to Line number
Find/replace text
Organize imports ctrl+shift+O
static imports ctrl+shift+M on static method/field/enum
Delete the line ctrl+shift+D
Ctrl + Q for going to last edited place
2nd rename for local renaming in file ctrl+2,r

Show in the view of other classes
type hierarchy view ctrl+t (e.g. when selected a method, it will list its declaration and all its implementations; when selected a type it will show its type hierarchy)
call/access hierarchy popup ctrl+alt+h, used for methods, fields, classes (use filters for results exclusion *Test classes)

Search (go to)
declarations ctrl+g
references ctrl+shift+g
text ctrl+alt+g

Search in workspace (ctrl+shift+) for:
Open Resource ctrl+shift+R
Open Type ctrl+shift+T
Open Listing of Eclipse shortcuts ctrl+shift+L (or Spring STS quick search)

Views/tabs:
F4 – type hierarchy
Shift+Alt+Q, P – package
Search opened tabs ctrl+e

Refactoring alt+shift+
Local variable extract
Inline method
Change method signature
Rename
Method extraction
Move method
Refactor menu

Removing method:
1) remove method body
2) click refactor/inline on method definition

Advertisements

2 thoughts on “Eclipse refactoring

  1. Thanks for the tip on “Extracting (moving) method’s block of code into a new separate method”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s