Skip to content

Commit

Permalink
Merge pull request projectlombok#148 from tomaszganski/feature/add-to…
Browse files Browse the repository at this point in the history
…-builder-support

Added initial @builder(toBuilder = true) support
  • Loading branch information
mplushnikov committed Oct 25, 2015
2 parents bdd6e5d + 03c381e commit eb9bdc9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
public class BuilderProcessor extends AbstractClassProcessor {

public static final String TO_BUILDER_ANNOTATION_KEY = "toBuilder";
private final BuilderHandler builderHandler = new BuilderHandler();
private final AllArgsConstructorProcessor allArgsConstructorProcessor = new AllArgsConstructorProcessor();

Expand Down Expand Up @@ -62,5 +63,9 @@ protected void generatePsiElements(@NotNull PsiClass psiClass, @NotNull PsiAnnot
builderClass = builderHandler.createBuilderClass(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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class BuilderHandler {
private final static String BUILDER_CLASS_NAME = "Builder";
private final static String BUILD_METHOD_NAME = "build";
private final static String BUILDER_METHOD_NAME = "builder";
public static final String TO_BUILDER_METHOD_NAME = "toBuilder";

@SuppressWarnings("deprecation")
private static final Collection<String> INVALID_ON_BUILDERS = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
Expand Down Expand Up @@ -224,12 +225,25 @@ public String getBuilderClassName(@NotNull PsiClass psiClass, @NotNull PsiAnnota

@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));
method.withModifier(PsiModifier.PUBLIC, PsiModifier.STATIC);
return method;
}

public PsiMethod createToBuilderMethod(@NotNull PsiClass containingClass, @Nullable PsiMethod psiMethod, @NotNull PsiClass builderPsiClass, @NotNull PsiAnnotation psiAnnotation) {
LombokLightMethodBuilder method = createBuilderMethod(containingClass, psiMethod, builderPsiClass, psiAnnotation, TO_BUILDER_METHOD_NAME);
method.withModifier(PsiModifier.PUBLIC);
return method;
}

@NotNull
private LombokLightMethodBuilder createBuilderMethod(@NotNull PsiClass containingClass, @Nullable PsiMethod psiMethod, @NotNull PsiClass builderPsiClass, @NotNull PsiAnnotation psiAnnotation, String methodName) {
final PsiType psiTypeWithGenerics = PsiClassUtil.getTypeWithGenerics(builderPsiClass);
final LombokLightMethodBuilder method = new LombokLightMethodBuilder(containingClass.getManager(), getBuilderMethodName(psiAnnotation))
final LombokLightMethodBuilder method = new LombokLightMethodBuilder(containingClass.getManager(), methodName)
.withMethodReturnType(psiTypeWithGenerics)
.withContainingClass(containingClass)
.withNavigationElement(psiAnnotation)
.withModifier(PsiModifier.PUBLIC, PsiModifier.STATIC);
.withModifier(PsiModifier.PUBLIC);

addTypeParameters(builderPsiClass, psiMethod, method);

Expand Down

0 comments on commit eb9bdc9

Please sign in to comment.