PsiDeclarationStatement (@NotNull PsiVariable variable, @Nullable PsiExpression initializer, CommentTracker tracker) { final Project project = variable.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory factory = psiFacade.getElementFactory(); String name = variable.getName(); if (name == null) { name = ""; } final PsiType type = variable.getType(); final PsiDeclarationStatement newDeclaration = factory.createVariableDeclarationStatement(name, type, tracker.markUnchanged(initializer), variable); final PsiLocalVariable newVariable = (PsiLocalVariable)newDeclaration.getDeclaredElements()[0]; final PsiModifierList newModifierList = newVariable.getModifierList(); final PsiModifierList modifierList = variable.getModifierList(); if (newModifierList != null && modifierList != null) { // remove final when PsiDeclarationFactory adds one by mistake newModifierList.setModifierProperty(PsiModifier.FINAL, variable.hasModifierProperty(PsiModifier.FINAL)); GenerateMembersUtil.copyAnnotations(modifierList, newModifierList); } final PsiTypeElement typeElement = variable.getTypeElement(); if (typeElement != null && typeElement.isInferredType()) { //restore 'var' for local variables newVariable.getTypeElement().replace(factory.createTypeElementFromText("var", variable)); } return newDeclaration; }