void (@NotNull PsiVariable var, @NotNull String toType) { Project project = var.getProject(); if (var instanceof PsiField || JavaCodeStyleSettings.getInstance(var.getContainingFile()).GENERATE_FINAL_LOCALS) { PsiModifierList modifierList = Objects.requireNonNull(var.getModifierList()); WriteAction.run(() -> { if (var.getInitializer() == null) { PsiExpression newInitializer = JavaPsiFacade.getElementFactory(project).createExpressionFromText("new " + toType + "()", var); var.setInitializer(newInitializer); } modifierList.setModifierProperty(PsiModifier.FINAL, true); modifierList.setModifierProperty(PsiModifier.VOLATILE, false); JavaCodeStyleManager.getInstance(project).shortenClassReferences(var); CodeStyleManager.getInstance(project).reformat(var); }); } }