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:
Special case of Change Method Signature refactoring.
You can introduce a public static final
field. The changes you make are in red:
class A {
public static final A instance = new A();
...
}
A
class is created by right-clicking the package (in the Package Explorer) that
you want to insert the class, followed by New ¡æ Class:
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.
In the Package Explorer window, right-click
the Java (class) file to be deleted, and ¡æ Delete.
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
A special case of Move
and Delegate (with no
delegation).
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
Before
|
After
|
|
Class A
|
void m(B b) {
}
|
|
Class B
|
void m(A a) {
}
|
Before
|
After
|
|
Class A
|
void m(B b) {
}
|
|
Class B
|
void m() {
}
|
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);
}
|
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);
}
|
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.
Special case of Change Method Signature
refactoring.