From 3a8fdc3e31e482a4f6044d9a75211a5c136e6ecc Mon Sep 17 00:00:00 2001 From: Joran Van Belle Date: Wed, 24 Apr 2024 17:09:17 +0200 Subject: [PATCH] feat: handle booleans more gracefully --- .github/workflows/maven.yml | 10 ++++-- annotations/pom.xml | 2 +- pom.xml | 6 +++- .../bob/definitions/TypeDefinition.java | 14 ++++++-- .../src/test/java/io/jonasg/bob/BobTests.java | 20 +++++++++++ .../BooleanFieldHasOtherNameThanSetter.java | 21 +++++++++++ ...ed_BooleanFieldHasOtherNameThanSetter.java | 35 +++++++++++++++++++ .../Expected_RecordsAreBuildable.java | 0 8 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/BooleanFieldHasOtherNameThanSetter.java create mode 100644 processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/Expected_BooleanFieldHasOtherNameThanSetter.java delete mode 100644 processor/src/test/resources/tests/successful-compilation/RecordsAreBuildable/Expected_RecordsAreBuildable.java diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 7a853cf..a767ec8 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -29,6 +29,10 @@ jobs: java-version: ${{ matrix.java }} distribution: 'temurin' cache: maven - - name: Build with Maven - if: ${{ matrix.java }} == 11 - run: mvn -B -ntp verify -Dtest=\!RecordsTest.java --file pom.xml \ No newline at end of file + - name: Build with Java 11 + if: ${{ matrix.java == 11 }} + run: mvn -B -ntp verify -Dtest=\!RecordsTest.java --file pom.xml + + - name: Build with Java higher than 11 + if: ${{ matrix.java > 11 }} + run: mvn -B -ntp verify --file pom.xml diff --git a/annotations/pom.xml b/annotations/pom.xml index 0cd05fc..bb0e162 100644 --- a/annotations/pom.xml +++ b/annotations/pom.xml @@ -32,4 +32,4 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7a0845c..168fa5f 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,10 @@ Jonas Geiregat jonas.grgt@gmail.com + + Joran Van Belle + joran.vanbelle@live.be + @@ -293,4 +297,4 @@ - + \ No newline at end of file diff --git a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java index d688589..0feb30f 100644 --- a/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java +++ b/processor/src/main/java/io/jonasg/bob/definitions/TypeDefinition.java @@ -1,5 +1,6 @@ package io.jonasg.bob.definitions; +import javax.lang.model.type.TypeKind; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -58,12 +59,15 @@ public List getSetterMethods() { for (FieldDefinition field : fields) { String name = field.name().substring(0, 1).toUpperCase() + field.name().substring(1); methodsWithOneParam.stream() - .filter(m -> m.name().equals(field.name())) + .filter(m -> m.name().equals(field.name()) || + isABooleanField(field, m)) .findFirst() .map(m -> new SetterMethodDefinition(m.name(), field, m.parameters().get(0))) .ifPresent(setters::add); methodsWithOneParam.stream() - .filter(m -> m.name().equals(String.format("set%s", name))) + .filter(m -> m.name().equals(String.format("set%s", name)) + || (m.name().equals(String.format("set%s", name.substring(2))) + && field.type().getKind().equals(TypeKind.BOOLEAN))) .findFirst() .map(m -> new SetterMethodDefinition(m.name(), field, m.parameters().get(0))) .ifPresent(setters::add); @@ -71,6 +75,12 @@ public List getSetterMethods() { return setters; } + private boolean isABooleanField(FieldDefinition field, MethodDefinition m) { + return field.name().startsWith("is") + && m.name().equalsIgnoreCase(field.name().substring(2)) + && field.type().getKind().equals(TypeKind.BOOLEAN); + } + public boolean containsSetterMethods() { return !getSetterMethods().isEmpty(); } diff --git a/processor/src/test/java/io/jonasg/bob/BobTests.java b/processor/src/test/java/io/jonasg/bob/BobTests.java index 2833832..b9a1caf 100644 --- a/processor/src/test/java/io/jonasg/bob/BobTests.java +++ b/processor/src/test/java/io/jonasg/bob/BobTests.java @@ -67,6 +67,26 @@ void allConstructorParamsAreBuildableIfHavingMatchingField() { .executeTest(); } + @Test + void booleanFieldHasOherNameThanSetter() { + Cute.blackBoxTest() + .given() + .processors(List.of(BuildableProcessor.class)) + .andSourceFiles( + "/tests/BooleanFieldHasOtherNameThanSetter/BooleanFieldHasOtherNameThanSetter.java") + .whenCompiled() + .thenExpectThat() + .compilationSucceeds() + .andThat() + .generatedSourceFile( + "io.jonasg.bob.test.BooleanFieldHasOtherNameThanSetterBuilder") + .matches( + CuteApi.ExpectedFileObjectMatcherKind.BINARY, + JavaFileObjectUtils.readFromResource( + "/tests/BooleanFieldHasOtherNameThanSetter/Expected_BooleanFieldHasOtherNameThanSetter.java")) + .executeTest(); + } + @Test void defaultValuesForParamsWithNoneMatchingField() { Cute.blackBoxTest() diff --git a/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/BooleanFieldHasOtherNameThanSetter.java b/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/BooleanFieldHasOtherNameThanSetter.java new file mode 100644 index 0000000..952479d --- /dev/null +++ b/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/BooleanFieldHasOtherNameThanSetter.java @@ -0,0 +1,21 @@ +package io.jonasg.bob.test; + +import io.jonasg.bob.Buildable; + +@Buildable +public class BooleanFieldHasOtherNameThanSetter { + private String firstName; + + private String lastName; + + private boolean isHappy; + + public BooleanFieldHasOtherNameThanSetter(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public void setHappy(boolean ishappy) { + this.isHappy = ishappy; + } +} \ No newline at end of file diff --git a/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/Expected_BooleanFieldHasOtherNameThanSetter.java b/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/Expected_BooleanFieldHasOtherNameThanSetter.java new file mode 100644 index 0000000..60e22f2 --- /dev/null +++ b/processor/src/test/resources/tests/BooleanFieldHasOtherNameThanSetter/Expected_BooleanFieldHasOtherNameThanSetter.java @@ -0,0 +1,35 @@ +package io.jonasg.bob.test; + +import java.lang.String; + +public final class BooleanFieldHasOtherNameThanSetterBuilder { + private String firstName; + + private String lastName; + + private boolean isHappy; + + public BooleanFieldHasOtherNameThanSetterBuilder() { + } + + public BooleanFieldHasOtherNameThanSetterBuilder firstName(String firstName) { + this.firstName = firstName; + return this; + } + + public BooleanFieldHasOtherNameThanSetterBuilder lastName(String lastName) { + this.lastName = lastName; + return this; + } + + public BooleanFieldHasOtherNameThanSetterBuilder isHappy(boolean isHappy) { + this.isHappy = isHappy; + return this; + } + + public BooleanFieldHasOtherNameThanSetter build() { + var instance = new BooleanFieldHasOtherNameThanSetter(firstName, lastName); + instance.setHappy(this.isHappy); + return instance; + } +} diff --git a/processor/src/test/resources/tests/successful-compilation/RecordsAreBuildable/Expected_RecordsAreBuildable.java b/processor/src/test/resources/tests/successful-compilation/RecordsAreBuildable/Expected_RecordsAreBuildable.java deleted file mode 100644 index e69de29..0000000