Adds external Contract for Objects::toString

Fixes IDEA-197174 Nullable inspection control flow with Objects.toString check enhancement
This commit is contained in:
Tagir Valeev
2018-08-14 23:03:23 +07:00
parent 5edac7de72
commit 5e7eaa14b2
2 changed files with 30 additions and 0 deletions

View File

@@ -95,4 +95,28 @@ class ContractReturnValues {
@Contract("null -> fail")
native static void check(@Nullable String s);
// IDEA-197174
private void withNullInspectionInsideIf(@Nullable String text) {
if (!Objects.toString(text, "").isEmpty()) {
System.out.println(text.trim());
}
}
private void withNullInspectionOutsideIf(@Nullable String text) {
if (Objects.toString(text, "").isEmpty()) {
System.out.println("Is Empty");
} else {
System.out.println(text.trim());
}
}
private void withNullInspectionWithoutDefaultValue(@Nullable String text) {
if (Objects.toString(text).isEmpty()) {
System.out.println("Is Empty");
} else {
// In this case system can produce NullPointerException, because Objects.toString(String str) calls methods String.valueOf()
System.out.println(text.<warning descr="Method invocation 'trim' may produce 'java.lang.NullPointerException'">trim</warning>());
}
}
}

View File

@@ -1788,6 +1788,12 @@
<item name='java.util.NavigableSet java.util.SortedSet&lt;E&gt; tailSet(E)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='java.util.Objects java.lang.String toString(java.lang.Object, java.lang.String)'>
<annotation name='org.jetbrains.annotations.Contract'>
<val name="value" val="&quot;null, _ -&gt; param2&quot;"/>
<val name="pure" val="true"/>
</annotation>
</item>
<item name='java.util.Optional T get()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
<annotation name='org.jetbrains.annotations.Contract'>