Files
openide/java/java-tests/testData/inspection/dataFlow/fixture/ArrayStoreProblems.java
Tagir Valeev 538893bcd1 [java-dfa] Do not rely on class qualified name when checking for assignability/convertibility
Qualified name is absent for local classes, so it doesn't work correctly if local classes are inherited.
Also, as we already have PsiClass objects themselves, we can skip unnecessary resolve by qualified name.
Finally, we can go further in isConvertibleFrom and inline isInheritorOrSelf. This allows to deduplicate areElementsEquivalent, which is supposed to be symmetrical, so extra work is avoided.
Fixes IDEA-253169 Inheritors of local class cause false-positive 'ArrayStoreException' warning

GitOrigin-RevId: cbadd6e6e853ae2310427dd977187b61cf1b5d5e
2020-10-19 05:03:29 +00:00

35 lines
1.6 KiB
Java

import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
class ArrayStoreProblems {
void testLocalClass() {
abstract class Foo {}
Foo[] data = new Foo[1];
data[0] = new Foo() {};
}
void test(String[] args, Integer[] args2) {
Object[] arr = args;
arr[0] <warning descr="Storing element of type 'java.lang.Integer' to array of 'java.lang.String' elements will produce 'ArrayStoreException'">=</warning> 123;
arr = args2;
arr[1] = 124;
arr[2] <warning descr="Storing element of type 'java.lang.String' to array of 'java.lang.Integer' elements will produce 'ArrayStoreException'">=</warning> "foo";
arr = args;
arr[3] = "bar";
arr[4] <warning descr="Storing element of type 'java.lang.Integer' to array of 'java.lang.String' elements will produce 'ArrayStoreException'">=</warning> 125;
}
void test2(Object obj) {
Object[] arr = new String[10];
arr[0] = obj;
arr[1] <warning descr="Storing element of type 'java.lang.Object' to array of 'java.lang.String' elements will produce 'ArrayStoreException'">=</warning> new Object();
}
void test3(Number n) {
Object[] arr = new CharSequence[10];
arr[0] = n; // could implement CharSequence
arr[1] = "foo";
arr[2] <warning descr="Storing element of type 'java.lang.Integer' to array of 'java.lang.CharSequence' elements will produce 'ArrayStoreException'">=</warning> 123;
arr[3] <warning descr="Storing element of type 'java.lang.Object' to array of 'java.lang.CharSequence' elements will produce 'ArrayStoreException'">=</warning> new Object();
}
}