diff options
author | Jan Rieke <it@janrieke.de> | 2020-05-01 23:56:17 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2020-05-28 21:18:35 +0200 |
commit | 48b1f17f7ef0e0b3d90dee74ee1b490a02cacff2 (patch) | |
tree | 1e7d52d4a53b3e9e9b8ba73edc99421a03102d24 /src/core/lombok/javac/handlers/HandleSuperBuilder.java | |
parent | faa70bd21d2872e8354574cc98c7aa82ab996bcd (diff) | |
download | lombok-48b1f17f7ef0e0b3d90dee74ee1b490a02cacff2.tar.gz lombok-48b1f17f7ef0e0b3d90dee74ee1b490a02cacff2.tar.bz2 lombok-48b1f17f7ef0e0b3d90dee74ee1b490a02cacff2.zip |
[SuperBuilder] allow constructor customization
Diffstat (limited to 'src/core/lombok/javac/handlers/HandleSuperBuilder.java')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSuperBuilder.java | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index 2dc3247d..b7cd6f9a 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -354,8 +354,10 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { } // Generate a constructor in the annotated class that takes a builder as argument. - generateBuilderBasedConstructor(cfv, tdParent, typeParams, builderFields, annotationNode, builderClassName, - superclassBuilderClassExpression != null); + if (!constructorExists(tdParent, builderClassName)) { + generateBuilderBasedConstructor(cfv, tdParent, typeParams, builderFields, annotationNode, builderClassName, + superclassBuilderClassExpression != null); + } if (isAbstract) { // Only non-abstract classes get the builder() and toBuilder() methods. @@ -1070,4 +1072,33 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { } return typeParameter; } + + /** + * Checks if there is a manual constructor in the given type with a single parameter (builder). + */ + private boolean constructorExists(JavacNode type, String builderClassName) { + if (type != null && type.get() instanceof JCClassDecl) { + for (JCTree def : ((JCClassDecl)type.get()).defs) { + if (def instanceof JCMethodDecl) { + JCMethodDecl md = (JCMethodDecl) def; + String name = md.name.toString(); + boolean matches = name.equals("<init>"); + if (isTolerate(type, md)) + continue; + if (matches && md.params != null && md.params.length() == 1) { + // Cannot use typeMatches() here, because the parameter could be fully-qualified, partially-qualified, or not qualified. + // A string-compare of the last part should work. If it's a false-positive, users could still @Tolerate it. + String typeName = md.params.get(0).getType().toString(); + int lastIndexOfDot = typeName.lastIndexOf('.'); + if (lastIndexOfDot >= 0) { + typeName = typeName.substring(lastIndexOfDot+1); + } + if ((builderClassName+"<?, ?>").equals(typeName)) + return true; + } + } + } + } + return false; + } } |