Skip to content

Commit 365b1c7

Browse files
committed
Add Nullable Setters to AutoValueTest, fix some bugs
1 parent 6a9bfe5 commit 365b1c7

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

value/src/it/functional/src/test/java/com/google/auto/value/AutoValueTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,54 @@ public void testOmitOptionalWithBuilder() {
14101410
assertThat(suppliedDirectly.optionalString()).hasValue("foo");
14111411
assertThat(suppliedDirectly.optionalInteger()).hasValue(23);
14121412
}
1413+
1414+
@AutoValue
1415+
public abstract static class OptionalPropertyWithNullableBuilder {
1416+
public abstract String notOptional();
1417+
public abstract com.google.common.base.Optional<String> optional();
1418+
1419+
public static Builder builder() {
1420+
return new AutoValue_AutoValueTest_OptionalPropertyWithNullableBuilder.Builder();
1421+
}
1422+
1423+
@AutoValue.Builder
1424+
public interface Builder {
1425+
Builder notOptional(String s);
1426+
Builder optional(@Nullable String s);
1427+
OptionalPropertyWithNullableBuilder build();
1428+
}
1429+
}
1430+
1431+
@Test
1432+
public void testOmitOptionalWithNullableBuilder() {
1433+
OptionalPropertyWithNullableBuilder instance1 = OptionalPropertyWithNullableBuilder.builder()
1434+
.notOptional("hello")
1435+
.build();
1436+
assertThat(instance1.notOptional()).isEqualTo("hello");
1437+
assertThat(instance1.optional()).isAbsent();;
1438+
1439+
OptionalPropertyWithNullableBuilder instance2 = OptionalPropertyWithNullableBuilder.builder()
1440+
.notOptional("hello")
1441+
.optional(null)
1442+
.build();
1443+
assertThat(instance2.notOptional()).isEqualTo("hello");
1444+
assertThat(instance2.optional()).isAbsent();
1445+
assertThat(instance1).isEqualTo(instance2);
1446+
1447+
OptionalPropertyWithNullableBuilder instance3 = OptionalPropertyWithNullableBuilder.builder()
1448+
.notOptional("hello")
1449+
.optional("world")
1450+
.build();
1451+
assertThat(instance3.notOptional()).isEqualTo("hello");
1452+
assertThat(instance3.optional()).hasValue("world");
1453+
1454+
try {
1455+
OptionalPropertyWithNullableBuilder.builder().build();
1456+
fail("Expected IllegalStateException for unset non-Optional property");
1457+
} catch (IllegalStateException e) {
1458+
assertThat(e.getMessage()).contains("notOptional");
1459+
}
1460+
}
14131461

14141462
@AutoValue
14151463
public abstract static class NullableOptionalPropertiesWithBuilder {

value/src/main/java/com/google/auto/value/processor/BuilderSpec.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ public String copy(AutoValueProcessor.Property property) {
385385
String copy = String.format(copyOf, property);
386386

387387
// Add a null guard only in cases where we are using copyOf and the property is @Nullable.
388-
if (property.isNullable() || nullableAnnotation != null) {
388+
// No guard for the @Nullable annotation case because from/ofNullable doesn't need it
389+
if (property.isNullable()) {
389390
copy = String.format("(%s == null ? null : %s)", property, copy);
390391
}
391392

value/src/main/java/com/google/auto/value/processor/autovalue.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ $a
254254
${setter.access}${builderTypeName}${builderActualTypes} ##
255255
${setter.name}(${nullableAnnotation}$setter.parameterType $p) {
256256

257-
#if (!$setter.primitiveParameter && !$nullableAnnotation)
257+
#if (!${setter.primitiveParameter} && ${nullableAnnotation.isEmpty()})
258258

259259
if ($p == null) {
260260
throw new NullPointerException("Null $p.name");

0 commit comments

Comments
 (0)