private static List<PsiStatement> getFinallyStatements(Project project, @Nullable SourcePosition position) {
    if (position == null) {
      return Collections.emptyList();
    }
    List<PsiStatement> res = new ArrayList<>();
    PsiElement element = position.getFile().findElementAt(position.getOffset());
    PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
    while (tryStatement != null) {
      PsiResourceList resourceList = tryStatement.getResourceList();
      if (resourceList != null) {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
        for (PsiResourceListElement listElement : resourceList) {
          String varName = getResourceName(listElement);
          if (varName != null) {
            res.add(factory.createStatementFromText("if (" + varName + " != null) " + varName + ".close();", tryStatement));
          }
        }
      }
      PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
      if (finallyBlock != null) {
        ContainerUtil.addAll(res, finallyBlock.getStatements());
      }
      tryStatement = PsiTreeUtil.getParentOfType(tryStatement, PsiTryStatement.class);
    }
    return res;
  }
private static List<PsiStatement> getFinallyStatements(Project project, @Nullable SourcePosition position)