diff options
author | Jan Rieke <rieke@subshell.com> | 2018-09-24 17:48:03 +0200 |
---|---|---|
committer | Jan Rieke <rieke@subshell.com> | 2018-09-24 18:02:48 +0200 |
commit | a454fd74cf040b3aa4f061377478d784bd2157ff (patch) | |
tree | 501dc4e0e5b1eed7287981d8ce3f180e44e04dab /src/core | |
parent | 80af03a3e48456e14b1b3da250047c531f460bb4 (diff) | |
download | lombok-a454fd74cf040b3aa4f061377478d784bd2157ff.tar.gz lombok-a454fd74cf040b3aa4f061377478d784bd2157ff.tar.bz2 lombok-a454fd74cf040b3aa4f061377478d784bd2157ff.zip |
SuperBuilder: allow customization of selected methods
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSuperBuilder.java | 35 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSuperBuilder.java | 34 |
2 files changed, 57 insertions, 12 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 97e38904..9a3275c2 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -259,14 +259,28 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { generateBuilderBasedConstructor(tdParent, typeParams, builderFields, annotationNode, builderClassName, superclassBuilderClass != null); - // Create the abstract builder class. + // Create the abstract builder class, or reuse an existing one. EclipseNode builderType = findInnerClass(tdParent, builderClassName); if (builderType == null) { builderType = generateBuilderAbstractClass(tdParent, builderClassName, superclassBuilderClass, typeParams, ast, classGenericName, builderGenericName); } else { - annotationNode.addError("@SuperBuilder does not support customized builders. Use @Builder instead."); - return; + TypeDeclaration builderTypeDeclaration = (TypeDeclaration) builderType.get(); + if ((builderTypeDeclaration.modifiers & (ClassFileConstants.AccStatic | ClassFileConstants.AccAbstract)) == 0) { + annotationNode.addError("Existing Builder must be an abstract static inner class."); + return; + } + sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(builderType, annotationNode); + // Generate errors for @Singular BFDs that have one already defined node. + for (BuilderFieldData bfd : builderFields) { + SingularData sd = bfd.singularData; + if (sd == null) continue; + EclipseSingularizer singularizer = sd.getSingularizer(); + if (singularizer == null) continue; + if (singularizer.checkForAlreadyExistingNodesAndGenerateError(builderType, sd)) { + bfd.singularData = null; + } + } } // Check validity of @ObtainVia fields, and add check if adding cleaning for @Singular is necessary. @@ -338,13 +352,18 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { return; } - // Create the builder implementation class. + // Create the builder implementation class, or reuse an existing one. EclipseNode builderImplType = findInnerClass(tdParent, builderImplClassName); if (builderImplType == null) { builderImplType = generateBuilderImplClass(tdParent, builderImplClassName, builderClassName, typeParams, ast); } else { - annotationNode.addError("@SuperBuilder does not support customized builders. Use @Builder instead."); - return; + TypeDeclaration builderImplTypeDeclaration = (TypeDeclaration) builderImplType.get(); + if ((builderImplTypeDeclaration.modifiers & ClassFileConstants.AccAbstract) != 0 || + (builderImplTypeDeclaration.modifiers & ClassFileConstants.AccStatic) == 0) { + annotationNode.addError("Existing BuilderImpl must be a non-abstract static inner class."); + return; + } + sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(builderImplType, annotationNode); } if (toBuilder) { @@ -362,7 +381,9 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { // Create the self() and build() methods in the BuilderImpl. injectMethod(builderImplType, generateSelfMethod(builderImplType, typeParams, p)); - injectMethod(builderImplType, generateBuildMethod(tdParent, buildMethodName, returnType, ast)); + if (methodExists(buildMethodName, builderImplType, -1) == MemberExistsResult.NOT_EXISTS) { + injectMethod(builderImplType, generateBuildMethod(tdParent, buildMethodName, returnType, ast)); + } // Add the builder() method to the annotated class. if (methodExists(builderMethodName, tdParent, -1) == MemberExistsResult.NOT_EXISTS) { diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index c84988ea..8af0a7c0 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -27,6 +27,8 @@ import static lombok.javac.handlers.JavacHandlerUtil.*; import java.util.ArrayList; +import javax.lang.model.element.Modifier; + import org.mangosdk.spi.ProviderFor; import com.sun.tools.javac.code.BoundKind; @@ -227,8 +229,23 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { builderType = generateBuilderAbstractClass(annotationNode, tdParent, builderClassName, superclassBuilderClassExpression, typeParams, superclassTypeParams, classGenericName, builderGenericName); } else { - annotationNode.addError("@SuperBuilder does not support customized builders. Use @Builder instead."); - return; + JCClassDecl builderTypeDeclaration = (JCClassDecl) builderType.get(); + if (!builderTypeDeclaration.getModifiers().getFlags().contains(Modifier.STATIC) + || !builderTypeDeclaration.getModifiers().getFlags().contains(Modifier.ABSTRACT)) { + annotationNode.addError("Existing Builder must be an abstract static inner class."); + return; + } + sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(builderType, annotationNode); + // Generate errors for @Singular BFDs that have one already defined node. + for (BuilderFieldData bfd : builderFields) { + SingularData sd = bfd.singularData; + if (sd == null) continue; + JavacSingularizer singularizer = sd.getSingularizer(); + if (singularizer == null) continue; + if (singularizer.checkForAlreadyExistingNodesAndGenerateError(builderType, sd)) { + bfd.singularData = null; + } + } } // Generate the fields in the abstract builder class that hold the values for the instance. @@ -281,8 +298,13 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { if (builderImplType == null) { builderImplType = generateBuilderImplClass(annotationNode, tdParent, builderImplClassName, builderClassName, typeParams); } else { - annotationNode.addError("@SuperBuilder does not support customized builders. Use @Builder instead."); - return; + JCClassDecl builderImplTypeDeclaration = (JCClassDecl) builderImplType.get(); + if (!builderImplTypeDeclaration.getModifiers().getFlags().contains(Modifier.STATIC) + || builderImplTypeDeclaration.getModifiers().getFlags().contains(Modifier.ABSTRACT)) { + annotationNode.addError("Existing BuilderImpl must be a non-abstract static inner class."); + return; + } + sanityCheckForMethodGeneratingAnnotationsOnBuilderClass(builderImplType, annotationNode); } // Create a simple constructor for the BuilderImpl class. @@ -291,7 +313,9 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { // Create the self() and build() methods in the BuilderImpl. injectMethod(builderImplType, generateSelfMethod(builderImplType, typeParams)); - injectMethod(builderImplType, generateBuildMethod(buildMethodName, tdParent, builderImplType, thrownExceptions)); + if (methodExists(buildMethodName, builderImplType, -1) == MemberExistsResult.NOT_EXISTS) { + injectMethod(builderImplType, generateBuildMethod(buildMethodName, tdParent, builderImplType, thrownExceptions)); + } recursiveSetGeneratedBy(builderImplType.get(), ast, annotationNode.getContext()); } |