EDIT : I submitted an Eclipse enhancement request for this refactoring.
Is there a way to move a private field from one class to its helper class? The below chicken-scratch UML shows what I'm doing manually right now. Class C1
has private field
and a private final reference to a Helper
object before the refactoring.
After the refactoring, all references in C1'
to field
are changed to helper.getField()
and helper.setfield()
as appropriate.
class Field {}
class C1 {
final private Field field;
final private Helper helper;
public Field getField() {
return field;
}
public C1() {
helper = new Helper();
field = new Field();
}
}
class Helper {}
class C1Prime {
final private HelperPrime helper;
public Field getField() {
return helper.getField();
}
public C1Prime() {
helper = new HelperPrime();
}
}
class HelperPrime {
final private Field field;
public HelperPrime() {
field = new Field();
}
public Field getField() {
return field;
}
}
I've used Eclipse's refactoring capabilities quite a bit, but I can't figure out a way to automate this.
For instance, ideally I would drag the private field/attribute/member from one class to another and hope that Eclipse asks me how I want to handle the unresolved references. It offers no suggestions and breaks all of the references.
The operation that I've been repeating is to separate knowledge and behavior that doesn't really belong in the current class. I'm moving attributes and behavior that references certain fields out of the original class into a new "helper" class.
The first step in my refactoring is to move the fields. A reference to the helper class exists as a field in the class I'm refactoring from. In order not to break C1
during the refactoring, I think it would be nice if Eclipse offered to to generate getters and setters in Helper'
and update the references in C1
to use the getters/setters in the new class.