PsiAssignmentExpression (@NotNull PsiDeclarationStatement declaration, @NotNull Project project) { if (declaration.getDeclaredElements().length == 1) { PsiLocalVariable var = (PsiLocalVariable)declaration.getDeclaredElements()[0]; var.normalizeDeclaration(); final PsiTypeElement typeElement = var.getTypeElement(); if (typeElement.isInferredType()) { PsiTypesUtil.replaceWithExplicitType(typeElement); } final String name = var.getName(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiExpressionStatement statement = (PsiExpressionStatement)factory.createStatementFromText(name + "=xxx;", declaration); statement = (PsiExpressionStatement)CodeStyleManager.getInstance(project).reformat(statement); PsiAssignmentExpression assignment = (PsiAssignmentExpression)statement.getExpression(); PsiExpression initializer = var.getInitializer(); assert initializer != null; PsiExpression rExpression = convertInitializerToExpression(initializer, factory, var.getType()); final PsiExpression expression = assignment.getRExpression(); assert expression != null; expression.replace(rExpression); PsiElement block = declaration.getParent(); if (block instanceof PsiForStatement) { final PsiDeclarationStatement varDeclStatement = factory.createVariableDeclarationStatement(name, var.getType(), null); // For index can't be final, right? for (PsiElement varDecl : varDeclStatement.getDeclaredElements()) { if (varDecl instanceof PsiModifierListOwner) { final PsiModifierList modList = ((PsiModifierListOwner)varDecl).getModifierList(); assert modList != null; modList.setModifierProperty(PsiModifier.FINAL, false); } } final PsiElement parent = block.getParent(); PsiExpressionStatement replaced = (PsiExpressionStatement)new CommentTracker().replaceAndRestoreComments(declaration, statement); if (!(parent instanceof PsiCodeBlock)) { final PsiBlockStatement blockStatement = (PsiBlockStatement)JavaPsiFacade.getElementFactory(project).createStatementFromText("{}", block); final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); codeBlock.add(varDeclStatement); codeBlock.add(block); block.replace(blockStatement); } else { parent.addBefore(varDeclStatement, block); } return (PsiAssignmentExpression)replaced.getExpression(); } else { try { PsiElement declaredElement = declaration.getDeclaredElements()[0]; if (!PsiUtil.isJavaToken(declaredElement.getLastChild(), JavaTokenType.SEMICOLON)) { TreeElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, declaration.getManager()); CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(declaration.addAfter(semicolon.getPsi(), declaredElement)); } return (PsiAssignmentExpression)((PsiExpressionStatement)block.addAfter(statement, declaration)).getExpression(); } finally { initializer = ((PsiLocalVariable)declaration.getDeclaredElements()[0]).getInitializer(); if (initializer != null) { initializer.delete(); } } } } else { ((PsiLocalVariable)declaration.getDeclaredElements()[0]).normalizeDeclaration(); } return null; }