
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
62 lines
1.5 KiB
Plaintext
62 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When boxed type `java.lang.Boolean` is used as an expression to determine the control flow (as described in https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.5[Java Language Specification §4.2.5 The `boolean` Type and boolean Values]) it will throw a `NullPointerException` if the value is `null` (as defined in https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8[Java Language Specification §5.1.8 Unboxing Conversion]).
|
|
|
|
|
|
It is safer to avoid such conversion altogether and handle the `null` value explicitly.
|
|
|
|
Note, however, that no issues will be raised for Booleans that have already been null-checked.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
Boolean b = getBoolean();
|
|
if (b) { // Noncompliant, it will throw NPE when b == null
|
|
foo();
|
|
} else {
|
|
bar();
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
Boolean b = getBoolean();
|
|
if (Boolean.TRUE.equals(b)) {
|
|
foo();
|
|
} else {
|
|
bar(); // will be invoked for both b == false and b == null
|
|
}
|
|
|
|
|
|
Boolean b = getBoolean();
|
|
if(b != null){
|
|
String test = b ? "test" : "";
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8[Java Language Specification §5.1.8 Unboxing Conversion]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use a primitive boolean expression here
|
|
|
|
|
|
=== Highlighting
|
|
|
|
An identifier with java.lang.Boolean type
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|