From 94eadb5131b32b6cf599a3f5445524a08ba0569b Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 15 Oct 2018 21:12:45 +0200 Subject: [fixes #1324] toBuilder with @Singular marked collections that are null no longer throws NPE. --- .../BuilderSingularToBuilderWithNull.java | 4 +- .../after-delombok/BuilderWithToBuilder.java | 77 ++++++---------------- .../BuilderSingularToBuilderWithNull.java | 7 +- .../resource/after-ecj/BuilderWithToBuilder.java | 66 +++++-------------- .../resource/before/BuilderWithToBuilder.java | 15 +---- 5 files changed, 46 insertions(+), 123 deletions(-) (limited to 'test') diff --git a/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java b/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java index b0563858..96ffd2dc 100644 --- a/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java +++ b/test/transform/resource/after-delombok/BuilderSingularToBuilderWithNull.java @@ -58,6 +58,8 @@ class BuilderSingularToBuilderWithNull { } @java.lang.SuppressWarnings("all") public BuilderSingularToBuilderWithNullBuilder toBuilder() { - return new BuilderSingularToBuilderWithNullBuilder().elems(this.elems == null ? java.util.Collections.emptyList() : this.elems); + final BuilderSingularToBuilderWithNullBuilder builder = new BuilderSingularToBuilderWithNullBuilder(); + if (this.elems != null) builder.elems(this.elems); + return builder; } } diff --git a/test/transform/resource/after-delombok/BuilderWithToBuilder.java b/test/transform/resource/after-delombok/BuilderWithToBuilder.java index 8615a40b..53256107 100644 --- a/test/transform/resource/after-delombok/BuilderWithToBuilder.java +++ b/test/transform/resource/after-delombok/BuilderWithToBuilder.java @@ -86,7 +86,9 @@ class BuilderWithToBuilder { } @java.lang.SuppressWarnings("all") public BuilderWithToBuilderBuilder toBuilder() { - return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(this.bars == null ? java.util.Collections.emptyList() : this.bars); + final BuilderWithToBuilderBuilder builder = new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)); + if (this.bars != null) builder.bars(this.bars); + return builder; } } class ConstructorWithToBuilder { @@ -94,15 +96,17 @@ class ConstructorWithToBuilder { private String mTwo; private T foo; @lombok.Singular - private List bars; - public ConstructorWithToBuilder(String mOne, T bar) { + private com.google.common.collect.ImmutableList bars; + public ConstructorWithToBuilder(String mOne, T baz, com.google.common.collect.ImmutableList bars) { } @java.lang.SuppressWarnings("all") public static class ConstructorWithToBuilderBuilder { @java.lang.SuppressWarnings("all") private String mOne; @java.lang.SuppressWarnings("all") - private T bar; + private T baz; + @java.lang.SuppressWarnings("all") + private com.google.common.collect.ImmutableList bars; @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder() { } @@ -112,18 +116,23 @@ class ConstructorWithToBuilder { return this; } @java.lang.SuppressWarnings("all") - public ConstructorWithToBuilderBuilder bar(final T bar) { - this.bar = bar; + public ConstructorWithToBuilderBuilder baz(final T baz) { + this.baz = baz; + return this; + } + @java.lang.SuppressWarnings("all") + public ConstructorWithToBuilderBuilder bars(final com.google.common.collect.ImmutableList bars) { + this.bars = bars; return this; } @java.lang.SuppressWarnings("all") public ConstructorWithToBuilder build() { - return new ConstructorWithToBuilder(mOne, bar); + return new ConstructorWithToBuilder(mOne, baz, bars); } @java.lang.Override @java.lang.SuppressWarnings("all") public java.lang.String toString() { - return "ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne + ", bar=" + this.bar + ")"; + return "ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne + ", baz=" + this.baz + ", bars=" + this.bars + ")"; } } @java.lang.SuppressWarnings("all") @@ -132,54 +141,6 @@ class ConstructorWithToBuilder { } @java.lang.SuppressWarnings("all") public ConstructorWithToBuilderBuilder toBuilder() { - return new ConstructorWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + return new ConstructorWithToBuilderBuilder().mOne(this.mOne).baz(this.foo).bars(this.bars); } -} -class StaticWithToBuilder { - private String mOne; - private String mTwo; - private T foo; - private K bar; - @lombok.Singular - private List bars; - public static StaticWithToBuilder test(String mOne, Z bar) { - return new StaticWithToBuilder(); - } - @java.lang.SuppressWarnings("all") - public static class StaticWithToBuilderBuilder { - @java.lang.SuppressWarnings("all") - private String mOne; - @java.lang.SuppressWarnings("all") - private Z bar; - @java.lang.SuppressWarnings("all") - StaticWithToBuilderBuilder() { - } - @java.lang.SuppressWarnings("all") - public StaticWithToBuilderBuilder mOne(final String mOne) { - this.mOne = mOne; - return this; - } - @java.lang.SuppressWarnings("all") - public StaticWithToBuilderBuilder bar(final Z bar) { - this.bar = bar; - return this; - } - @java.lang.SuppressWarnings("all") - public StaticWithToBuilder build() { - return StaticWithToBuilder.test(mOne, bar); - } - @java.lang.Override - @java.lang.SuppressWarnings("all") - public java.lang.String toString() { - return "StaticWithToBuilder.StaticWithToBuilderBuilder(mOne=" + this.mOne + ", bar=" + this.bar + ")"; - } - } - @java.lang.SuppressWarnings("all") - public static StaticWithToBuilderBuilder builder() { - return new StaticWithToBuilderBuilder(); - } - @java.lang.SuppressWarnings("all") - public StaticWithToBuilderBuilder toBuilder() { - return new StaticWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); - } -} +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java b/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java index bbbf9268..ccd6335a 100644 --- a/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java +++ b/test/transform/resource/after-ecj/BuilderSingularToBuilderWithNull.java @@ -52,6 +52,9 @@ import lombok.Singular; return new BuilderSingularToBuilderWithNullBuilder(); } public @java.lang.SuppressWarnings("all") BuilderSingularToBuilderWithNullBuilder toBuilder() { - return new BuilderSingularToBuilderWithNullBuilder().elems(((this.elems == null) ? java.util.Collections.emptyList() : this.elems)); + final BuilderSingularToBuilderWithNullBuilder builder = new BuilderSingularToBuilderWithNullBuilder(); + if ((this.elems != null)) + builder.elems(this.elems); + return builder; } -} +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/BuilderWithToBuilder.java b/test/transform/resource/after-ecj/BuilderWithToBuilder.java index 636dc42d..420f1583 100644 --- a/test/transform/resource/after-ecj/BuilderWithToBuilder.java +++ b/test/transform/resource/after-ecj/BuilderWithToBuilder.java @@ -74,13 +74,17 @@ import lombok.Builder; return new BuilderWithToBuilderBuilder(); } public @java.lang.SuppressWarnings("all") BuilderWithToBuilderBuilder toBuilder() { - return new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)).bars(((this.bars == null) ? java.util.Collections.emptyList() : this.bars)); + final BuilderWithToBuilderBuilder builder = new BuilderWithToBuilderBuilder().one(this.mOne).two(this.mTwo).foo(BuilderWithToBuilder.rrr(this)); + if ((this.bars != null)) + builder.bars(this.bars); + return builder; } } @lombok.experimental.Accessors(prefix = "m") class ConstructorWithToBuilder { public static @java.lang.SuppressWarnings("all") class ConstructorWithToBuilderBuilder { private @java.lang.SuppressWarnings("all") String mOne; - private @java.lang.SuppressWarnings("all") T bar; + private @java.lang.SuppressWarnings("all") T baz; + private @java.lang.SuppressWarnings("all") com.google.common.collect.ImmutableList bars; @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder() { super(); } @@ -88,68 +92,32 @@ import lombok.Builder; this.mOne = mOne; return this; } - public @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder bar(final T bar) { - this.bar = bar; + public @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder baz(final T baz) { + this.baz = baz; + return this; + } + public @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder bars(final com.google.common.collect.ImmutableList bars) { + this.bars = bars; return this; } public @java.lang.SuppressWarnings("all") ConstructorWithToBuilder build() { - return new ConstructorWithToBuilder(mOne, bar); + return new ConstructorWithToBuilder(mOne, baz, bars); } public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { - return (((("ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne) + ", bar=") + this.bar) + ")"); + return (((((("ConstructorWithToBuilder.ConstructorWithToBuilderBuilder(mOne=" + this.mOne) + ", baz=") + this.baz) + ", bars=") + this.bars) + ")"); } } private String mOne; private String mTwo; private T foo; - private @lombok.Singular List bars; - public @Builder(toBuilder = true) ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T bar) { + private @lombok.Singular com.google.common.collect.ImmutableList bars; + public @Builder(toBuilder = true) ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T baz, com.google.common.collect.ImmutableList bars) { super(); } public static @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder builder() { return new ConstructorWithToBuilderBuilder(); } public @java.lang.SuppressWarnings("all") ConstructorWithToBuilderBuilder toBuilder() { - return new ConstructorWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); - } -} -@lombok.experimental.Accessors(prefix = "m") class StaticWithToBuilder { - public static @java.lang.SuppressWarnings("all") class StaticWithToBuilderBuilder { - private @java.lang.SuppressWarnings("all") String mOne; - private @java.lang.SuppressWarnings("all") Z bar; - @java.lang.SuppressWarnings("all") StaticWithToBuilderBuilder() { - super(); - } - public @java.lang.SuppressWarnings("all") StaticWithToBuilderBuilder mOne(final String mOne) { - this.mOne = mOne; - return this; - } - public @java.lang.SuppressWarnings("all") StaticWithToBuilderBuilder bar(final Z bar) { - this.bar = bar; - return this; - } - public @java.lang.SuppressWarnings("all") StaticWithToBuilder build() { - return StaticWithToBuilder.test(mOne, bar); - } - public @java.lang.Override @java.lang.SuppressWarnings("all") java.lang.String toString() { - return (((("StaticWithToBuilder.StaticWithToBuilderBuilder(mOne=" + this.mOne) + ", bar=") + this.bar) + ")"); - } - } - private String mOne; - private String mTwo; - private T foo; - private K bar; - private @lombok.Singular List bars; - StaticWithToBuilder() { - super(); - } - public static @Builder(toBuilder = true) StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { - return new StaticWithToBuilder(); - } - public static @java.lang.SuppressWarnings("all") StaticWithToBuilderBuilder builder() { - return new StaticWithToBuilderBuilder(); - } - public @java.lang.SuppressWarnings("all") StaticWithToBuilderBuilder toBuilder() { - return new StaticWithToBuilderBuilder().mOne(this.mOne).bar(this.foo); + return new ConstructorWithToBuilderBuilder().mOne(this.mOne).baz(this.foo).bars(this.bars); } } diff --git a/test/transform/resource/before/BuilderWithToBuilder.java b/test/transform/resource/before/BuilderWithToBuilder.java index 63e16ae8..50938ab2 100644 --- a/test/transform/resource/before/BuilderWithToBuilder.java +++ b/test/transform/resource/before/BuilderWithToBuilder.java @@ -13,19 +13,8 @@ class BuilderWithToBuilder { class ConstructorWithToBuilder { private String mOne, mTwo; private T foo; - @lombok.Singular private List bars; - @Builder(toBuilder = true) - public ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T bar) { - } -} -@lombok.experimental.Accessors(prefix = "m") -class StaticWithToBuilder { - private String mOne, mTwo; - private T foo; - private K bar; - @lombok.Singular private List bars; + @lombok.Singular private com.google.common.collect.ImmutableList bars; @Builder(toBuilder = true) - public static StaticWithToBuilder test(String mOne, @Builder.ObtainVia(field = "foo") Z bar) { - return new StaticWithToBuilder(); + public ConstructorWithToBuilder(String mOne, @Builder.ObtainVia(field = "foo") T baz, com.google.common.collect.ImmutableList bars) { } } -- cgit