inline refactoring trick
consider the following scenario: you have a class Foo which offers a non-static method getFoo():
public class Foo {
public String getFoo() {
return "foo";
}
}
you're wearing the refactoring hat and have decided to change the method to be static. of course you could just add a static modifier and fix all compile errors, but let's assume that your method is used a gazillion times in your project, so this is not really an option...
tada! here comes the inline refactoring trick (I haven't invented it, but somehow I can't find the original article anymore):
- perform a rename method refactoring and change
getFoo()togetBar():public class Foo { public String getBar() { return "foo"; } } - duplicate the method, rename one of the duplicates back to
getFoo(), and add the static modifier to the one still calledgetBar():public class Foo { public String getFoo() { return "foo"; } public static String getBar() { return "foo"; } } - replace the complete contents of
getFoo()with a static call togetBar():public class Foo { public String getFoo() { return Foo.getBar(); } public static String getBar() { return "foo"; } } - here comes the magic: perform an inline refactoring on the
getFoo()method, which will change all former callers ofgetFoo()to now invokeFoo.getBar() - rename
getBar()back togetFoo() - take off that STUPID HAT












