From 76f6013b5950b64dbc776d6ddebdca439653381b Mon Sep 17 00:00:00 2001 From: Jan Rieke Date: Thu, 16 Aug 2018 18:31:19 +0200 Subject: do null checks after assignment so that default==null is covered; only assign value once so that final fields work (ecj) --- .../eclipse/handlers/HandleSuperBuilder.java | 27 ++++++++++------------ 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java index 8badb04c..52b6345e 100644 --- a/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleSuperBuilder.java @@ -48,7 +48,6 @@ import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.IfStatement; import org.eclipse.jdt.internal.compiler.ast.MessageSend; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; -import org.eclipse.jdt.internal.compiler.ast.OperatorIds; import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; @@ -61,7 +60,6 @@ import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeParameter; import org.eclipse.jdt.internal.compiler.ast.TypeReference; -import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; import org.eclipse.jdt.internal.compiler.ast.Wildcard; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.lookup.ClassScope; @@ -456,7 +454,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { constructor.arguments = new Argument[] {new Argument("b".toCharArray(), p, builderType, Modifier.FINAL)}; List statements = new ArrayList(); - List nullChecks = new ArrayList(); for (BuilderFieldData fieldNode : builderFields) { char[] fieldName = removePrefixFromField(fieldNode.originalFieldNode); @@ -475,7 +472,6 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { 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 +479,30 @@ public class HandleSuperBuilder extends EclipseAnnotationHandler { 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); -- cgit