Skip to content

Commit

Permalink
#2596 Builder's setter prefix configurable from lombok.config
Browse files Browse the repository at this point in the history
  • Loading branch information
sadv1r committed May 20, 2021
1 parent 6758714 commit 384aec3
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 8 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ DaveLaw <[email protected]>
Dave Brosius <[email protected]>
Dawid Rusin <[email protected]>
Denis Stepanov <[email protected]>
Dmitry Ivanov <[email protected]>
Emil Lundberg <[email protected]>
Enrique da Costa Cambio <[email protected]>
Jacob Middag <[email protected]>
Expand Down
7 changes: 7 additions & 0 deletions src/core/lombok/ConfigurationKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ private ConfigurationKeys() {}
*/
public static final ConfigurationKey<String> BUILDER_CLASS_NAME = new ConfigurationKey<String>("lombok.builder.className", "Default name of the generated builder class. A * is replaced with the name of the relevant type (default = *Builder).") {};

/**
* lombok configuration: {@code lombok.builder.setterPrefix} = &lt;String: prefix&gt;.
*
* For any usage of the {@code @Builder} annotation without an explicit {@code setterPrefix} parameter, this prefix is used.
*/
public static final ConfigurationKey<String> BUILDER_SETTER_PREFIX = new ConfigurationKey<String>("lombok.builder.setterPrefix", "The prefix to prepend to generated @Builder setter method names") {};

/**
* lombok configuration: {@code lombok.builder.flagUsage} = {@code WARNING} | {@code ERROR}.
*
Expand Down
11 changes: 11 additions & 0 deletions src/core/lombok/core/handlers/HandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -845,4 +845,15 @@ public static String getParamJavadoc(String methodComment, String param) {
}
return null;
}

public static String getBuilderSetterPrefix(LombokNode<?, ?, ?> node, String setterPrefix) {
if (setterPrefix != null && !setterPrefix.isEmpty()) {
return setterPrefix;
}

String configurationPrefix = node.getAst().readConfiguration(ConfigurationKeys.BUILDER_SETTER_PREFIX);
if (configurationPrefix != null && !configurationPrefix.isEmpty()) return configurationPrefix;

return setterPrefix;
}
}
8 changes: 4 additions & 4 deletions src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
bfd.builderFieldName = bfd.name;
bfd.annotations = copyAnnotations(fd, findCopyableAnnotations(fieldNode));
bfd.type = fd.type;
bfd.singularData = getSingularData(fieldNode, ast, annInstance.setterPrefix());
bfd.singularData = getSingularData(fieldNode, ast, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
bfd.originalFieldNode = fieldNode;

if (bfd.singularData != null && isDefault != null) {
Expand Down Expand Up @@ -478,7 +478,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
bfd.builderFieldName = bfd.name;
bfd.annotations = copyAnnotations(arg, copyableAnnotations);
bfd.type = arg.type;
bfd.singularData = getSingularData(param, ast, annInstance.setterPrefix());
bfd.singularData = getSingularData(param, ast, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
bfd.originalFieldNode = param;
addObtainVia(bfd, param);
job.builderFields.add(bfd);
Expand Down Expand Up @@ -547,7 +547,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
}

for (BuilderFieldData bfd : job.builderFields) {
makePrefixedSetterMethodsForBuilder(job, bfd, annInstance.setterPrefix());
makePrefixedSetterMethodsForBuilder(job, bfd, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
}

{
Expand Down Expand Up @@ -594,7 +594,7 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
tps[i].name = typeArgsForToBuilder.get(i);
}
}
MethodDeclaration md = generateToBuilderMethod(job, tps, annInstance.setterPrefix());
MethodDeclaration md = generateToBuilderMethod(job, tps, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));

if (md != null) injectMethod(job.parentType, md);
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static class BuilderFieldData {
bfd.builderFieldName = bfd.name;
bfd.annotations = findCopyableAnnotations(fieldNode);
bfd.type = fd.vartype;
bfd.singularData = getSingularData(fieldNode, annInstance.setterPrefix());
bfd.singularData = getSingularData(fieldNode, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
bfd.originalFieldNode = fieldNode;

if (bfd.singularData != null && isDefault != null) {
Expand Down Expand Up @@ -417,7 +417,7 @@ static class BuilderFieldData {
bfd.rawName = raw.name;
bfd.annotations = findCopyableAnnotations(param);
bfd.type = raw.vartype;
bfd.singularData = getSingularData(param, annInstance.setterPrefix());
bfd.singularData = getSingularData(param, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
bfd.originalFieldNode = param;
addObtainVia(bfd, param);
job.builderFields.add(bfd);
Expand Down Expand Up @@ -484,7 +484,7 @@ static class BuilderFieldData {
}

for (BuilderFieldData bfd : job.builderFields) {
makePrefixedSetterMethodsForBuilder(job, bfd, annInstance.setterPrefix());
makePrefixedSetterMethodsForBuilder(job, bfd, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
}

{
Expand Down Expand Up @@ -535,7 +535,7 @@ static class BuilderFieldData {
}
tps = lb.toList();
}
JCMethodDecl md = generateToBuilderMethod(job, tps, annInstance.setterPrefix());
JCMethodDecl md = generateToBuilderMethod(job, tps, getBuilderSetterPrefix(annotationNode, annInstance.setterPrefix()));
if (md != null) {
recursiveSetGeneratedBy(md, annotationNode);
injectMethod(job.parentType, md);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.List;
class BuilderSimpleWithSetterPrefix<T> {
private int unprefixed;
@java.lang.SuppressWarnings("all")
BuilderSimpleWithSetterPrefix(final int unprefixed) {
this.unprefixed = unprefixed;
}
@java.lang.SuppressWarnings("all")
protected static class BuilderSimpleWithSetterPrefixBuilder<T> {
@java.lang.SuppressWarnings("all")
private int unprefixed;
@java.lang.SuppressWarnings("all")
BuilderSimpleWithSetterPrefixBuilder() {
}
/**
* @return {@code this}.
*/
@java.lang.SuppressWarnings("all")
public BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T> withUnprefixed(final int unprefixed) {
this.unprefixed = unprefixed;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderSimpleWithSetterPrefix<T> build() {
return new BuilderSimpleWithSetterPrefix<T>(this.unprefixed);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder(unprefixed=" + this.unprefixed + ")";
}
}
@java.lang.SuppressWarnings("all")
protected static <T> BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T> builder() {
return new BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.List;
@lombok.Builder(access = lombok.AccessLevel.PROTECTED) class BuilderSimpleWithSetterPrefix<T> {
protected static @java.lang.SuppressWarnings("all") class BuilderSimpleWithSetterPrefixBuilder<T> {
private @java.lang.SuppressWarnings("all") int unprefixed;
@java.lang.SuppressWarnings("all") BuilderSimpleWithSetterPrefixBuilder() {
super();
}
/**
* @return {@code this}.
*/
public @java.lang.SuppressWarnings("all") BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T> withUnprefixed(final int unprefixed) {
this.unprefixed = unprefixed;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderSimpleWithSetterPrefix<T> build() {
return new BuilderSimpleWithSetterPrefix<T>(this.unprefixed);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return (("BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder(unprefixed=" + this.unprefixed) + ")");
}
}
private int unprefixed;
@java.lang.SuppressWarnings("all") BuilderSimpleWithSetterPrefix(final int unprefixed) {
super();
this.unprefixed = unprefixed;
}
protected static @java.lang.SuppressWarnings("all") <T>BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T> builder() {
return new BuilderSimpleWithSetterPrefix.BuilderSimpleWithSetterPrefixBuilder<T>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//CONF: lombok.builder.setterPrefix = with

import java.util.List;

@lombok.Builder(access = lombok.AccessLevel.PROTECTED)
class BuilderSimpleWithSetterPrefix<T> {
private int unprefixed;
}
4 changes: 4 additions & 0 deletions website/templates/features/Builder.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ <h3 id="jackson"><a name="jackson">With Jackson</a></h3>
<code>lombok.builder.className</code> = [a java identifier with an optional star to indicate where the return type name goes] (default: <code>*Builder</code>)
</dt><dd>
Unless you explicitly pick the builder's class name with the <code>builderClassName</code> parameter, this name is chosen; any star in the name is replaced with the relevant return type.
</dd><dt>
<code>lombok.builder.setterPrefix</code> = [the prefix to prepend to generated <code>@Builder</code> setter method names] (default: not set)
</dt><dd>
Unless you explicitly pick the builder's setter prefix with the <code>setterPrefix</code> parameter, this prefix is chosen.
</dd><dt>
<code>lombok.builder.flagUsage</code> = [<code>warning</code> | <code>error</code>] (default: not set)
</dt><dd>
Expand Down

0 comments on commit 384aec3

Please sign in to comment.