Reputation: 6192
Say I have some class Foo
class Foo {
protected String x = "x";
public String getX() {
return x;
}
}
I have a program that uses Foo and violates LoD
class Bar {
protected Foo foo;
public Bar() {
this.foo = new Foo();
}
public Foo getFoo() {
return foo;
}
}
public static void main(String [] args) {
Bar bar = new Bar();
String x = bar.getFoo().getX();
}
Refactoring to use LoD looks like this:
class Bar {
protected Foo foo;
public Bar() {
this.foo = new Foo()
}
public String getFooX {
return foo.getX();
}
}
public static void main(String [] args) {
Bar bar = new Bar();
String x = bar.getFooX();
}
IntelliJ-IDEA has a lot of refactoring methods (e.g. extract to method, extract to variable, inline).
Is there a method in IntelliJ-IDEA that will refactor code like bar.getFoo().getX()
to look like bar.getFooX()
?
Upvotes: 3
Views: 243
Reputation: 26462
Assuming that your example is Java code, you could do the following:
bar.getFoo().getX()
(creating getFooX()
)getFooX()
to Bar
if necessarygetFooX()
getFooX()
$a$.getFoo().getX()
with $a$.getFooX()
if you forgot step 3;-)getFoo()
(should be only in getFooX()
)Upvotes: 6