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 committed Apr 24, 2024
1 parent 2ebd260 commit 53a8028
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 6 deletions.
2 changes: 2 additions & 0 deletions annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<version>0.4.1</version>

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

Expand All @@ -45,8 +44,8 @@
<flatten-maven-plugin.version>1.5.0</flatten-maven-plugin.version>
<surefire-maven-plugin.version>3.1.2</surefire-maven-plugin.version>

<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

<jreleaser.version>1.9.0</jreleaser.version>
<java-poet.version>1.13.0</java-poet.version>
Expand Down Expand Up @@ -103,6 +102,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected MethodSpec generateSetterForField(BuildableField field) {
.returns(builderType())
.addParameter(TypeName.get(field.type()), field.name());
if (field.isConstructorArgument() && isAnEnforcedConstructorPolicy() || field.isMandatory()) {
builder.addStatement("this.$L.set($L)", field.name(), field.name());
builder.addStatement("this.$L.set($L)", field.name(), field.name()); // fixme
} else {
builder.addStatement("this.$L = $L", field.name(), field.name());
}
Expand Down
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,12 +59,15 @@ 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()) || (m.name().equals(field.name().substring(2))
&& field.type().getKind().equals(TypeKind.BOOLEAN)))
.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);
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;
}
}

0 comments on commit 53a8028

Please sign in to comment.