replace constructor with builder: do not suggest to default chain constructor params

This commit is contained in:
anna
2010-11-25 22:24:23 +03:00
parent 7efe81bbed
commit ba1cd24643
5 changed files with 52 additions and 1 deletions
@@ -61,8 +61,12 @@ public class ParameterData {
int i = 0;
for (final PsiParameter parameter : chainedConstructor.getParameterList().getParameters()) {
if (!parameter.isVarArgs()) {
final PsiExpression arg = args[i];
final ParameterData parameterData = initParameterData(parameter, result);
parameterData.setDefaultValue(args[i++].getText());
if (!(arg instanceof PsiReferenceExpression && ((PsiReferenceExpression)arg).resolve() instanceof PsiParameter)) {
parameterData.setDefaultValue(arg.getText());
}
i++;
}
}
}
@@ -0,0 +1,18 @@
public class Builder {
private int i = 2;
private int j;
public Builder setI(int i) {
this.i = i;
return this;
}
public Builder setJ(int j) {
this.j = j;
return this;
}
public Test createTest() {
return new Test(i, j);
}
}
@@ -0,0 +1,11 @@
public class Test {
public Test(int i, int j){}
public Test(int j){
this(2, j);
}
void foo(){}
public static void main(String[] args){
new Builder().setJ(1).createTest().foo();
new Builder().setI(2).setJ(3).createTest().foo();
}
}
@@ -0,0 +1,11 @@
public class Test {
public Test(int i, int j){}
public Test(int j){
this(2, j);
}
void foo(){}
public static void main(String[] args){
new Test(1).foo();
new Test(2, 3).foo();
}
}
@@ -50,6 +50,13 @@ public class ReplaceConstructorWithBuilderTest extends MultiFileTestCase {
doTest(true, defaults);
}
public void testConstructorChainWithoutDefaults() throws Exception {
final HashMap<String, String> defaults = new HashMap<String, String>();
defaults.put("i", "2");
defaults.put("j", null);
doTest(true, defaults);
}
public void testConstructorTree() throws Exception {
doTest(true, null, "Found constructors are not reducible to simple chain");
}