Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fixes #2768] Making the Builder constructor's visibility configurable. #2779

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Roland Praml <pram@gmx.de>
Rostislav Krasny <45571812+rosti-il@users.noreply.github.com>
Samuel Pereira <samuel.p.araujo@gmail.com>
Sander Koning <askoning@gmail.com>
Sebestyen Bartha <sebestyen.bartha@gmail.com>
Szymon Pacanowski <spacanowski@gmail.com>
Taiki Sugawara <buzz.taiki@gmail.com>
Takuya Murakami <tmurakam@tmurakam.org>
Expand Down
8 changes: 8 additions & 0 deletions src/core/lombok/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@
* @return The builder class will be generated with this access modifier.
*/
AccessLevel access() default lombok.AccessLevel.PUBLIC;

/**
* Sets the access level of the generated builder's parameterless constructor. By default the generated constructor's access level is {@code package}
* Note: This does nothing if you write your own builder constructor (we won't change its access level).
*
* @return The builder's constructor will be generated with this access modifier.
*/
AccessLevel constructorAccess() default lombok.AccessLevel.PACKAGE;

/**
* Prefix to prepend to 'set' methods in the generated builder class. By default, generated methods do not include a prefix.
Expand Down
12 changes: 9 additions & 3 deletions src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static class BuilderJob {
ASTNode source;
EclipseNode sourceNode;
List<BuilderFieldData> builderFields;
AccessLevel accessInners, accessOuters;
AccessLevel accessInners, accessOuters, accessConstructor;
boolean oldFluent, oldChain, toBuilder;

EclipseNode builderType;
Expand Down Expand Up @@ -176,6 +176,12 @@ void init(AnnotationValues<Builder> annValues, Builder ann, EclipseNode node) {
accessOuters = AccessLevel.PUBLIC;
}
accessInners = accessOuters == AccessLevel.PROTECTED ? AccessLevel.PUBLIC : accessOuters;
accessConstructor = ann.constructorAccess();
if (accessConstructor == null) accessConstructor = AccessLevel.PACKAGE;
if (accessConstructor == AccessLevel.NONE) {
sourceNode.addError("AccessLevel.NONE is not valid for constructor");
accessConstructor = AccessLevel.PACKAGE;
}

// These exist just to support the 'old' lombok.experimental.Builder, which had these properties. lombok.Builder no longer has them.
oldFluent = toBoolean(annValues.getActualExpression("fluent"), true);
Expand Down Expand Up @@ -336,7 +342,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
allFields.add(fieldNode);
}

handleConstructor.generateConstructor(parent, AccessLevel.PACKAGE, allFields, false, null, SkipIfConstructorExists.I_AM_BUILDER,
handleConstructor.generateConstructor(parent, job.accessConstructor, allFields, false, null, SkipIfConstructorExists.I_AM_BUILDER,
Collections.<Annotation>emptyList(), annotationNode);

job.typeParams = job.builderTypeParams = td.typeParameters;
Expand Down Expand Up @@ -526,7 +532,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {

if (constructorExists(job.builderType) == MemberExistsResult.NOT_EXISTS) {
ConstructorDeclaration cd = HandleConstructor.createConstructor(
AccessLevel.PACKAGE, job.builderType, Collections.<EclipseNode>emptyList(), false,
job.accessConstructor, job.builderType, Collections.<EclipseNode>emptyList(), false,
annotationNode, Collections.<Annotation>emptyList());
if (cd != null) injectMethod(job.builderType, cd);
}
Expand Down
12 changes: 9 additions & 3 deletions src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static class BuilderJob {
List<JCTypeParameter> builderTypeParams;
JavacNode sourceNode;
java.util.List<BuilderFieldData> builderFields;
AccessLevel accessInners, accessOuters;
AccessLevel accessInners, accessOuters, accessConstructor;
boolean oldFluent, oldChain, toBuilder;

JavacNode builderType;
Expand All @@ -124,6 +124,12 @@ void init(AnnotationValues<Builder> annValues, Builder ann, JavacNode node) {
accessOuters = AccessLevel.PUBLIC;
}
accessInners = accessOuters == AccessLevel.PROTECTED ? AccessLevel.PUBLIC : accessOuters;
accessConstructor = ann.constructorAccess();
if (accessConstructor == null) accessConstructor = AccessLevel.PACKAGE;
if (accessConstructor == AccessLevel.NONE) {
sourceNode.addError("AccessLevel.NONE is not valid for constructor");
accessConstructor = AccessLevel.PACKAGE;
}

oldFluent = toBoolean(annValues.getActualExpression("fluent"), true);
oldChain = toBoolean(annValues.getActualExpression("chain"), true);
Expand Down Expand Up @@ -279,7 +285,7 @@ static class BuilderFieldData {
allFields.append(fieldNode);
}

handleConstructor.generateConstructor(parent, AccessLevel.PACKAGE, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, annotationNode);
handleConstructor.generateConstructor(parent, job.accessConstructor, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, annotationNode);

buildMethodReturnType = namePlusTypeParamsToTypeReference(parent.getTreeMaker(), parent, td.typarams);
job.typeParams = job.builderTypeParams = td.typarams;
Expand Down Expand Up @@ -464,7 +470,7 @@ static class BuilderFieldData {
}

if (constructorExists(job.builderType) == MemberExistsResult.NOT_EXISTS) {
JCMethodDecl cd = HandleConstructor.createConstructor(AccessLevel.PACKAGE, List.<JCAnnotation>nil(), job.builderType, List.<JavacNode>nil(), false, annotationNode);
JCMethodDecl cd = HandleConstructor.createConstructor(job.accessConstructor, List.<JCAnnotation>nil(), job.builderType, List.<JavacNode>nil(), false, annotationNode);
if (cd != null) injectMethod(job.builderType, cd);
}

Expand Down