private static PsiElement fixCatchBlock(GrTryCatchStatement tryCatch, PsiClassType[] exceptions) {
    if (exceptions.length == 0) return tryCatch;
    final GroovyPsiElementFactory factory =
        GroovyPsiElementFactory.getInstance(tryCatch.getProject());

    final GrCatchClause[] clauses = tryCatch.getCatchClauses();
    List<String> restricted =
        ContainerUtil.map(
            clauses,
            new Function<GrCatchClause, String>() {
              @Override
              @Nullable
              public String fun(GrCatchClause grCatchClause) {
                final GrParameter grParameter = grCatchClause.getParameter();
                return grParameter != null ? grParameter.getName() : null;
              }
            });

    restricted = ContainerUtil.skipNulls(restricted);
    final DefaultGroovyVariableNameValidator nameValidator =
        new DefaultGroovyVariableNameValidator(tryCatch, restricted);

    GrCatchClause anchor = clauses.length == 0 ? null : clauses[clauses.length - 1];
    for (PsiClassType type : exceptions) {
      final String[] names =
          GroovyNameSuggestionUtil.suggestVariableNameByType(type, nameValidator);
      final GrCatchClause catchClause = factory.createCatchClause(type, names[0]);
      final GrStatement printStackTrace =
          factory.createStatementFromText(names[0] + ".printStackTrace()");
      catchClause.getBody().addStatementBefore(printStackTrace, null);
      anchor = tryCatch.addCatchClause(catchClause, anchor);
      JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
    }
    return tryCatch;
  }
 private static PsiElement generateTryCatch(PsiElement element, PsiClassType[] exceptions) {
   if (exceptions.length == 0) return element;
   GrTryCatchStatement tryCatch =
       (GrTryCatchStatement)
           GroovyPsiElementFactory.getInstance(element.getProject())
               .createStatementFromText("try{} catch (Exception e){}");
   final GrStatement statement = PsiTreeUtil.getParentOfType(element, GrStatement.class);
   assert statement != null;
   tryCatch.getTryBlock().addStatementBefore(statement, null);
   tryCatch = (GrTryCatchStatement) statement.replace(tryCatch);
   tryCatch.getCatchClauses()[0].delete();
   fixCatchBlock(tryCatch, exceptions);
   return tryCatch;
 }