IntroduceParameterObjectPseudoRefactoringInEclipse

September 11, 2007

Eclipse 3.3 now supports this refactoring. Woot. (see IntroduceParameterObject for a full example).



There currently isn't a way to do an Introduce Parameter Object refactoring in Eclipse in one-step, but you can hack your way through most of the labor with existing refactorings. The bulk of this is taken from comments in this bug report.

Given this to start:

public String foo(int x, int y, int z) {
return String.format("%s, %s, %s", x, y, z);
}


manually recode this to the following (note: you can use quick fix stuffs to transform the params to finals)

public String foo(final int x, final int y, final int z) {
return new Object() {
String compute(int z) {
return String.format("%s, %s, %s", x, y, z);
}
}.compute(z);
}


now use the Convert Anonymous to Nested refactoring on the Object(). Unfortunately, this step tends to gork up the parameter names when they're transformed to member names in the new Nested class, so you'll need to edit them if you want.

You'll now have something like this:

class Compute {
...
}

public String foo(final int x, final int y, final int z) {
return new Compute(x, y, z).compute(int z);
}


Inline the foo method and no more long parameter list -- except in the constructor of the nested class now. You'll need to decide if this is acceptable, or if you want to translate the constructor params to setters.

tags: ComputersAndTechnology