Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0a8045

Browse files
committed
Require non-null values for Option typed fields, and also for directly setting an Option object as well.
This prevents Option fields being set to `Some(null)`, which is unexpected and can lead to bugs.#27
1 parentf80022d commitd0a8045

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

‎vavr-encodings/src/main/java/org/immutables/vavr/encodings/VavrOptionEncoding.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
packageorg.immutables.vavr.encodings;
1818

19-
importio.vavr.control.Option;
19+
importjava.util.Objects;
20+
2021
importorg.immutables.encode.Encoding;
2122

22-
importjava.util.Objects;
23+
importio.vavr.control.Option;
2324

2425
@Encoding
2526
classVavrOptionEncoding<T>
@@ -43,7 +44,7 @@ public Option<T> withOption(
4344
publicOption<T>with(
4445
finalTvalue)
4546
{
46-
returnOption.some(value);
47+
returnOption.some(Objects.requireNonNull(value));
4748
}
4849

4950
@Encoding.Builder
@@ -61,14 +62,14 @@ static final class Builder<T>
6162
voidset(
6263
finalOption<T>opt)
6364
{
64-
this.optional =opt;
65+
this.optional =Objects.requireNonNull(opt);
6566
}
6667

6768
@Encoding.Init
6869
voidsetValue(
6970
finalTx)
7071
{
71-
this.optional =Option.of(x);
72+
this.optional =Option.some(Objects.requireNonNull(x));
7273
}
7374

7475
@Encoding.Naming(value ="unset*")

‎vavr-examples/src/test/java/org/immutables/vavr/tests/examples/ExampleOptionTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,36 @@ public void testUnset()
6969
finalImmutableExampleOptionTypea0 =b.build();
7070
Assert.assertEquals(Option.none(),a0.optionalInteger());
7171
}
72+
73+
@Test(expected =NullPointerException.class)
74+
publicvoidtestSetNullValue()
75+
{
76+
finalImmutableExampleOptionType.Builderb =
77+
ImmutableExampleOptionType.builder();
78+
b.optionalInteger((Integer)null);
79+
}
80+
81+
@Test(expected =NullPointerException.class)
82+
publicvoidtestSetNullOption()
83+
{
84+
finalImmutableExampleOptionType.Builderb =
85+
ImmutableExampleOptionType.builder();
86+
b.optionalInteger((Option<Integer>)null);
87+
}
88+
89+
@Test(expected =NullPointerException.class)
90+
publicvoidtestWithNull()
91+
{
92+
finalImmutableExampleOptionTypeb =
93+
ImmutableExampleOptionType.builder().build();
94+
b.withOptionalInteger((Integer)null);
95+
}
96+
97+
@Test(expected =NullPointerException.class)
98+
publicvoidtestWithNullOption()
99+
{
100+
finalImmutableExampleOptionTypeb =
101+
ImmutableExampleOptionType.builder().build();
102+
b.withOptionalInteger((Option<Integer>)null);
103+
}
72104
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp