diff options
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleBuilder.java | 42 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleBuilder.java | 30 |
2 files changed, 24 insertions, 48 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 3d4a7a66..3795c3de 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -37,7 +37,6 @@ import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration; import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.Argument; -import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.Assignment; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; @@ -56,11 +55,9 @@ import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; -import org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; -import org.eclipse.jdt.internal.compiler.ast.StringLiteral; import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TrueLiteral; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; @@ -213,8 +210,8 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { superclassBuilderClassName = new String(td.superclass.getLastToken()) + "Builder"; } - boolean callBuilderBasedSuperConstructor = inherit && td.superclass != null; if (extendable) { + boolean callBuilderBasedSuperConstructor = td.superclass != null; generateBuilderBasedConstructor(tdParent, builderFields, annotationNode, builderClassName, callBuilderBasedSuperConstructor); } else { @@ -539,13 +536,21 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { } /** + * Generates a constructor that has a builder as the only parameter. + * The values from the builder are used to initialize the fields of new instances. + * + * @param typeNode + * the type (with the {@code @Builder} annotation) for which a + * constructor should be generated. + * @param builderFields a list of fields in the builder which should be assigned to new instances. + * @param sourceNode the annotation (used for setting source code locations for the generated code). * @param builderClassnameAsParameter - * if {@code != null}, the only parameter of the constructor will + * If {@code != null}, the only parameter of the constructor will * be a builder with this classname; the constructor will then * use the values within this builder to assign the fields of new * instances. * @param callBuilderBasedSuperConstructor - * if {@code true}, the constructor will explicitly call a super + * If {@code true}, the constructor will explicitly call a super * constructor with the builder as argument. Requires * {@code builderClassAsParameter != null}. */ @@ -622,31 +627,6 @@ public class HandleBuilder extends EclipseAnnotationHandler<Builder> { constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]); constructor.arguments = new Argument[] {new Argument("b".toCharArray(), p, new SingleTypeReference(builderClassnameAsParameter.toCharArray(), p), Modifier.FINAL)}; - boolean suppressConstructorProperties = Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES)); - if (!suppressConstructorProperties) { - // Add ConstructorProperties - long[] poss = new long[3]; - Arrays.fill(poss, p); - QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(HandleConstructor.JAVA_BEANS_CONSTRUCTORPROPERTIES, poss); - setGeneratedBy(constructorPropertiesType, source); - SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, source.sourceStart); - ann.declarationSourceEnd = source.sourceEnd; - - ArrayInitializer fieldNames = new ArrayInitializer(); - fieldNames.sourceStart = source.sourceStart; - fieldNames.sourceEnd = source.sourceEnd; - fieldNames.expressions = new Expression[1]; - - fieldNames.expressions[0] = new StringLiteral("b".toCharArray(), source.sourceStart, source.sourceEnd, 0); - setGeneratedBy(fieldNames.expressions[0], source); - - ann.memberValue = fieldNames; - setGeneratedBy(ann, source); - setGeneratedBy(ann.memberValue, source); - Annotation[] constructorProperties = new Annotation[] { ann }; - constructor.annotations = copyAnnotations(source, constructorProperties); - } - constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope); injectMethod(typeNode, constructor); diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index 42e76cf1..9860fb07 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -175,8 +175,8 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { superclassBuilderClassName = extendsClause + "Builder"; } - boolean callBuilderBasedSuperConstructor = inherit && extendsClause != null; if (extendable) { + boolean callBuilderBasedSuperConstructor = extendsClause != null; generateBuilderBasedConstructor(tdParent, builderFields, annotationNode, builderClassName, callBuilderBasedSuperConstructor); } else { new HandleConstructor().generateConstructor(tdParent, AccessLevel.PROTECTED, List.<JCAnnotation>nil(), allFields.toList(), false, null, SkipIfConstructorExists.I_AM_BUILDER, annotationNode); @@ -510,13 +510,20 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { /** * Generates a constructor that has a builder as the only parameter. - * + * The values from the builder are used to initialize the fields of new instances. + * + * @param typeNode + * the type (with the {@code @Builder} annotation) for which a + * constructor should be generated. + * @param builderFields a list of fields in the builder which should be assigned to new instances. + * @param source the annotation (used for setting source code locations for the generated code). * @param builderClassnameAsParameter - * the only parameter of the constructor will be a builder with - * this classname; the constructor will use the values within - * this builder to assign the fields of new instances. + * If {@code != null}, the only parameter of the constructor will + * be a builder with this classname; the constructor will then + * use the values within this builder to assign the fields of new + * instances. * @param callBuilderBasedSuperConstructor - * if {@code true}, the constructor will explicitly call a super + * If {@code true}, the constructor will explicitly call a super * constructor with the builder as argument. Requires * {@code builderClassAsParameter != null}. */ @@ -562,17 +569,6 @@ public class HandleBuilder extends JavacAnnotationHandler<Builder> { JCModifiers mods = maker.Modifiers(toJavacModifier(level), List.<JCAnnotation>nil()); - boolean suppressConstructorProperties = Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES)); - if (!suppressConstructorProperties) { - // Add ConstructorProperties - JCExpression constructorPropertiesType = chainDots(typeNode, "java", "beans", "ConstructorProperties"); - ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>(); - fieldNames.append(maker.Literal(builderVariableName.toString())); - JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList()); - JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray)); - mods.annotations = mods.annotations.append(annotation); - } - // Create a constructor that has just the builder as parameter. ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); |