Skip to content

Commit

Permalink
Adapt fromMethodCreator to use single field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Goszczurny committed Feb 11, 2018
1 parent 6ebabad commit 5ac2632
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public BuilderPsiClassBuilder withButMethod() {
}

public BuilderPsiClassBuilder withFromMethod(boolean innerBuilder) {
PsiMethod method = fromMethodCreator.fromMethod(builderClassName, allSelectedPsiFields, srcClass, innerBuilder);
PsiMethod method = fromMethodCreator.fromMethod(builderClassName, allSelectedPsiFields, srcClass,
innerBuilder, useSingleField, srcClassFieldName);
builderClass.add(method);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.psi.PsiMember;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import org.apache.commons.lang.StringUtils;

import java.util.List;

Expand All @@ -18,20 +19,34 @@ public FromMethodCreator(PsiElementFactory elementFactory) {
this.elementFactory = elementFactory;
}

public PsiMethod fromMethod(String builderClassName, List<PsiField> allSelectedPsiFields, PsiClass srcClass, boolean innerBuilder) {
public PsiMethod fromMethod(String builderClassName, List<PsiField> allSelectedPsiFields, PsiClass srcClass,
boolean innerBuilder, boolean useSingleField, String srcClassFieldName) {
StringBuilder text = new StringBuilder("public " + builderClassName + " from(" + srcClass.getQualifiedName() + " " + FROM_PARAMETER_NAME + ") { ");
for (PsiField field : allSelectedPsiFields) {
appendField(text, field, srcClass, innerBuilder);
appendField(text, field, srcClass, innerBuilder, useSingleField, srcClassFieldName);
}
text.append("return this;}");
return elementFactory.createMethodFromText(text.toString(), srcClass);
}

private void appendField(StringBuilder text, PsiField field, PsiClass srcClass, boolean innerBuilder) {
private void appendField(StringBuilder text, PsiField field, PsiClass srcClass,
boolean innerBuilder, boolean useSingleField, String srcClassFieldName) {

if(useSingleField) {
text.append("this.").append(srcClassFieldName)
.append(".")
.append("set")
.append(StringUtils.capitalize(field.getName()))
.append("(")
.append(FROM_PARAMETER_NAME).append(".").append(getFieldValue(field, srcClass, innerBuilder))
.append(");");
} else {
text.append("this.").append(field.getName()).append(" = ").append(FROM_PARAMETER_NAME).append(".")
.append(getFieldValue(field, srcClass, innerBuilder))
.append(";");
}


text.append("this.").append(field.getName()).append(" = ").append(FROM_PARAMETER_NAME).append(".")
.append(getFieldValue(field, srcClass, innerBuilder))
.append(";");
}

private String getFieldValue(PsiField field, PsiClass srcClass, boolean innerBuilder) {
Expand Down

0 comments on commit 5ac2632

Please sign in to comment.