TrackingRunner: minor simplifications

GitOrigin-RevId: fd26fd980ec4bff58d51dab255ed5f3ac05a70b6
This commit is contained in:
Tagir Valeev
2019-05-20 14:06:41 +03:00
committed by intellij-monorepo-bot
parent 8133278136
commit 0fdc4afedf
2 changed files with 18 additions and 18 deletions
@@ -106,18 +106,19 @@ public class TrackingRunner extends StandardDataFlowRunner {
return states;
}
public static List<CauseItem> findProblemCause(boolean unknownAreNullables,
boolean ignoreAssertions,
PsiExpression expression,
DfaProblemType type) {
@Nullable
public static CauseItem findProblemCause(boolean unknownAreNullables,
boolean ignoreAssertions,
PsiExpression expression,
DfaProblemType type) {
PsiElement body = DfaUtil.getDataflowContext(expression);
if (body == null) return Collections.emptyList();
if (body == null) return null;
TrackingRunner runner = new TrackingRunner(unknownAreNullables, body, expression);
if (!analyze(ignoreAssertions, expression, body, runner)) return Collections.emptyList();
return ContainerUtil.createMaybeSingletonList(runner.findProblemCause(expression, type));
if (!runner.analyze(ignoreAssertions, expression, body)) return null;
return runner.findProblemCause(expression, type);
}
private static boolean analyze(boolean ignoreAssertions, PsiExpression expression, PsiElement body, TrackingRunner runner) {
private boolean analyze(boolean ignoreAssertions, PsiExpression expression, PsiElement body) {
List<DfaMemoryState> endOfInitializerStates = new ArrayList<>();
StandardInstructionVisitor visitor = new StandardInstructionVisitor(true) {
@Override
@@ -130,7 +131,7 @@ public class TrackingRunner extends StandardDataFlowRunner {
return super.visitEndOfInitializer(instruction, runner, state);
}
};
RunnerResult result = runner.analyzeMethodRecursively(body, visitor, ignoreAssertions);
RunnerResult result = analyzeMethodRecursively(body, visitor, ignoreAssertions);
if (result != RunnerResult.OK) return false;
if (body instanceof PsiClass) {
PsiMethod ctor = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class, PsiLambdaExpression.class);
@@ -141,12 +142,12 @@ public class TrackingRunner extends StandardDataFlowRunner {
PsiMethodCallExpression call = JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(ctor);
if (JavaPsiConstructorUtil.isChainedConstructorCall(call) ||
(call == null && hasImplicitImpureSuperCall((PsiClass)body, ctor))) {
initialStates = Collections.singletonList(runner.createMemoryState());
initialStates = Collections.singletonList(createMemoryState());
}
else {
initialStates = StreamEx.of(endOfInitializerStates).map(DfaMemoryState::createCopy).toList();
}
return runner.analyzeBlockRecursively(ctorBody, initialStates, visitor, false) == RunnerResult.OK;
return analyzeBlockRecursively(ctorBody, initialStates, visitor, false) == RunnerResult.OK;
}
}
}
@@ -69,27 +69,26 @@ public class FindDfaProblemCauseFix implements LocalQuickFix, LowPriorityAction
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
ThrowableComputable<List<TrackingRunner.CauseItem>, RuntimeException> causeFinder = () -> {
ThrowableComputable<TrackingRunner.CauseItem, RuntimeException> causeFinder = () -> {
PsiExpression element = myAnchor.getElement();
if (element == null) return Collections.emptyList();
if (element == null) return null;
return TrackingRunner.findProblemCause(myUnknownMembersAsNullable, myIgnoreAssertStatements, element, myProblemType);
};
List<TrackingRunner.CauseItem> items = ProgressManager.getInstance().runProcessWithProgressSynchronously(
TrackingRunner.CauseItem item = ProgressManager.getInstance().runProcessWithProgressSynchronously(
() -> ReadAction.compute(causeFinder), "Finding Cause", true, project);
PsiFile file = myAnchor.getContainingFile();
if (!items.isEmpty() && file != null) {
displayProblemCause(file, items);
if (item != null && file != null) {
displayProblemCause(file, item);
}
}
private static void displayProblemCause(PsiFile file, List<TrackingRunner.CauseItem> items) {
private static void displayProblemCause(PsiFile file, TrackingRunner.CauseItem root) {
Project project = file.getProject();
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor == null) return;
Document document = editor.getDocument();
PsiFile topLevelFile = InjectedLanguageManager.getInstance(project).getTopLevelFile(file);
if (topLevelFile == null || document != topLevelFile.getViewProvider().getDocument()) return;
TrackingRunner.CauseItem root = ContainerUtil.getOnlyItem(items);
class CauseWithDepth {
final int myDepth;
final TrackingRunner.CauseItem myCauseItem;