From 1b662785f30a362fc5388cbc335863008d6ec3e0 Mon Sep 17 00:00:00 2001 From: JonasG Date: Wed, 20 Mar 2024 21:06:13 +0100 Subject: [PATCH] feat: add generics support --- README.md | 17 +++++++--- .../java/io/jonasg/bob/BobFeaturesTests.java | 21 ++++++++++-- .../Expected_GenericsAreBuildable.java | 33 +++++++++++++++++++ .../GenericsAreBuildable.java | 16 +++++++++ 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/test/resources/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java create mode 100644 src/test/resources/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java diff --git a/README.md b/README.md index bba6a1a..9ea5b94 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# 👷‍ Bob -Lightweight Builder generator for Java +# 👷‍Bob +🪶Lightweight Builder generator for Java ## Why Bob? @@ -37,8 +37,6 @@ public class Car { this.color = color; this.price = price; } - - // getters toString and hashcode left out for brevity } ``` @@ -123,7 +121,16 @@ public class Cup { Can be used as: ```java -Cup string = new CupBuilder().topping("String") +Cup string = new CupBuilder().topping("cream") .contents(BigDecimal.ZERO) .build(); ``` + +or alternatively: + +```java +GenericsAreBuildableBuilder.of(BigDecimal.class, String.class) +.topping("cream") +.contents(BigDecimal.ZERO) +.build(); +``` diff --git a/src/test/java/io/jonasg/bob/BobFeaturesTests.java b/src/test/java/io/jonasg/bob/BobFeaturesTests.java index 3942234..760b943 100644 --- a/src/test/java/io/jonasg/bob/BobFeaturesTests.java +++ b/src/test/java/io/jonasg/bob/BobFeaturesTests.java @@ -130,7 +130,7 @@ void useConstructorAnnotatedWithBuildableConstructor() { @Test void useConstructorWithTheGreatestNumberOfParameters() { - Cute.blackBoxTest() + Cute.blackBoxTest() .given() .processors(List.of(BuildableProcessor.class)) .andSourceFiles("/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/UseConstructorWithTheGreatestNumberOfParameters.java") @@ -141,7 +141,24 @@ void useConstructorWithTheGreatestNumberOfParameters() { .generatedSourceFile("io.jonasg.bob.test.builder.UseConstructorWithTheGreatestNumberOfParametersBuilder") .matches( CuteApi.ExpectedFileObjectMatcherKind.BINARY, - JavaFileObjectUtils.readFromResource("/tests/successful-compilation/useConstructorWithTheGreatestNumberOfParameters/Expected_UseConstructorWithTheGreatestNumberOfParameters.java")) + JavaFileObjectUtils.readFromResource("/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/Expected_UseConstructorWithTheGreatestNumberOfParameters.java")) + .executeTest(); + } + + @Test + void genericsAreBuildable() { + Cute.blackBoxTest() + .given() + .processors(List.of(BuildableProcessor.class)) + .andSourceFiles("/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java") + .whenCompiled() + .thenExpectThat() + .compilationSucceeds() + .andThat() + .generatedSourceFile("io.jonasg.bob.test.builder.GenericsAreBuildableBuilder") + .matches( + CuteApi.ExpectedFileObjectMatcherKind.BINARY, + JavaFileObjectUtils.readFromResource("/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java")) .executeTest(); } diff --git a/src/test/resources/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java b/src/test/resources/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java new file mode 100644 index 0000000..475b0f8 --- /dev/null +++ b/src/test/resources/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java @@ -0,0 +1,33 @@ +package io.jonasg.bob.test.builder; + +import io.jonasg.bob.test.GenericsAreBuildable; +import java.lang.Class; +import java.lang.String; + +public final class GenericsAreBuildableBuilder { + private T contents; + + private R topping; + + public GenericsAreBuildableBuilder() { + } + + public GenericsAreBuildableBuilder contents(T contents) { + this.contents = contents; + return this; + } + + public GenericsAreBuildableBuilder topping(R topping) { + this.topping = topping; + return this; + } + + public GenericsAreBuildable build() { + return new GenericsAreBuildable(contents, topping); + } + + public static GenericsAreBuildableBuilder of(Class Ttype, + Class Rtype) { + return new GenericsAreBuildableBuilder<>(); + } +} diff --git a/src/test/resources/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java b/src/test/resources/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java new file mode 100644 index 0000000..0d357fe --- /dev/null +++ b/src/test/resources/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java @@ -0,0 +1,16 @@ +package io.jonasg.bob.test; + +import java.lang.String; +import io.jonasg.bob.Buildable; + +@Buildable +public class GenericsAreBuildable { + + private T contents; + private R topping; + + public GenericsAreBuildable(T contents, R topping) { + this.contents = contents; + this.topping = topping; + } +}