mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 06:16:14 +00:00
activate extendable builders explicitly (ensure backwards compatibility)
This commit is contained in:
1 parent
4ec2e5135c
commit
bd51fa8484
3 files changed
+45
-11
No files matched your search
@@ -123,14 +123,29 @@ public @interface Builder {
|
||||
String builderClassName() default "";
|
||||
|
||||
/**
|
||||
* If true, the generated builder class will extend the builder of the
|
||||
* If true, the generated builder class will extend the {@code @Builder} of the
|
||||
* superclass. In this way, the builder will also contain methods for fields
|
||||
* from the superclass. Note that both this builder and the superclass'
|
||||
* builder must be a type {@code @Builder}; this feature does neither work
|
||||
* for constructor nor method {@code @Builder}s.
|
||||
* from the superclass.<br>
|
||||
* Note that both this builder and the superclass' builder must be a type
|
||||
* {@code @Builder}; this feature does neither work for constructor nor
|
||||
* method {@code @Builder}s. The parent {@code @Builder} must be
|
||||
* {@link #extendable()}. <br>
|
||||
* Implies {@link #extendable()}.
|
||||
*/
|
||||
boolean inherit() default false;
|
||||
|
||||
/**
|
||||
* If true, the generated builder pattern will be extendable by
|
||||
* {@code @Builder}s of subclasses, using the {@link #inherit()} feature.
|
||||
* This is achieved by generating a special constructor that takes a builder
|
||||
* instance as parameter (instead of an {@link AllArgsConstructor}). <br>
|
||||
* Note that this builder must be a type {@code @Builder}; this feature does
|
||||
* neither work for constructor nor method {@code @Builder}s.<br>
|
||||
* If {@link #inherit()} {@code == true}, {@link #extendable()} will
|
||||
* automatically be {@code true}.
|
||||
*/
|
||||
boolean extendable() default false;
|
||||
|
||||
/**
|
||||
* Name of the builder class in the superclass. Only relevant if
|
||||
* {@code inherit = true} (see {@link #inherit()}).
|
||||
|
||||
@@ -141,6 +141,7 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
String builderClassName = builderInstance.builderClassName();
|
||||
|
||||
boolean inherit = builderInstance.inherit();
|
||||
boolean extendable = inherit || builderInstance.extendable(); // inherit implies extendable
|
||||
String superclassBuilderClassName = builderInstance.superclassBuilderClassName();
|
||||
|
||||
String toBuilderMethodName = "toBuilder";
|
||||
@@ -203,9 +204,9 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
superclassBuilderClassName = new String(td.superclass.getLastToken()) + "Builder";
|
||||
}
|
||||
|
||||
boolean callSuperConstructor = inherit && td.superclass != null;
|
||||
boolean callBuilderBasedSuperConstructor = inherit && td.superclass != null;
|
||||
new HandleConstructor().generateConstructor(tdParent, AccessLevel.PROTECTED, allFields, false, null, SkipIfConstructorExists.I_AM_BUILDER, true,
|
||||
Collections.<Annotation>emptyList(), annotationNode, builderClassName, callSuperConstructor);
|
||||
Collections.<Annotation>emptyList(), annotationNode, extendable ? builderClassName : null, callBuilderBasedSuperConstructor);
|
||||
|
||||
returnType = namePlusTypeParamsToTypeReference(td.name, td.typeParameters, p);
|
||||
typeParams = td.typeParameters;
|
||||
@@ -216,6 +217,10 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
annotationNode.addError("@Builder(inherit=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
if (extendable) {
|
||||
annotationNode.addError("@Builder(extendable=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
ConstructorDeclaration cd = (ConstructorDeclaration) parent.get();
|
||||
if (cd.typeParameters != null && cd.typeParameters.length > 0) {
|
||||
annotationNode.addError("@Builder is not supported on constructors with constructor type parameters.");
|
||||
@@ -234,6 +239,10 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
annotationNode.addError("@Builder(inherit=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
if (extendable) {
|
||||
annotationNode.addError("@Builder(extendable=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
MethodDeclaration md = (MethodDeclaration) parent.get();
|
||||
tdParent = parent.up();
|
||||
isStatic = md.isStatic();
|
||||
@@ -427,7 +436,7 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> {
|
||||
}
|
||||
|
||||
if (methodExists(buildMethodName, builderType, -1) == MemberExistsResult.NOT_EXISTS) {
|
||||
boolean useBuilderBasedConstructor = parent.get() instanceof TypeDeclaration;
|
||||
boolean useBuilderBasedConstructor = parent.get() instanceof TypeDeclaration && extendable;
|
||||
MethodDeclaration md = generateBuildMethod(isStatic, buildMethodName, nameOfStaticBuilderMethod, returnType, builderFields, builderType, thrownExceptions, addCleaning, ast, useBuilderBasedConstructor);
|
||||
if (md != null) injectMethod(builderType, md);
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
String toBuilderMethodName = "toBuilder";
|
||||
|
||||
boolean inherit = builderInstance.inherit();
|
||||
boolean extendable = inherit || builderInstance.extendable(); // inherit implies extendable
|
||||
String superclassBuilderClassName = builderInstance.superclassBuilderClassName();
|
||||
|
||||
boolean toBuilder = builderInstance.toBuilder();
|
||||
@@ -176,8 +177,8 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
superclassBuilderClassName = extendsClause + "Builder";
|
||||
}
|
||||
|
||||
boolean callSuperConstructor = inherit && extendsClause != null;
|
||||
new HandleConstructor().generateConstructor(tdParent, AccessLevel.PROTECTED, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, null, annotationNode, builderClassName, callSuperConstructor);
|
||||
boolean callBuilderBasedSuperConstructor = inherit && extendsClause != null;
|
||||
new HandleConstructor().generateConstructor(tdParent, AccessLevel.PROTECTED, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, null, annotationNode, extendable ? builderClassName : null, callBuilderBasedSuperConstructor);
|
||||
|
||||
returnType = namePlusTypeParamsToTypeReference(tdParent.getTreeMaker(), td.name, td.typarams);
|
||||
typeParams = td.typarams;
|
||||
@@ -187,7 +188,12 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
if (inherit) {
|
||||
annotationNode.addError("@Builder(inherit=true) is only supported for type builders.");
|
||||
return;
|
||||
} JCMethodDecl jmd = (JCMethodDecl) fillParametersFrom.get();
|
||||
}
|
||||
if (extendable) {
|
||||
annotationNode.addError("@Builder(extendable=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
JCMethodDecl jmd = (JCMethodDecl) fillParametersFrom.get();
|
||||
if (!jmd.typarams.isEmpty()) {
|
||||
annotationNode.addError("@Builder is not supported on constructors with constructor type parameters.");
|
||||
return;
|
||||
@@ -205,6 +211,10 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
annotationNode.addError("@Builder(inherit=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
if (extendable) {
|
||||
annotationNode.addError("@Builder(extendable=true) is only supported for type builders.");
|
||||
return;
|
||||
}
|
||||
tdParent = parent.up();
|
||||
JCClassDecl td = (JCClassDecl) tdParent.get();
|
||||
JCMethodDecl jmd = (JCMethodDecl) fillParametersFrom.get();
|
||||
@@ -387,7 +397,7 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> {
|
||||
}
|
||||
|
||||
if (methodExists(buildMethodName, builderType, -1) == MemberExistsResult.NOT_EXISTS) {
|
||||
boolean useBuilderBasedConstructor = parent.get() instanceof JCClassDecl;
|
||||
boolean useBuilderBasedConstructor = parent.get() instanceof JCClassDecl && extendable;
|
||||
JCMethodDecl md = generateBuildMethod(isStatic, buildMethodName, nameOfBuilderMethod, returnType, builderFields, builderType, thrownExceptions, ast, addCleaning, useBuilderBasedConstructor);
|
||||
if (md != null) injectMethod(builderType, md);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user