void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element.getParent() instanceof PsiLocalVariable variable)) return; PsiTypeCastExpression cast = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(variable.getInitializer()), PsiTypeCastExpression.class); if (cast == null) return; PsiTypeElement typeElement = cast.getCastType(); if (typeElement == null) return; PsiInstanceOfExpression instanceOf = PsiTreeUtil.findSameElementInCopy(myInstanceOfPointer.getElement(), element.getContainingFile()); if (instanceOf == null) return; CommentTracker ct = new CommentTracker(); StringBuilder text = new StringBuilder(ct.text(instanceOf.getOperand())); text.append(" instanceof "); PsiModifierList modifierList = variable.getModifierList(); JavaCodeStyleSettings codeStyleSettings = JavaCodeStyleSettings.getInstance(variable.getContainingFile()); if (modifierList != null && modifierList.getTextLength() > 0) { modifierList.setModifierProperty(PsiModifier.FINAL, codeStyleSettings.GENERATE_FINAL_LOCALS); text.append(ct.text(modifierList)).append(' '); } else if (codeStyleSettings.GENERATE_FINAL_LOCALS) { text.append("final "); } text.append(typeElement.getText()).append(' '); if (instanceOf.getPattern() instanceof PsiDeconstructionPattern) { return; } text.append(variable.getName()); PsiElement replaced = ct.replace(instanceOf, text.toString()); ct.deleteAndRestoreComments(variable); if (!(replaced instanceof PsiInstanceOfExpression instanceOfExpression)) { return; } PsiPrimaryPattern pattern = instanceOfExpression.getPattern(); if (!(pattern instanceof PsiTypeTestPattern typeTestPattern)) { return; } PsiPatternVariable patternVariable = typeTestPattern.getPatternVariable(); PsiElement scope = JavaSharedImplUtil.getPatternVariableDeclarationScope(instanceOfExpression); boolean nameDeclaredInside = InstanceOfUtils.isConflictingNameDeclaredInside(patternVariable, scope, true); if (!nameDeclaredInside) { return; } final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); List<String> names = new VariableNameGenerator(instanceOfExpression, VariableKind.LOCAL_VARIABLE) .byType(patternVariable.getType()).byName( codeStyleManager.suggestUniqueVariableName(patternVariable.getName(), instanceOfExpression, true)).generateAll(true); updater.rename(patternVariable, names); }