IDEA-176650 Provide a quick fix of "can produce NPE" that wraps qualifier with Obejcts.requireNonNull

This commit is contained in:
Tagir Valeev
2017-07-27 18:48:43 +07:00
parent 1e41f2d63b
commit 4882d4a589
14 changed files with 219 additions and 4 deletions
@@ -0,0 +1,10 @@
// "Replace with 'Objects.requireNonNull(arr)'" "true"
import java.util.Objects;
class MyClass {
void test() {
int[] arr = Math.random() > 0.5 ? null : new int[10];
System.out.println(Objects.requireNonNull(arr)[1]);
}
}
@@ -0,0 +1,14 @@
// "Replace with 'Objects.requireNonNull(getObject())'" "true"
import java.util.Objects;
class MyClass {
int a;
static MyClass getObject() {
return Math.random() > 0.5 ? new MyClass() : null;
}
void test() {
Objects.requireNonNull(getObject()).a = 5;
}
}
@@ -0,0 +1,12 @@
// "Replace with 'Objects.requireNonNull(arr)'" "true"
import java.util.Objects;
class MyClass {
void foo(String[] arr) {}
void test() {
String[] arr = Math.random() > 0.5 ? null : new String[10];
foo(Objects.requireNonNull(arr));
}
}
@@ -0,0 +1,12 @@
// "Replace with 'Objects.requireNonNull(Math.random() > 0.5 ? null : "bar")'" "true"
import java.util.List;
import java.util.Objects;
class MyClass {
void foo(String str) {}
void test() {
foo(Objects.requireNonNull(Math.random() > 0.5 ? null : "bar"));
}
}
@@ -0,0 +1,11 @@
// "Replace with 'Objects.requireNonNull(s1)'" "true"
import java.util.List;
import java.util.Objects;
class MyClass {
void test(List<String> list) {
list.stream().map(s -> s.isEmpty() ? null : s)
.map(s1 -> Objects.requireNonNull(s1).trim())
.forEach(System.out::println);
}
}
@@ -0,0 +1,8 @@
// "Replace with 'Objects.requireNonNull(arr)'" "true"
class MyClass {
void test() {
int[] arr = Math.random() > 0.5 ? null : new int[10];
System.out.println(arr<caret>[1]);
}
}
@@ -0,0 +1,12 @@
// "Replace with 'Objects.requireNonNull(getObject())'" "true"
class MyClass {
int a;
static MyClass getObject() {
return Math.random() > 0.5 ? new MyClass() : null;
}
void test() {
getObject<caret>().a = 5;
}
}
@@ -0,0 +1,10 @@
// "Replace with 'Objects.requireNonNull(arr)'" "true"
class MyClass {
void foo(String[] arr) {}
void test() {
String[] arr = Math.random() > 0.5 ? null : new String[10];
foo(ar<caret>r);
}
}
@@ -0,0 +1,11 @@
// "Replace with 'Objects.requireNonNull(Math.random() > 0.5 ? null : "bar")'" "true"
import java.util.List;
class MyClass {
void foo(String str) {}
void test() {
foo(Math.random() > 0.5 ? null :<caret> "bar");
}
}
@@ -0,0 +1,10 @@
// "Replace with 'Objects.requireNonNull(s1)'" "true"
import java.util.List;
class MyClass {
void test(List<String> list) {
list.stream().map(s -> s.isEmpty() ? null : s)
.map(s1 -> s1.t<caret>rim())
.forEach(System.out::println);
}
}