diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-03-25 23:11:48 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-03-25 23:11:48 +0100 |
commit | 228e99fe5203e92c7297325fec69a82abc1a4bd7 (patch) | |
tree | d3176a94f672a58f9c9927d812d048323080f363 /src/core/lombok/javac | |
parent | 535e20972c56e65b598df0d0849a25daa7a69e8d (diff) | |
download | lombok-228e99fe5203e92c7297325fec69a82abc1a4bd7.tar.gz lombok-228e99fe5203e92c7297325fec69a82abc1a4bd7.tar.bz2 lombok-228e99fe5203e92c7297325fec69a82abc1a4bd7.zip |
[fixes #2046] you can now suppress the builder() method, useful if you only want toBuilder(). Also suppresses the warnings about any missing Builder.Default annotations.
Diffstat (limited to 'src/core/lombok/javac')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleBuilder.java | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 609adbd6..2ef2fac2 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -119,7 +119,15 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { if (buildMethodName == null) buildMethodName = "build"; if (builderClassName == null) builderClassName = ""; - if (!checkName("builderMethodName", builderMethodName, annotationNode)) return; + boolean generateBuilderMethod; + if (builderMethodName.isEmpty()) { + generateBuilderMethod = false; + } else if (!checkName("builderMethodName", builderMethodName, annotationNode)) { + return; + } else { + generateBuilderMethod = true; + } + if (!checkName("buildMethodName", buildMethodName, annotationNode)) return; if (!builderClassName.isEmpty()) { if (!checkName("builderClassName", builderClassName, annotationNode)) return; @@ -140,6 +148,8 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { boolean addCleaning = false; boolean isStatic = true; + ArrayList<JavacNode> nonFinalNonDefaultedFields = null; + if (parent.get() instanceof JCClassDecl) { tdParent = parent; JCClassDecl td = (JCClassDecl) tdParent.get(); @@ -172,7 +182,8 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { if (fd.init != null && isDefault == null) { if (isFinal) continue; - fieldNode.addWarning("@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final."); + if (nonFinalNonDefaultedFields == null) nonFinalNonDefaultedFields = new ArrayList<JavacNode>(); + nonFinalNonDefaultedFields.add(fieldNode); } if (isDefault != null) { @@ -431,7 +442,8 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { if (addCleaning) injectMethod(builderType, generateCleanMethod(builderFields, builderType, ast)); - if (methodExists(builderMethodName, tdParent, -1) == MemberExistsResult.NOT_EXISTS) { + if (generateBuilderMethod && methodExists(builderMethodName, tdParent, -1) != MemberExistsResult.NOT_EXISTS) generateBuilderMethod = false; + if (generateBuilderMethod) { JCMethodDecl md = generateBuilderMethod(isStatic, builderMethodName, builderClassName, annotationNode, tdParent, typeParams); recursiveSetGeneratedBy(md, ast, annotationNode.getContext()); if (md != null) injectMethod(tdParent, md); @@ -459,6 +471,12 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { } } } + + if (nonFinalNonDefaultedFields != null && generateBuilderMethod) { + for (JavacNode fieldNode : nonFinalNonDefaultedFields) { + fieldNode.addWarning("@Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final."); + } + } } private static String unpack(JCExpression expr) { |