Skip to content

Commit

Permalink
Make properties of type java.util.Optional implicitly optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jan 21, 2024
1 parent 87f17f0 commit 0d85732
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/jilt/internal/AbstractBuilderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ private boolean determineIfAttributeIsOptional(VariableElement attribute) {
if (this.firstAnnotationCalledNullable(attribute) != null) {
return true;
}
TypeName attributeType = ClassName.get(attribute.asType());
// Optional most likely has a type parameter, so use toString() and startsWith()
// for comparison, disregarding the type parameter
if (attributeType.toString().startsWith("java.util.Optional")) {
return true;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ protected final String interfaceNameForAttribute(VariableElement attribute) {
return interfaceNameFromBaseName(Utils.capitalize(attributeSimpleName(attribute)));
}

protected final ParameterSpec setterParameterInInterface(VariableElement attribute) {
return this.setterParameter(attribute, this.attributeType(attribute));
protected final ParameterSpec setterParameterInInterface(VariableElement attribute,
boolean withMangledTypeParameters) {
return this.setterParameter(attribute, this.attributeType(attribute, withMangledTypeParameters));
}

protected final TypeName attributeType(VariableElement attribute) {
protected final TypeName attributeType(VariableElement attribute,
boolean withMangledTypeParameters) {
TypeName ret = TypeName.get(attribute.asType());
return this.mangleTypeName(ret);
return withMangledTypeParameters ? this.mangleTypeName(ret) : ret;
}

protected final String lastInterfaceName() {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/jilt/internal/TypeSafeBuilderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ protected void generateClassesNeededByBuilder() throws Exception {
for (VariableElement currentAttribute : attributes()) {
boolean attributeIsOptional = this.isOptional(currentAttribute);

boolean mangleTypeParameters = !attributeIsOptional;
MethodSpec setterMethod = MethodSpec
.methodBuilder(builderSetterMethodName(currentAttribute))
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(this.returnTypeForSetterFor(currentAttribute, !attributeIsOptional))
.addParameter(this.setterParameterInInterface(currentAttribute))
.returns(this.returnTypeForSetterFor(currentAttribute, mangleTypeParameters))
.addParameter(this.setterParameterInInterface(currentAttribute, mangleTypeParameters))
.build();

if (attributeIsOptional) {
Expand All @@ -57,7 +58,8 @@ protected void generateClassesNeededByBuilder() throws Exception {
}
}

this.addBuildMethodToInterface(optionalsInterfaceBuilder, /* withMangledTypeParameters */ false);
this.addBuildMethodToInterface(optionalsInterfaceBuilder,
/* withMangledTypeParameters */ false);
outerInterfacesBuilder.addType(optionalsInterfaceBuilder.build());

JavaFile javaFile = JavaFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ protected void generateClassesNeededByBuilder() throws Exception {
innerInterfaceBuilder.addMethod(MethodSpec
.methodBuilder(builderSetterMethodName(currentAttribute))
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(this.returnTypeForSetterFor(currentAttribute, true))
.addParameter(this.setterParameterInInterface(currentAttribute))
.returns(this.returnTypeForSetterFor(currentAttribute,
/* withMangledTypeParameters */ true))
.addParameter(this.setterParameterInInterface(currentAttribute,
/* withMangledTypeParameters */ true))
.build());

if (nextAttribute == null && isOptional(currentAttribute)) {
this.addBuildMethodToInterface(innerInterfaceBuilder, /* withMangledTypeParameters */ true);
this.addBuildMethodToInterface(innerInterfaceBuilder,
/* withMangledTypeParameters */ true);
}
} while (nextAttribute != null
&& isOptional(currentAttribute)
Expand All @@ -62,7 +65,8 @@ && isOptional(currentAttribute)
.interfaceBuilder(lastInterfaceName())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addTypeVariables(this.builderClassTypeParameters());
this.addBuildMethodToInterface(finalInterfaceBuilder, /* withMangledTypeParameters */ false);
this.addBuildMethodToInterface(finalInterfaceBuilder,
/* withMangledTypeParameters */ false);
outerInterfacesBuilder.addType(finalInterfaceBuilder.build());

JavaFile javaFile = JavaFile
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jilt/test/OptionalsValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class OptionalsValueTest {
@Test
public void optional_property_is_always_optional() {
OptionalsValue<String, Integer> value = OptionalsValueBuilder.<String, Integer>optionalsValue()
.optional(Optional.of("abc"))
.t2(33)
.optional(Optional.of("abc"))
.build();

assertThat(value.optional).contains("abc");
Expand Down

0 comments on commit 0d85732

Please sign in to comment.