Skip to content

Commit

Permalink
feat: handle booleans more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranVanBelle authored and jonas-grgt committed Apr 25, 2024
1 parent 286a122 commit 3a8fdc3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 7 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- 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
2 changes: 1 addition & 1 deletion annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
</dependency>
</dependencies>

</project>
</project>
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<name>Jonas Geiregat</name>
<email>[email protected]</email>
</developer>
<developer>
<name>Joran Van Belle</name>
<email>[email protected]</email>
</developer>
</developers>

<licenses>
Expand Down Expand Up @@ -293,4 +297,4 @@
</repository>
</distributionManagement>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,19 +59,28 @@ public List<SetterMethodDefinition> 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);
}
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();
}
Expand Down
20 changes: 20 additions & 0 deletions processor/src/test/java/io/jonasg/bob/BobTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Empty file.

0 comments on commit 3a8fdc3

Please sign in to comment.