diff options
author | Roel Spilker <r.spilker@gmail.com> | 2018-08-27 21:36:41 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2018-08-27 21:36:41 +0200 |
commit | 0cb87873c2bc75478a5caf651af6148ed1d7e412 (patch) | |
tree | 49fbeb4ef47948bb678c2a55587246e0dc7758ad /src/core | |
parent | ecf754c87d3aaf1f788c9f00e5815ce7174f3722 (diff) | |
parent | 16fc34f36c77832b2bb7d383105c0b4c4dc5ddb6 (diff) | |
download | lombok-0cb87873c2bc75478a5caf651af6148ed1d7e412.tar.gz lombok-0cb87873c2bc75478a5caf651af6148ed1d7e412.tar.bz2 lombok-0cb87873c2bc75478a5caf651af6148ed1d7e412.zip |
Merge branch 'superBuilderNonNull' of https://github.com/janrieke/lombok into janrieke-superBuilderNonNull
# Conflicts:
# src/core/lombok/eclipse/handlers/HandleSuperBuilder.java
# src/core/lombok/javac/handlers/HandleSuperBuilder.java
# test/transform/resource/after-delombok/SuperBuilderWithDefaults.java
# test/transform/resource/after-ecj/SuperBuilderWithDefaults.java
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSuperBuilder.java | 25 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSuperBuilder.java | 22 |
2 files changed, 23 insertions, 24 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 8badb04c..a0102220 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -456,7 +456,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { constructor.arguments = new Argument[] {new Argument("b".toCharArray(), p, builderType, Modifier.FINAL)}; List<Statement> statements = new ArrayList<Statement>(); - List<Statement> nullChecks = new ArrayList<Statement>(); for (BuilderFieldData fieldNode : builderFields) { char[] fieldName = removePrefixFromField(fieldNode.originalFieldNode); @@ -475,7 +474,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { assignmentExpr = new QualifiedNameReference(variableInBuilder, positions, s, e); } Statement assignment = new Assignment(fieldInThis, assignmentExpr, (int) p); - statements.add(assignment); // In case of @Builder.Default, set the value to the default if it was NOT set in the builder. if (fieldNode.nameOfSetFlag != null) { @@ -483,29 +481,30 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler<SuperBuilder> { long[] positions = new long[] {p, p}; QualifiedNameReference setVariableInBuilderRef = new QualifiedNameReference(setVariableInBuilder, positions, s, e); - MessageSend inv = new MessageSend(); - inv.sourceStart = source.sourceStart; - inv.sourceEnd = source.sourceEnd; - inv.receiver = new SingleNameReference(((TypeDeclaration) typeNode.get()).name, 0L); - inv.selector = fieldNode.nameOfDefaultProvider; - inv.typeArguments = typeParameterNames(((TypeDeclaration) typeNode.get()).typeParameters); + MessageSend defaultMethodCall = new MessageSend(); + defaultMethodCall.sourceStart = source.sourceStart; + defaultMethodCall.sourceEnd = source.sourceEnd; + defaultMethodCall.receiver = new SingleNameReference(((TypeDeclaration) typeNode.get()).name, 0L); + defaultMethodCall.selector = fieldNode.nameOfDefaultProvider; + defaultMethodCall.typeArguments = typeParameterNames(((TypeDeclaration) typeNode.get()).typeParameters); - assignment = new Assignment(fieldInThis, inv, (int) p); - IfStatement ifBlockForDefault = new IfStatement(new UnaryExpression(setVariableInBuilderRef, OperatorIds.NOT), assignment, s, e); + Statement defaultAssignment = new Assignment(fieldInThis, defaultMethodCall, (int) p); + IfStatement ifBlockForDefault = new IfStatement(setVariableInBuilderRef, assignment, defaultAssignment, s, e); statements.add(ifBlockForDefault); + } else { + statements.add(assignment); } Annotation[] nonNulls = findAnnotations((FieldDeclaration)fieldNode.originalFieldNode.get(), NON_NULL_PATTERN); if (nonNulls.length != 0) { Statement nullCheck = generateNullCheck((FieldDeclaration)fieldNode.originalFieldNode.get(), sourceNode); if (nullCheck != null) { - nullChecks.add(nullCheck); + statements.add(nullCheck); } } } - nullChecks.addAll(statements); - constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]); + constructor.statements = statements.isEmpty() ? null : statements.toArray(new Statement[statements.size()]); constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope); diff --git a/src/core/lombok/javac/handlers/HandleSuperBuilder.java b/src/core/lombok/javac/handlers/HandleSuperBuilder.java index c00bd66d..aaf94dd8 100644 --- a/src/core/lombok/javac/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/javac/handlers/HandleSuperBuilder.java @@ -406,17 +406,10 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { AccessLevel level = AccessLevel.PROTECTED; - ListBuffer<JCStatement> nullChecks = new ListBuffer<JCStatement>(); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); Name builderVariableName = typeNode.toName("b"); for (BuilderFieldData bfd : builderFields) { - List<JCAnnotation> nonNulls = findAnnotations(bfd.originalFieldNode, NON_NULL_PATTERN); - if (!nonNulls.isEmpty()) { - JCStatement nullCheck = generateNullCheck(maker, bfd.originalFieldNode, source); - if (nullCheck != null) nullChecks.append(nullCheck); - } - JCExpression rhs; if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) { bfd.singularData.getSingularizer().appendBuildCode(bfd.singularData, bfd.originalFieldNode, bfd.type, statements, bfd.name, "b"); @@ -427,14 +420,21 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { JCFieldAccess fieldInThis = maker.Select(maker.Ident(typeNode.toName("this")), bfd.rawName); JCStatement assign = maker.Exec(maker.Assign(fieldInThis, rhs)); - statements.append(assign); - // In case of @Builder.Default, set the value to the default if it was NOT set in the builder. + // In case of @Builder.Default, set the value to the default if it was not set in the builder. if (bfd.nameOfSetFlag != null) { JCFieldAccess setField = maker.Select(maker.Ident(builderVariableName), bfd.nameOfSetFlag); fieldInThis = maker.Select(maker.Ident(typeNode.toName("this")), bfd.rawName); JCAssign assignDefault = maker.Assign(fieldInThis, maker.Apply(typeParameterNames(maker, ((JCClassDecl) typeNode.get()).typarams), maker.Select(maker.Ident(((JCClassDecl) typeNode.get()).name), bfd.nameOfDefaultProvider), List.<JCExpression>nil())); - statements.append(maker.If(maker.Unary(CTC_NOT, setField), maker.Exec(assignDefault), null)); + statements.append(maker.If(setField, assign, maker.Exec(assignDefault))); + } else { + statements.append(assign); + } + + List<JCAnnotation> nonNulls = findAnnotations(bfd.originalFieldNode, NON_NULL_PATTERN); + if (!nonNulls.isEmpty()) { + JCStatement nullCheck = generateNullCheck(maker, bfd.originalFieldNode, source); + if (nullCheck != null) statements.append(nullCheck); } } @@ -465,7 +465,7 @@ public class HandleSuperBuilder extends JavacAnnotationHandler<SuperBuilder> { JCMethodDecl constr = recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("<init>"), null, List.<JCTypeParameter>nil(), params.toList(), List.<JCExpression>nil(), - maker.Block(0L, nullChecks.appendList(statements).toList()), null), source.get(), typeNode.getContext()); + maker.Block(0L, statements.toList()), null), source.get(), typeNode.getContext()); injectMethod(typeNode, constr, null, Javac.createVoidType(typeNode.getSymbolTable(), CTC_VOID)); } |