Skip to content

Commit

Permalink
Partial fix for projectlombok#163 "Overloading of Builder Method not …
Browse files Browse the repository at this point in the history
…Handled Correctly"

added check for existed builderMethods
  • Loading branch information
Michail Plushnikov committed Jan 12, 2016
1 parent eed1f9c commit b5910fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot
if (null == builderClass) {
builderClass = builderHandler.createBuilderClass(psiClass, psiAnnotation);
}
target.add(builderHandler.createBuilderMethod(psiClass, null, builderClass, psiAnnotation));

if (builderHandler.shouldGenerateBuilderMethod(psiClass, psiAnnotation)) {
target.add(builderHandler.createBuilderMethod(psiClass, null, builderClass, psiAnnotation));
}

if (PsiAnnotationUtil.getBooleanAnnotationValue(psiAnnotation, TO_BUILDER_ANNOTATION_KEY, false)) {
target.add(builderHandler.createToBuilderMethod(psiClass, null, builderClass, psiAnnotation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,21 @@ public String getBuilderClassName(@NotNull PsiClass psiClass, @NotNull PsiAnnota
return builderClassName;
}

public boolean shouldGenerateBuilderMethod(@NotNull PsiClass psiClass, @NotNull PsiAnnotation psiAnnotation) {
final String builderMethodName = getBuilderMethodName(psiAnnotation);
final Collection<PsiMethod> existingMethods = PsiClassUtil.collectClassStaticMethodsIntern(psiClass);
for (PsiMethod existingMethod : existingMethods) {
if (existingMethod.getName().equals(builderMethodName)) {
return false;
}
}
return true;
}

@NotNull
public PsiMethod createBuilderMethod(@NotNull PsiClass containingClass, @Nullable PsiMethod psiMethod, @NotNull PsiClass builderPsiClass, @NotNull PsiAnnotation psiAnnotation) {
LombokLightMethodBuilder method = createBuilderMethod(containingClass, psiMethod, builderPsiClass, psiAnnotation, getBuilderMethodName(psiAnnotation));
final String builderMethodName = getBuilderMethodName(psiAnnotation);
LombokLightMethodBuilder method = createBuilderMethod(containingClass, psiMethod, builderPsiClass, psiAnnotation, builderMethodName);
method.withModifier(PsiModifier.PUBLIC, PsiModifier.STATIC);
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ protected void processIntern(@NotNull PsiMethod psiMethod, @NotNull PsiAnnotatio
if (null == builderClass) {
builderClass = builderHandler.createBuilderClass(psiClass, psiMethod, psiAnnotation);
}
target.add(builderHandler.createBuilderMethod(psiClass, psiMethod, builderClass, psiAnnotation));

if (builderHandler.shouldGenerateBuilderMethod(psiClass, psiAnnotation)) {
target.add(builderHandler.createBuilderMethod(psiClass, psiMethod, builderClass, psiAnnotation));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static Collection<PsiMethod> collectClassConstructorIntern(@NotNull PsiCl
public static Collection<PsiMethod> collectClassStaticMethodsIntern(@NotNull PsiClass psiClass) {
final Collection<PsiMethod> psiMethods = collectClassMethodsIntern(psiClass);

Collection<PsiMethod> staticMethods = new ArrayList<PsiMethod>(5);
Collection<PsiMethod> staticMethods = new ArrayList<PsiMethod>(psiMethods.size());
for (PsiMethod psiMethod : psiMethods) {
if (psiMethod.hasModifierProperty(PsiModifier.STATIC)) {
staticMethods.add(psiMethod);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.plushnikov.builder.issue163;

import lombok.Builder;
import lombok.Value;

@Value
@Builder(builderClassName = "Builder", builderMethodName = "newBuilder")
public final class LombokTest {

private final String field;
private final String otherField;

/**
* LombokTest Builder.
*
* @param field the field value
* @return a new builder instance with field set
*/
public static Builder newBuilder(final String field) {
return new Builder().field(field);
}
}

0 comments on commit b5910fc

Please sign in to comment.