PsiElement[] (PsiElement block, PsiElement[] statements, boolean generateInitializers) { try{ PsiManager psiManager = block.getManager(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(psiManager.getProject()); ArrayList<PsiElement> array = new ArrayList<>(); for (PsiElement statement : statements) { if (statement instanceof PsiDeclarationStatement declaration && needToDeclareOut(statements, declaration)) { PsiElement[] elements = declaration.getDeclaredElements(); for (PsiElement element : elements) { PsiVariable var = (PsiVariable)element; PsiExpression initializer = var.getInitializer(); if (initializer != null) { String name = var.getName(); PsiExpressionStatement assignment = (PsiExpressionStatement)factory.createStatementFromText(name + "=x;", null); assignment = (PsiExpressionStatement)CodeStyleManager.getInstance(psiManager.getProject()).reformat(assignment); PsiAssignmentExpression expr = (PsiAssignmentExpression)assignment.getExpression(); expr.getRExpression().replace(CommonJavaRefactoringUtil.convertInitializerToNormalExpression(initializer, var.getType())); assignment = (PsiExpressionStatement)block.addAfter(assignment, declaration); array.add(assignment); } } PsiDeclarationStatement newDeclaration; if (!array.isEmpty()) { PsiElement firstStatement = array.get(0); newDeclaration = (PsiDeclarationStatement)block.addBefore(declaration, firstStatement); declaration.delete(); } else { newDeclaration = declaration; } elements = newDeclaration.getDeclaredElements(); for (PsiElement element1 : elements) { PsiVariable var = (PsiVariable)element1; PsiExpression initializer = var.getInitializer(); if (initializer != null) { PsiTypeElement typeElement = var.getTypeElement(); if (typeElement != null && typeElement.isInferredType() && PsiTypesUtil.replaceWithExplicitType(typeElement) == null) { continue; } if (!generateInitializers || var.hasModifierProperty(PsiModifier.FINAL)) { initializer.delete(); } else { String defaultValue = PsiTypesUtil.getDefaultValueOfType(var.getType()); PsiExpression expr = factory.createExpressionFromText(defaultValue, null); initializer.replace(expr); } } } continue; } array.add(statement); } return PsiUtilCore.toPsiElementArray(array); } catch(IncorrectOperationException e){ LOG.error(e); return statements; } }