From 8439cb5428a7dedbfa836b7028db7c7dba61293a Mon Sep 17 00:00:00 2001 From: Jan Rieke Date: Thu, 16 Aug 2018 15:27:54 +0200 Subject: @SuperBuilder adapts @Builder.Default behavior from @Builder as #1347 is fixed now (ecj) --- .../after-ecj/SuperBuilderWithDefaults.java | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'test/transform/resource/after-ecj') diff --git a/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java b/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java index f0e3d8be..731bd9f2 100644 --- a/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java +++ b/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java @@ -36,14 +36,22 @@ public class SuperBuilderWithDefaults { return new Parent(this); } } - private @lombok.Builder.Default long millis = System.currentTimeMillis(); - private @lombok.Builder.Default N numberField = null; + private @lombok.Builder.Default long millis; + private @lombok.Builder.Default N numberField; + private static @java.lang.SuppressWarnings("all") long $default$millis() { + return System.currentTimeMillis(); + } + private static @java.lang.SuppressWarnings("all") N $default$numberField() { + return null; + } protected @java.lang.SuppressWarnings("all") Parent(final ParentBuilder b) { super(); - if (b.millis$set) - this.millis = b.millis; - if (b.numberField$set) - this.numberField = b.numberField; + this.millis = b.millis; + if ((! b.millis$set)) + this.millis = Parent.$default$millis(); + this.numberField = b.numberField; + if ((! b.numberField$set)) + this.numberField = Parent.$default$numberField(); } public static @java.lang.SuppressWarnings("all") ParentBuilder builder() { return new ParentBuilderImpl(); @@ -78,11 +86,15 @@ public class SuperBuilderWithDefaults { return new Child(this); } } - private @lombok.Builder.Default double doubleField = Math.PI; + private @lombok.Builder.Default double doubleField; + private static @java.lang.SuppressWarnings("all") double $default$doubleField() { + return Math.PI; + } protected @java.lang.SuppressWarnings("all") Child(final ChildBuilder b) { super(b); - if (b.doubleField$set) - this.doubleField = b.doubleField; + this.doubleField = b.doubleField; + if ((! b.doubleField$set)) + this.doubleField = Child.$default$doubleField(); } public static @java.lang.SuppressWarnings("all") ChildBuilder builder() { return new ChildBuilderImpl(); -- cgit 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 +++--- .../after-ecj/SuperBuilderWithNonNull.java | 97 ++++++++++++++++++++++ 2 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 test/transform/resource/after-ecj/SuperBuilderWithNonNull.java (limited to 'test/transform/resource/after-ecj') 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); diff --git a/test/transform/resource/after-ecj/SuperBuilderWithNonNull.java b/test/transform/resource/after-ecj/SuperBuilderWithNonNull.java new file mode 100644 index 00000000..106b8326 --- /dev/null +++ b/test/transform/resource/after-ecj/SuperBuilderWithNonNull.java @@ -0,0 +1,97 @@ +import java.util.List; +public class SuperBuilderWithNonNull { + public static @lombok.experimental.SuperBuilder class Parent { + public static abstract @java.lang.SuppressWarnings("all") class ParentBuilder> { + private @java.lang.SuppressWarnings("all") String nonNullParentField; + private @java.lang.SuppressWarnings("all") boolean nonNullParentField$set; + public ParentBuilder() { + super(); + } + protected abstract @java.lang.SuppressWarnings("all") B self(); + public abstract @java.lang.SuppressWarnings("all") C build(); + public @java.lang.SuppressWarnings("all") B nonNullParentField(final String nonNullParentField) { + this.nonNullParentField = nonNullParentField; + nonNullParentField$set = true; + return self(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return (("SuperBuilderWithNonNull.Parent.ParentBuilder(nonNullParentField=" + this.nonNullParentField) + ")"); + } + } + private static final @java.lang.SuppressWarnings("all") class ParentBuilderImpl extends ParentBuilder { + private ParentBuilderImpl() { + super(); + } + protected @java.lang.Override @java.lang.SuppressWarnings("all") ParentBuilderImpl self() { + return this; + } + public @java.lang.Override @java.lang.SuppressWarnings("all") Parent build() { + return new Parent(this); + } + } + final @lombok.NonNull @lombok.Builder.Default String nonNullParentField; + private static @java.lang.SuppressWarnings("all") String $default$nonNullParentField() { + return "default"; + } + protected @java.lang.SuppressWarnings("all") Parent(final ParentBuilder b) { + super(); + if (b.nonNullParentField$set) + this.nonNullParentField = b.nonNullParentField; + else + this.nonNullParentField = Parent.$default$nonNullParentField(); + if ((nonNullParentField == null)) + { + throw new java.lang.NullPointerException("nonNullParentField is marked @NonNull but is null"); + } + } + public static @java.lang.SuppressWarnings("all") ParentBuilder builder() { + return new ParentBuilderImpl(); + } + } + public static @lombok.experimental.SuperBuilder class Child extends Parent { + public static abstract @java.lang.SuppressWarnings("all") class ChildBuilder> extends Parent.ParentBuilder { + private @java.lang.SuppressWarnings("all") String nonNullChildField; + public ChildBuilder() { + super(); + } + protected abstract @java.lang.Override @java.lang.SuppressWarnings("all") B self(); + public abstract @java.lang.Override @java.lang.SuppressWarnings("all") C build(); + public @java.lang.SuppressWarnings("all") B nonNullChildField(final String nonNullChildField) { + this.nonNullChildField = nonNullChildField; + return self(); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { + return (((("SuperBuilderWithNonNull.Child.ChildBuilder(super=" + super.toString()) + ", nonNullChildField=") + this.nonNullChildField) + ")"); + } + } + private static final @java.lang.SuppressWarnings("all") class ChildBuilderImpl extends ChildBuilder { + private ChildBuilderImpl() { + super(); + } + protected @java.lang.Override @java.lang.SuppressWarnings("all") ChildBuilderImpl self() { + return this; + } + public @java.lang.Override @java.lang.SuppressWarnings("all") Child build() { + return new Child(this); + } + } + @lombok.NonNull String nonNullChildField; + protected @java.lang.SuppressWarnings("all") Child(final ChildBuilder b) { + super(b); + this.nonNullChildField = b.nonNullChildField; + if ((nonNullChildField == null)) + { + throw new java.lang.NullPointerException("nonNullChildField is marked @NonNull but is null"); + } + } + public static @java.lang.SuppressWarnings("all") ChildBuilder builder() { + return new ChildBuilderImpl(); + } + } + public SuperBuilderWithNonNull() { + super(); + } + public static void test() { + Child x = Child.builder().nonNullChildField("child").nonNullParentField("parent").build(); + } +} -- cgit From 16fc34f36c77832b2bb7d383105c0b4c4dc5ddb6 Mon Sep 17 00:00:00 2001 From: Jan Rieke Date: Thu, 16 Aug 2018 18:37:34 +0200 Subject: adapted existing tests to changed default assignment generation --- .../resource/after-delombok/SuperBuilderWithDefaults.java | 12 ++++++------ .../resource/after-ecj/SuperBuilderWithDefaults.java | 15 +++++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'test/transform/resource/after-ecj') diff --git a/test/transform/resource/after-delombok/SuperBuilderWithDefaults.java b/test/transform/resource/after-delombok/SuperBuilderWithDefaults.java index 06d88f4d..17707bf5 100644 --- a/test/transform/resource/after-delombok/SuperBuilderWithDefaults.java +++ b/test/transform/resource/after-delombok/SuperBuilderWithDefaults.java @@ -61,10 +61,10 @@ public class SuperBuilderWithDefaults { } @java.lang.SuppressWarnings("all") protected Parent(final ParentBuilder b) { - this.millis = b.millis; - if (!b.millis$set) this.millis = Parent.$default$millis(); - this.numberField = b.numberField; - if (!b.numberField$set) this.numberField = Parent.$default$numberField(); + if (b.millis$set) this.millis = b.millis; + else this.millis = Parent.$default$millis(); + if (b.numberField$set) this.numberField = b.numberField; + else this.numberField = Parent.$default$numberField(); } @java.lang.SuppressWarnings("all") public static ParentBuilder builder() { @@ -120,8 +120,8 @@ public class SuperBuilderWithDefaults { @java.lang.SuppressWarnings("all") protected Child(final ChildBuilder b) { super(b); - this.doubleField = b.doubleField; - if (!b.doubleField$set) this.doubleField = Child.$default$doubleField(); + if (b.doubleField$set) this.doubleField = b.doubleField; + else this.doubleField = Child.$default$doubleField(); } @java.lang.SuppressWarnings("all") public static ChildBuilder builder() { diff --git a/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java b/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java index 731bd9f2..ef0713c5 100644 --- a/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java +++ b/test/transform/resource/after-ecj/SuperBuilderWithDefaults.java @@ -46,11 +46,13 @@ public class SuperBuilderWithDefaults { } protected @java.lang.SuppressWarnings("all") Parent(final ParentBuilder b) { super(); - this.millis = b.millis; - if ((! b.millis$set)) + if (b.millis$set) + this.millis = b.millis; + else this.millis = Parent.$default$millis(); - this.numberField = b.numberField; - if ((! b.numberField$set)) + if (b.numberField$set) + this.numberField = b.numberField; + else this.numberField = Parent.$default$numberField(); } public static @java.lang.SuppressWarnings("all") ParentBuilder builder() { @@ -92,8 +94,9 @@ public class SuperBuilderWithDefaults { } protected @java.lang.SuppressWarnings("all") Child(final ChildBuilder b) { super(b); - this.doubleField = b.doubleField; - if ((! b.doubleField$set)) + if (b.doubleField$set) + this.doubleField = b.doubleField; + else this.doubleField = Child.$default$doubleField(); } public static @java.lang.SuppressWarnings("all") ChildBuilder builder() { -- cgit