mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-dfa] FieldChecker: correctly find parent method if we are inside static analyzer
DfaPsiUtil.getBlockNotNullFields may run DFA for static initializer block. If it happens to be a local/anonymous class inside a method, that outer method was incorrectly used as a context. This caused wrong state of FieldChecker and might eventually lead to StackOverflowError. GitOrigin-RevId: a96b4527cb03d77160be3579935e47d79bfca48c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
2f5354d87e
commit
a813ef19b5
+2
-1
@@ -8,6 +8,7 @@ import com.intellij.psi.util.CachedValuesManager;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -19,7 +20,7 @@ public class FieldChecker {
|
||||
private final PsiClass myClass;
|
||||
|
||||
private FieldChecker(PsiElement context) {
|
||||
PsiMethod method = context instanceof PsiClass ? null : PsiTreeUtil.getParentOfType(context, PsiMethod.class);
|
||||
PsiMethod method = ObjectUtils.tryCast(PsiTreeUtil.getNonStrictParentOfType(context, PsiMember.class), PsiMethod.class);
|
||||
PsiClass contextClass = method != null ? method.getContainingClass() : context instanceof PsiClass ? (PsiClass)context : null;
|
||||
myClass = contextClass;
|
||||
if (method == null || myClass == null) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
public class StaticFieldInAnonymous {
|
||||
void test() {
|
||||
Runnable r = new Runnable() {
|
||||
private static final String s;
|
||||
|
||||
static {
|
||||
s = "foo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (<warning descr="Condition 's == null' is always 'false'">s == null</warning>) {}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -38,4 +38,5 @@ public class DataFlowInspection16Test extends DataFlowInspectionTestCase {
|
||||
"public @interface NonnullByDefault {}");
|
||||
doTest();
|
||||
}
|
||||
public void testStaticFieldInAnonymous() { doTest(); }
|
||||
}
|
||||
Reference in New Issue
Block a user