Skip to content

Commit

Permalink
Make fields and properties annotated with @nullable optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jan 16, 2024
1 parent 3d1c2f8 commit 0042473
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.assertj:assertj-core:3.6.2'
testImplementation 'com.google.code.findbugs:jsr305:3.0.2'
testAnnotationProcessor project
}
// required in Gradle 8, since the above line 'testAnnotationProcessor project'
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/jilt/internal/AbstractBuilderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jilt.utils.Utils;

import javax.annotation.processing.Filer;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
Expand Down Expand Up @@ -144,12 +145,29 @@ private TypeName returnTypeForSetterFor(VariableElement attribute) {
private Set<VariableElement> initOptionalAttributes() {
Set<VariableElement> ret = new HashSet<VariableElement>();
for (VariableElement attribute : attributes) {
if (attribute.getAnnotation(Opt.class) != null)
if (this.determineIfAttributeIsOptional(attribute)) {
ret.add(attribute);
}
}
return ret;
}

private boolean determineIfAttributeIsOptional(VariableElement attribute) {
if (attribute.getAnnotation(Opt.class) != null) {
return true;
}
for (AnnotationMirror annotation : attribute.getAnnotationMirrors()) {
if (annotationIsCalledNullable(annotation)) {
return true;
}
}
return false;
}

private static boolean annotationIsCalledNullable(AnnotationMirror annotation) {
return "Nullable".equals(annotation.getAnnotationType().asElement().getSimpleName().toString());
}

private String initBuilderClassPackage() {
String annotationBuilderPackageName = builderAnnotation.packageName();
if (annotationBuilderPackageName.isEmpty()) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/jilt/test/FullNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jilt.test;

import org.jilt.test.data.nullable.FullName;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.jilt.test.data.nullable.FullNameBuilder.fullName;

public class FullNameTest {
@Test
public void nullable_attribute() throws Exception {
FullName value = fullName()
.firstName("First")
.lastName(null)
// middleName is implicitly optional, because of @Nullable
.build();

assertThat(value.firstName).isEqualTo("First");
assertThat(value.middleName).isNull();
assertThat(value.lastName).isNull();
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/jilt/test/data/nullable/FullName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jilt.test.data.nullable;

import org.jilt.Builder;
import org.jilt.BuilderStyle;

import javax.annotation.Nullable;

public final class FullName {
public final String firstName, middleName, lastName;

@Builder(style = BuilderStyle.TYPE_SAFE)
public FullName(String firstName, @Nullable String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
}

0 comments on commit 0042473

Please sign in to comment.