Skip to content

Commit

Permalink
[fixes #2011] If you have a field named build or toString, and yo…
Browse files Browse the repository at this point in the history
…u generate a builder, that builder wouldn’t make the build or toString methods because it thinks the builder-setter methods it just generated that so happen to have that name indicate you don’t want lombok to do that.

You really shouldn’t name any fields builder or toString, though.
  • Loading branch information
rzwitserloot committed Jan 29, 2019
1 parent 9608113 commit 3d0beec
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 10 deletions.
13 changes: 9 additions & 4 deletions src/core/lombok/eclipse/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2018 The Project Lombok Authors.
* Copyright (C) 2013-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -87,6 +87,7 @@
import lombok.eclipse.Eclipse;
import lombok.eclipse.EclipseAnnotationHandler;
import lombok.eclipse.EclipseNode;
import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
import lombok.eclipse.handlers.EclipseSingularsRecipes.EclipseSingularizer;
import lombok.eclipse.handlers.EclipseSingularsRecipes.SingularData;
import lombok.eclipse.handlers.HandleConstructor.SkipIfConstructorExists;
Expand Down Expand Up @@ -460,9 +461,13 @@ private static final char[] prefixWith(char[] prefix, char[] name) {
makeSetterMethodsForBuilder(builderType, bfd, annotationNode, fluent, chain);
}

if (methodExists(buildMethodName, builderType, -1) == MemberExistsResult.NOT_EXISTS) {
MethodDeclaration md = generateBuildMethod(tdParent, isStatic, buildMethodName, nameOfStaticBuilderMethod, returnType, builderFields, builderType, thrownExceptions, addCleaning, ast);
if (md != null) injectMethod(builderType, md);
{
MemberExistsResult methodExists = methodExists(buildMethodName, builderType, -1);
if (methodExists == MemberExistsResult.EXISTS_BY_LOMBOK) methodExists = methodExists(buildMethodName, builderType, 0);
if (methodExists == MemberExistsResult.NOT_EXISTS) {
MethodDeclaration md = generateBuildMethod(tdParent, isStatic, buildMethodName, nameOfStaticBuilderMethod, returnType, builderFields, builderType, thrownExceptions, addCleaning, ast);
if (md != null) injectMethod(builderType, md);
}
}

if (methodExists("toString", builderType, 0) == MemberExistsResult.NOT_EXISTS) {
Expand Down
16 changes: 10 additions & 6 deletions src/core/lombok/javac/handlers/HandleBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2018 The Project Lombok Authors.
* Copyright (C) 2013-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -405,11 +405,15 @@ static class BuilderFieldData {
makeSetterMethodsForBuilder(builderType, bfd, annotationNode, fluent, chain);
}

if (methodExists(buildMethodName, builderType, -1) == MemberExistsResult.NOT_EXISTS) {
JCMethodDecl md = generateBuildMethod(tdParent, isStatic, buildMethodName, nameOfBuilderMethod, returnType, builderFields, builderType, thrownExceptions, ast, addCleaning);
if (md != null) {
injectMethod(builderType, md);
recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
{
MemberExistsResult methodExists = methodExists(builderMethodName, builderType, -1);
if (methodExists == MemberExistsResult.EXISTS_BY_LOMBOK) methodExists = methodExists(buildMethodName, builderType, 0);
if (methodExists == MemberExistsResult.NOT_EXISTS) {
JCMethodDecl md = generateBuildMethod(tdParent, isStatic, buildMethodName, nameOfBuilderMethod, returnType, builderFields, builderType, thrownExceptions, ast, addCleaning);
if (md != null) {
injectMethod(builderType, md);
recursiveSetGeneratedBy(md, ast, annotationNode.getContext());
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions test/transform/resource/after-delombok/BuilderWithBadNames.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class BuilderWithBadNames {
String build;
String toString;
@java.lang.SuppressWarnings("all")
BuilderWithBadNames(final String build, final String toString) {
this.build = build;
this.toString = toString;
}
@java.lang.SuppressWarnings("all")
public static class BuilderWithBadNamesBuilder {
@java.lang.SuppressWarnings("all")
private String build;
@java.lang.SuppressWarnings("all")
private String toString;
@java.lang.SuppressWarnings("all")
BuilderWithBadNamesBuilder() {
}
@java.lang.SuppressWarnings("all")
public BuilderWithBadNamesBuilder build(final String build) {
this.build = build;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderWithBadNamesBuilder toString(final String toString) {
this.toString = toString;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderWithBadNames build() {
return new BuilderWithBadNames(build, toString);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "BuilderWithBadNames.BuilderWithBadNamesBuilder(build=" + this.build + ", toString=" + this.toString + ")";
}
}
@java.lang.SuppressWarnings("all")
public static BuilderWithBadNamesBuilder builder() {
return new BuilderWithBadNamesBuilder();
}
}
33 changes: 33 additions & 0 deletions test/transform/resource/after-ecj/BuilderWithBadNames.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public @lombok.Builder class BuilderWithBadNames {
public static @java.lang.SuppressWarnings("all") class BuilderWithBadNamesBuilder {
private @java.lang.SuppressWarnings("all") String build;
private @java.lang.SuppressWarnings("all") String toString;
@java.lang.SuppressWarnings("all") BuilderWithBadNamesBuilder() {
super();
}
public @java.lang.SuppressWarnings("all") BuilderWithBadNamesBuilder build(final String build) {
this.build = build;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderWithBadNamesBuilder toString(final String toString) {
this.toString = toString;
return this;
}
public @java.lang.SuppressWarnings("all") BuilderWithBadNames build() {
return new BuilderWithBadNames(build, toString);
}
public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() {
return (((("BuilderWithBadNames.BuilderWithBadNamesBuilder(build=" + this.build) + ", toString=") + this.toString) + ")");
}
}
String build;
String toString;
@java.lang.SuppressWarnings("all") BuilderWithBadNames(final String build, final String toString) {
super();
this.build = build;
this.toString = toString;
}
public static @java.lang.SuppressWarnings("all") BuilderWithBadNamesBuilder builder() {
return new BuilderWithBadNamesBuilder();
}
}
5 changes: 5 additions & 0 deletions test/transform/resource/before/BuilderWithBadNames.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@lombok.Builder
public class BuilderWithBadNames {
String build;
String toString;
}

0 comments on commit 3d0beec

Please sign in to comment.