@Override
  public void onReferencesBuild(RefElement refElement) {
    mark(refElement);
    if (refElement instanceof RefClass) {
      final PsiClass psiClass = ObjectUtils.tryCast(refElement.getPsiElement(), PsiClass.class);
      if (psiClass != null) {
        PsiField[] psiFields = psiClass.getFields();
        Set<PsiVariable> allFields = new HashSet<>();
        ContainerUtil.addAll(allFields, psiFields);
        List<PsiVariable> instanceInitializerInitializedFields = new ArrayList<>();
        Set<PsiField> fieldsInitializedInInitializers = null;

        for (PsiClassInitializer initializer : psiClass.getInitializers()) {
          PsiCodeBlock body = initializer.getBody();
          ControlFlow flow;
          try {
            flow = ControlFlowFactory.getInstance(body.getProject())
              .getControlFlow(body, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), false);
          }
          catch (AnalysisCanceledException e) {
            flow = ControlFlow.EMPTY;
          }
          Collection<PsiVariable> writtenVariables = new ArrayList<>();
          ControlFlowUtil.getWrittenVariables(flow, 0, flow.getSize(), false, writtenVariables);
          if (fieldsInitializedInInitializers == null) {
            fieldsInitializedInInitializers = new HashSet<>();
          }
          for (PsiVariable psiVariable : writtenVariables) {
            if (allFields.contains(psiVariable) && ControlFlowUtil.isVariableDefinitelyAssigned(psiVariable, flow)) {
              if (instanceInitializerInitializedFields.contains(psiVariable)) {
                allFields.remove(psiVariable);
                instanceInitializerInitializedFields.remove(psiVariable);
              }
              else {
                instanceInitializerInitializedFields.add(psiVariable);
              }
              fieldsInitializedInInitializers.add((PsiField)psiVariable);
            }
          }
          for (PsiVariable psiVariable : writtenVariables) {
            if (!instanceInitializerInitializedFields.contains(psiVariable)) {
              allFields.remove(psiVariable);
            }
          }
        }

        for (PsiMethod constructor : psiClass.getConstructors()) {
          PsiCodeBlock body = constructor.getBody();
          if (body != null) {
            ControlFlow flow;
            try {
              flow = ControlFlowFactory.getInstance(body.getProject())
                .getControlFlow(body, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(), false);
            }
            catch (AnalysisCanceledException e) {
              flow = ControlFlow.EMPTY;
            }

            Collection<PsiVariable> writtenVariables = ControlFlowUtil.getWrittenVariables(flow, 0, flow.getSize(), false);
            for (PsiVariable psiVariable : writtenVariables) {
              if (instanceInitializerInitializedFields.contains(psiVariable)) {
                allFields.remove(psiVariable);
                instanceInitializerInitializedFields.remove(psiVariable);
              }
            }
            List<PsiMethod> redirectedConstructors = JavaHighlightUtil.getChainedConstructors(constructor);
            if (redirectedConstructors.isEmpty()) {
              List<PsiVariable> ssaVariables = ControlFlowUtil.getSSAVariables(flow);
              ArrayList<PsiVariable> good = new ArrayList<>(ssaVariables);
              good.addAll(instanceInitializerInitializedFields);
              allFields.retainAll(good);
            }
            else {
              allFields.removeAll(writtenVariables);
            }
          }
        }

        for (PsiField psiField : psiFields) {
          if ((fieldsInitializedInInitializers != null && !fieldsInitializedInInitializers.contains(psiField) ||
               !allFields.contains(psiField)) && psiField.getInitializer() == null) {
            final RefFieldImpl refField = (RefFieldImpl)myManager.getReference(psiField);
            if (refField != null) {
              refField.initializeIfNeeded();
              refField.setFlag(false, CAN_BE_FINAL_MASK);
            }
          }
        }
      }
    }
    else if (refElement instanceof RefMethod refMethod) {
      if (refMethod.isEntry()) {
        ((RefMethodImpl)refMethod).setFlag(false, CAN_BE_FINAL_MASK);
      }
    }
  }
@Override
  public void onReferencesBuild(RefElement refElement)