@@ -4,7 +4,7 @@ <h2>Why is this an issue?</h2>
44boolean Values</ a > ) it will throw a < code > NullPointerException</ code > if the value is < code > null</ code > (as defined in < a
55href ="https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8 "> Java Language Specification §5.1.8 Unboxing Conversion</ a > ).</ p >
66< p > It is safer to avoid such conversion altogether and handle the < code > null</ code > value explicitly.</ p >
7- < p > Note, however, that no issues will be raised for Booleans that have already been null-checked.</ p >
7+ < p > Note, however, that no issues will be raised for Booleans that have already been null-checked or are marked < code > @NonNull/@NotNull </ code > .</ p >
88< h3 > Noncompliant code example</ h3 >
99< pre >
1010Boolean b = getBoolean();
@@ -29,6 +29,30 @@ <h3>Compliant solution</h3>
2929 String test = b ? "test" : "";
3030}
3131</ pre >
32+ < h3 > Exceptions</ h3 >
33+ < p > The issue is not raised if the expression is annotated < code > @NonNull</ code > / < code > @NotNull</ code > . This is useful if a boxed type is an
34+ instantiation of a generic type parameter and cannot be avoided.</ p >
35+ < pre >
36+ List<Boolean> list = new ArrayList<>();
37+ list.add(true);
38+ list.add(false);
39+ list.forEach((@NonNull Boolean value) -> {
40+ // Compliant
41+ if(value) {
42+ System.out.println("yes");
43+ }
44+ });
45+
46+ @NonNull Boolean someMethod() { /* ... */ }
47+
48+ // Compliant
49+ if(someMethod()) { /* ... */ }
50+
51+ @NonNull Boolean boxedNonNull = Boolean.TRUE;
52+
53+ // Compliant
54+ if(boxedNonNull) { /* ... */ }
55+ </ pre >
3256< h2 > Resources</ h2 >
3357< ul >
3458 < li > < a href ="https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8 "> Java Language Specification §5.1.8 Unboxing Conversion</ a >
0 commit comments