Eclipse Refactorings

Eclipse provides a large collection of refactorings. Below is a list of refactorings some or all of which you will use in this assignment. Please read this document and understand it before proceeding.

 

Relatives Here is an important fact when using Eclipse refactorings. Consider the class diagram below. If you rename or alter the arguments to any draw() method in this figure, you will rename or alter the arguments in ALL of the draw() methods. We use the term relatives to refer the set of methods that are updated simultaneously. Keep this concept in mind as you read on.

 

 

Below is a list of Eclipse refactorings you may use:

 

Add Parameter

Add Singleton

Create Class

Change Method Signature

Delete Class

Inline Method

Move Method

Move Method (leaving delegate behind)

Rename Method

Remove Parameter

 

 

Add Parameter

 

Special case of Change Method Signature refactoring.

 

 

Add Singleton

 

The Singleton pattern allows only one instance of a class declaration. Eclipse has no refactoring to make a class into a singleton. You can use this ¡°fake¡± refactoring by making the following changes manually. Given a class A that has no references (yet):

 

class A {

...

}

 

You can introduce a public static final field. The changes you make are in red:

 

class A {

public static final A instance = new A();

...

}

 

 

Create Class

 

A class is created by right-clicking the package (in the Package Explorer) that you want to insert the class, followed by New ¡æ Class:

 

 

 

Change Method Signature

 

To alter the signature (name, return type, or arguments) of a method, select the method in an Eclipse source code window, right-click the method, ¡æ Refactor ¡æ Change Method Signature:

 

 

To add a parameter, click on the Add button, supply the parameter type (A below) and parameter name (a below) and default value (null below) and click OK.

 

*Uncheck ¡°Keep original method as delegate to changed method¡± option

 

Deletion and editing a parameter is straightforward.

 

Note: this refactoring first computes the set relatives of the identified method (which itself is included in this set) and performs the change method signature on ALL relatives. See the relatives idiom above for more details.

 

 

Delete Class

 

In the Package Explorer window, right-click the Java (class) file to be deleted, and ¡æ Delete.

 

 

 

Inline Method

 

Suppose you had the following situation:

 

public class Main {

B b;

int i = 0;

 

void foo(A a) { bar(); }

 

void bar() {

i++;

}

}

 

You can eliminate bar() by inlining it into foo(A a) to yield:

 

public class Main {

B b;

int i = 0;

 

void foo(A a) { i++; }

}

 

To do this, right-click the bar() method, ¡æ Refactor ¡æ Inline:

 

 

You will be presented with the following dialog. Fill it in as needed to inline bar().

 

*Check ¡°All invocations¡± option

**Check ¡°Delete method declaration¡± option

 

 

Move Method

 

A special case of Move and Delegate (with no delegation).

 

Move Method (leaving delegate behind)

 

In the following figure, method A.m(B) is moved to class B leaving its delegate behind. The delegate method simply relays any calls to the moved method.

 

 

In the Package Explorer window, right-click the method to be moved and ¡æ Refactor ¡æ Move:

 

 

Note 1: Eclipse presents a dialog that lists all possible destination of the selected method. For example:

 

class A {

B b;

C c;

 

void m(D d, E e, F f) { ... }

}

  

The possible destinations of method m are class B, C, D, E, and F. To move the method, you have to select its destination. Also, to leave behind a delegate, check option ¡°keep original method as delegate to moved method¡± box (and uncheck option ¡°Mark as deprecated¡±).

 

 

*Uncheck ¡°Mark as deprecated¡± option

 

Note 2: When A.m(B b) is moved to B via its parameter b, it should become B.m(A a) as shown below.

 

Before

After

Class  A

void m(B b) {

}

Class  B

void m(A a) {

}

 

However, Eclipse automatically optimizes a method signature whenever it sees that a parameter is not being used so it ¡°can¡± produce B.m(). You will have to change it manually.

 

Before

After

Class  A

void m(B b) {

}

Class  B

void m() {

}

 

If method calls exist, they also need to be updated (e.g., a.m(b) to b.m(a)). Especially, if the delegate is created, the body of the delegate needs to be updated.

 

With no delegate

Before

After

Class  A

void m(B b) {

}

Class  B

void m(A a) {

}

Class  C

void n(A a, B b) {

  a.m(b);

}

void n(A a, B b) {

  b.m(a);

}

 

Leave delegate behind

Before

After

Class  A

void m(B b) {

}

void m(B b) {  //delegate

  b.m(this);

}

Class  B

void m(A a) {

}

Class  C

void n(A a, B b) {

  a.m(b);

}

void n(A a, B b) {

  a.m(b);

}

 

Note 3: It is not always possible to move any methods. For example, abstract methods and interface methods cannot be moved.

 

 

Rename Method

 

Right-click the method that you want to rename, ¡æ Refactor ¡æ Rename:

 

*Uncheck ¡°Keep original method as delegate to renamed method¡± option

 

Note: this refactoring first computes the set relatives of the identified method (which itself is included in this set) and performs the rename on ALL relatives. See the relatives idiom above for more details.

 

Remove Parameter

 

Special case of Change Method Signature refactoring.