summaryrefslogtreecommitdiff
path: root/java/java-tests/testData/inspection/dataFlow/fixture
diff options
context:
space:
mode:
Diffstat (limited to 'java/java-tests/testData/inspection/dataFlow/fixture')
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/AccessingSameArrayElements.java56
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/AssertThat.java18
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/CatchRuntimeException.java2
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ContractInferenceBewareOverriding.java21
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/FinalFieldDuringSuperInitialization.java20
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/GuavaCheckNotNull.java11
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/NumberComparisonsWhenValueIsKnown.java16
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences.java10
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_NotInComplexAssignment.java6
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall.java14
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall_after.java14
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum.java7
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum_after.java9
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant.java10
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant_after.java10
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString.java8
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString_after.java8
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_Switch.java22
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_after.java10
-rw-r--r--java/java-tests/testData/inspection/dataFlow/fixture/UseInferredContracts.java15
20 files changed, 198 insertions, 89 deletions
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/AccessingSameArrayElements.java b/java/java-tests/testData/inspection/dataFlow/fixture/AccessingSameArrayElements.java
new file mode 100644
index 000000000000..a5a0001517d6
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/AccessingSameArrayElements.java
@@ -0,0 +1,56 @@
+abstract class Test {
+ void testSameEquals(int[] arr1, int[] arr2) {
+ if (arr1[0] == arr2[0] && <warning descr="Condition 'arr1[0] == arr2[0]' is always 'true' when reached">arr1[0] == arr2[0]</warning>) {
+ }
+ }
+
+ void testMatrices(int[][][] arr1, int[][][] arr2) {
+ if (arr1[0][1][2] == arr1[0][1][3] &&
+ <warning descr="Condition 'arr1[0][1][2] == arr1[0][1][3]' is always 'true' when reached">arr1[0][1][2] == arr1[0][1][3]</warning>) {
+ }
+ }
+
+ void testNotEquals(int[] arr1, int[] arr2) {
+ if (arr1[0] != arr2[0]) return;
+
+ if (<warning descr="Condition 'arr1[0] == arr2[0]' is always 'true'">arr1[0] == arr2[0]</warning>) {}
+ }
+
+ void testInvalidatingCall(int[] arr1, int[] arr2) {
+ if (arr1[0] != arr2[0]) return;
+
+ changeArray(arr1);
+
+ if (arr1[0] == arr2[0]) {}
+ }
+ abstract void changeArray(int[] array);
+
+
+ void testIndirectlyInvalidatingCall(int[] arr1, int[] arr2, int[][] arr3) {
+ arr3[0] = arr1;
+
+ if (arr1[0] != arr2[0]) return;
+
+ changeSubArray(arr3);
+
+ if (arr1[0] == arr2[0]) {}
+ }
+ abstract void changeSubArray(int[][] array);
+
+
+ void testAssigning(int[] arr1, int j) {
+ arr1[0] = j;
+ if (<warning descr="Condition 'arr1[0] == j' is always 'true'">arr1[0] == j</warning>) { }
+ }
+
+ void testReassigning(int[] arr1) {
+ arr1[0] = 1;
+ arr1[1] = 3;
+ if (<warning descr="Condition 'arr1[0] == 1' is always 'true'">arr1[0] == 1</warning>) { }
+
+ for (int i = 0; i < arr1.length; i++) {
+ arr1[i] = 2;
+ }
+ if (arr1[0] == 1) { }
+ }
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/AssertThat.java b/java/java-tests/testData/inspection/dataFlow/fixture/AssertThat.java
new file mode 100644
index 000000000000..b3d614cd8cb3
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/AssertThat.java
@@ -0,0 +1,18 @@
+import org.hamcrest.CoreMatchers;
+import org.jetbrains.annotations.Nullable;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+class Contracts {
+
+ private void checkNotNullValue(@Nullable Object o) {
+ assertThat(o, CoreMatchers.<Object>notNullValue());
+ System.out.println(o.hashCode());
+ }
+
+ private void checkNotEqualToNull(@Nullable String test) {
+ assertThat("String is null", test, not(equalTo(null)));
+ int length = test.length();
+ }
+
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/CatchRuntimeException.java b/java/java-tests/testData/inspection/dataFlow/fixture/CatchRuntimeException.java
index 8f6508642352..e635b8dbd9a9 100644
--- a/java/java-tests/testData/inspection/dataFlow/fixture/CatchRuntimeException.java
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/CatchRuntimeException.java
@@ -36,7 +36,7 @@ class BrokenAlignment {
}
public void bar(@NotNull Object foo) {
- assert <warning descr="Condition 'foo != null' is always 'true'">foo != null</warning>;
+ if (<warning descr="Condition 'foo != null' is always 'true'">foo != null</warning>);
}
public void bar2(@NotNull Object foo) {
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ContractInferenceBewareOverriding.java b/java/java-tests/testData/inspection/dataFlow/fixture/ContractInferenceBewareOverriding.java
new file mode 100644
index 000000000000..a1d263f778f6
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/ContractInferenceBewareOverriding.java
@@ -0,0 +1,21 @@
+import org.jetbrains.annotations.Nullable;
+
+class Doo {
+
+ boolean isMaybeNotNull(@Nullable Object o) {
+ return o != null;
+ }
+
+ void foo(@Nullable String s) {
+ if (isMaybeNotNull(s)) {
+ System.out.println(<warning descr="Method invocation 's.length()' may produce 'java.lang.NullPointerException'">s.length()</warning>);
+ }
+ }
+
+}
+
+class DooImpl extends Doo {
+ boolean isMaybeNotNull(@Nullable Object o) {
+ return hashCode() == 42;
+ }
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/FinalFieldDuringSuperInitialization.java b/java/java-tests/testData/inspection/dataFlow/fixture/FinalFieldDuringSuperInitialization.java
new file mode 100644
index 000000000000..59fc5fba349a
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/FinalFieldDuringSuperInitialization.java
@@ -0,0 +1,20 @@
+class Parent {
+ Parent() {
+ callProtectedMethod();
+ }
+ protected void callProtectedMethod() { }
+}
+
+class Child extends Parent {
+ private final Object myField;
+ Child() {
+ super();
+ myField = new Object();
+ }
+ @Override
+ protected void callProtectedMethod() {
+ if (myField != null) { // HERE myField CAN be null
+ System.out.println();
+ }
+ }
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/GuavaCheckNotNull.java b/java/java-tests/testData/inspection/dataFlow/fixture/GuavaCheckNotNull.java
new file mode 100644
index 000000000000..4a11daa71c77
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/GuavaCheckNotNull.java
@@ -0,0 +1,11 @@
+import org.jetbrains.annotations.Nullable;
+
+class Contracts {
+
+ private void check(@Nullable Object o) {
+ com.google.common.base.Preconditions.checkNotNull(o);
+ System.out.println(o.hashCode());
+ }
+
+
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/NumberComparisonsWhenValueIsKnown.java b/java/java-tests/testData/inspection/dataFlow/fixture/NumberComparisonsWhenValueIsKnown.java
new file mode 100644
index 000000000000..71cca5352cf2
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/NumberComparisonsWhenValueIsKnown.java
@@ -0,0 +1,16 @@
+import java.util.Arrays;
+
+class Test {
+ int[] mIndex = null;
+ int mSize = 0;
+
+ void indexValues() {
+ float loadFactor = mIndex == null ? 1.f : ((float) mSize) / ((float) mIndex.length);
+
+ if (loadFactor < 0.25f || 0.75f <= loadFactor) {
+ mIndex = new int[mSize * 2];
+ }
+
+ mIndex[0] = -1;
+ }
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences.java
index d81b46724a81..dbb4f50f44a5 100644
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences.java
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences.java
@@ -1,13 +1,15 @@
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
class Test {
- private void test2(@NotNull Object bar) {
+ private void test2(@NotNull Object bar) {}
+
+ void println(@Nullable Object o) {}
- }
private Object test(Object foo, Object bar) {
if (foo == null) {
- System.out.println(<warning descr="Value 'foo' is always 'null'"><caret>foo</warning>);
- System.out.println(<warning descr="Value 'foo' is always 'null'">foo</warning>);
+ println(<warning descr="Value 'foo' is always 'null'"><caret>foo</warning>);
+ println(<warning descr="Value 'foo' is always 'null'">foo</warning>);
return <warning descr="Expression 'foo' might evaluate to null but is returned by the method which is not declared as @Nullable">foo</warning>;
}
if (bar == null) {
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_NotInComplexAssignment.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_NotInComplexAssignment.java
deleted file mode 100644
index a13f0f1b5d60..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_NotInComplexAssignment.java
+++ /dev/null
@@ -1,6 +0,0 @@
-class Test {
- void foo() {
- int x = 42;
- <caret>x += 100;
- }
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall.java
new file mode 100644
index 000000000000..82c1e9122cc1
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall.java
@@ -0,0 +1,14 @@
+import org.jetbrains.annotations.Nullable;
+
+class Test {
+ void method(@Nullable String o) {}
+
+ void method(@Nullable Integer o) {}
+
+ private void test(String foo) {
+ if (foo == null) {
+ method(<warning descr="Value 'foo' is always 'null'"><caret>foo</warning>);
+ }
+ }
+
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall_after.java
new file mode 100644
index 000000000000..22524625216d
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_OverloadedCall_after.java
@@ -0,0 +1,14 @@
+import org.jetbrains.annotations.Nullable;
+
+class Test {
+ void method(@Nullable String o) {}
+
+ void method(@Nullable Integer o) {}
+
+ private void test(String foo) {
+ if (foo == null) {
+ method(<caret>(String) null);
+ }
+ }
+
+} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum.java
deleted file mode 100644
index 2ed16ce0c6f5..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum.java
+++ /dev/null
@@ -1,7 +0,0 @@
-class Test {
- private void test() {
- foo.MyEnum s = foo.MyEnum.FOO;
- System.out.println(<caret><warning descr="Value 's' is always 'FOO'">s</warning>);
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum_after.java
deleted file mode 100644
index 29552fa61615..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithEnum_after.java
+++ /dev/null
@@ -1,9 +0,0 @@
-import foo.MyEnum;
-
-class Test {
- private void test() {
- foo.MyEnum s = foo.MyEnum.FOO;
- System.out.println(MyEnum.FOO);
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant.java
deleted file mode 100644
index 5388bcda069f..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant.java
+++ /dev/null
@@ -1,10 +0,0 @@
-class Test {
- public static final int CONST = 23942;
- private void test(int a) {
- if (a == CONST) {
- System.out.println(<caret><warning descr="Value 'a' is always 'CONST'">a</warning>);
- }
-
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant_after.java
deleted file mode 100644
index 11feed5f7903..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithIntConstant_after.java
+++ /dev/null
@@ -1,10 +0,0 @@
-class Test {
- public static final int CONST = 23942;
- private void test(int a) {
- if (a == CONST) {
- System.out.println(<caret>CONST);
- }
-
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString.java
deleted file mode 100644
index fcff4846b378..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString.java
+++ /dev/null
@@ -1,8 +0,0 @@
-class Test {
- public static final String CONST = "foo bar";
- private void test() {
- String s = CONST;
- System.out.println(<caret><warning descr="Value 's' is always 'CONST'">s</warning>);
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString_after.java
deleted file mode 100644
index a462170b2b31..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_ReplaceWithString_after.java
+++ /dev/null
@@ -1,8 +0,0 @@
-class Test {
- public static final String CONST = "foo bar";
- private void test() {
- String s = CONST;
- System.out.println(CONST);
- }
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_Switch.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_Switch.java
deleted file mode 100644
index e48dc19555bb..000000000000
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_Switch.java
+++ /dev/null
@@ -1,22 +0,0 @@
-class Test {
- private void test() {
- int state = 1;
- switch (<warning descr="Value 'state' is always '1'">state</warning>) {
- case 1: break;
- }
- }
-
- private void test2(int state) {
- switch (state) {
- case ONE:
- case TWO:
- if (state == TWO) {
- System.out.println("hello");
- }
- }
- }
-
- public static final int ONE = 1;
- public static final int TWO = 2;
-
-} \ No newline at end of file
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_after.java b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_after.java
index 0d187397afc9..395fd486c388 100644
--- a/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_after.java
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/ReportConstantReferences_after.java
@@ -1,13 +1,15 @@
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
class Test {
- private void test2(@NotNull Object bar) {
+ private void test2(@NotNull Object bar) {}
+
+ void println(@Nullable Object o) {}
- }
private Object test(Object foo, Object bar) {
if (foo == null) {
- System.out.println(<caret>null);
- System.out.println(foo);
+ println(<caret>null);
+ println(foo);
return foo;
}
if (bar == null) {
diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/UseInferredContracts.java b/java/java-tests/testData/inspection/dataFlow/fixture/UseInferredContracts.java
new file mode 100644
index 000000000000..34b9d708a4b2
--- /dev/null
+++ b/java/java-tests/testData/inspection/dataFlow/fixture/UseInferredContracts.java
@@ -0,0 +1,15 @@
+import org.jetbrains.annotations.Nullable;
+
+class Doo {
+
+ static boolean isNotNull(@Nullable Object o) {
+ return o != null;
+ }
+
+ void foo(@Nullable String s) {
+ if (isNotNull(s)) {
+ System.out.println(s.length());
+ }
+ }
+
+}