void (final PsiElement body, final Set<? super PsiField> candidates, Set<? super PsiField> usedFields, boolean ignoreFieldsUsedInMultipleMethods, Set<? super PsiField> ignored) { try { final ControlFlow controlFlow = ControlFlowFactory .getControlFlow(body, AllVariablesControlFlowPolicy.getInstance(), ControlFlowOptions.NO_CONST_EVALUATE); final List<PsiField> usedVars = ContainerUtil.filterIsInstance( ControlFlowUtil.getUsedVariables(controlFlow, 0, controlFlow.getSize()), PsiField.class); if (usedVars.isEmpty()) return; final Collection<PsiVariable> writtenVariables = ControlFlowUtil.getWrittenVariables(controlFlow, 0, controlFlow.getSize(), false); for (PsiField usedVariable : usedVars) { if (!writtenVariables.contains(usedVariable)) { ignored.add(usedVariable); } if (!usedFields.add(usedVariable) && (ignoreFieldsUsedInMultipleMethods || ignored.contains(usedVariable))) { candidates.remove(usedVariable); //used in more than one code block } } if (candidates.isEmpty()) return; final List<PsiReferenceExpression> readBeforeWrites = ControlFlowUtil.getReadBeforeWrite(controlFlow); for (final PsiReferenceExpression readBeforeWrite : readBeforeWrites) { final PsiElement resolved = readBeforeWrite.resolve(); if (resolved instanceof PsiField field && (!isImmutableState(field.getType()) || !PsiUtil.isConstantExpression(field.getInitializer()) || writtenVariables.contains(field))) { PsiElement parent = body.getParent(); if (parent instanceof PsiMethod method && method.isConstructor() && field.getInitializer() != null && !field.hasModifierProperty(PsiModifier.STATIC) && PsiTreeUtil.isAncestor(method.getContainingClass(), field, true)) { continue; } candidates.remove(field); } } } catch (AnalysisCanceledException e) { candidates.clear(); } }