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

#2596 Builder's setter prefix configurable from lombok.config #2850

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -300,6 +300,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 @@ -863,4 +863,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 @@ -320,7 +320,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 @@ -483,7 +483,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 @@ -552,7 +552,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 @@ -599,7 +599,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 @@ -261,7 +261,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 @@ -422,7 +422,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 @@ -489,7 +489,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 @@ -540,7 +540,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 @@ -187,6 +187,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