private static void processLoop(Project project, PsiExpression iteratedValue, PsiReferenceExpression mapRef, ReplacementMode mode) {
      PsiForeachStatement foreachStatement = tryCast(ParenthesesUtils.getParentSkipParentheses(iteratedValue), PsiForeachStatement.class);
      if (foreachStatement == null) return;

      final PsiMethodCallExpression newIterationValue =
        (PsiMethodCallExpression)new CommentTracker().replaceAndRestoreComments(iteratedValue, mapRef.getText() + "."+mode.myDisplayName);
      mapRef = (PsiReferenceExpression)newIterationValue.getMethodExpression().getQualifierExpression();
      PsiType collectionType = newIterationValue.getType();
      PsiType newParameterType = GenericsUtil
        .getVariableTypeByExpressionType(PsiUtil.substituteTypeParameter(collectionType, CommonClassNames.JAVA_UTIL_COLLECTION, 0, true));
      boolean insertCast = false;
      if (newParameterType == null) {
        newParameterType = TypeUtils.getObjectType(foreachStatement);
        insertCast = mode == ReplacementMode.ENTRY_SET;
      }
      final PsiParameter keyParameter = foreachStatement.getIterationParameter();
      final String keyParameterText = keyParameter.getText();
      final String keyTypeText = keyParameter.getType().getCanonicalText();
      PsiStatement body = Objects.requireNonNull(foreachStatement.getBody());
      List<PsiExpression> accesses = ParameterAccessCollector.collectParameterAccesses(keyParameter, mapRef, body);
      String loopParameterName = null;
      if (mode == ReplacementMode.VALUES && body instanceof PsiBlockStatement) {
        loopParameterName = tryReuseValueVariable(((PsiBlockStatement)body).getCodeBlock(), accesses, newParameterType);
      }
      if (loopParameterName == null) {
        loopParameterName = createNewVariableName(foreachStatement, newParameterType, mode);
      }
      final PsiParameter loopParameter = JavaPsiFacade.getElementFactory(project).createParameter(loopParameterName, newParameterType);
      if (keyParameter.hasModifierProperty(PsiModifier.FINAL)) {
        final PsiModifierList modifierList = loopParameter.getModifierList();
        if (modifierList != null) {
          modifierList.setModifierProperty(PsiModifier.FINAL, true);
        }
      }
      keyParameter.replace(loopParameter);
      String replacement = insertCast ? "(("+ CommonClassNames.JAVA_UTIL_MAP_ENTRY+")" + loopParameterName + ')' : loopParameterName;
      if (body instanceof PsiBlockStatement && 
          accesses.stream().filter(access -> !(access instanceof PsiMethodCallExpression)).count() > 1) {
        PsiCodeBlock codeBlock = ((PsiBlockStatement)body).getCodeBlock();
        PsiJavaToken brace = codeBlock.getLBrace();
        if (brace != null) {
          String keyVariableText = keyParameterText + "=" + replacement + ".getKey();";
          PsiStatement keyDeclaration = JavaPsiFacade.getElementFactory(project).createStatementFromText(keyVariableText, body);
          codeBlock.addAfter(keyDeclaration, brace);
          accesses = ContainerUtil.filterIsInstance(accesses, PsiMethodCallExpression.class);
        }
      }
      replaceParameterAccess(accesses, keyTypeText, replacement, mode);
    }
private static void processLoop(Project project, PsiExpression iteratedValue, PsiReferenceExpression mapRef, ReplacementMode mode)