record (boolean isBefore, @NotNull PsiLocalVariable variable, @NotNull String name) { @Nullable static DeclarationInfo findExistingAnonymousClass(@NotNull PsiVariable variable) { PsiElement varDeclarationStatement = CommonJavaRefactoringUtil.getParentStatement(variable, false); if (varDeclarationStatement == null) return null; PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(varDeclarationStatement, PsiStatement.class); PsiDeclarationStatement nextDeclarationStatement = tryCast(nextStatement, PsiDeclarationStatement.class); DeclarationInfo nextDeclaration = findExistingAnonymousClass(variable, nextDeclarationStatement, true); if (nextDeclaration != null) return nextDeclaration; PsiDeclarationStatement previousDeclarationStatement = PsiTreeUtil.getPrevSiblingOfType(varDeclarationStatement, PsiDeclarationStatement.class); return findExistingAnonymousClass(variable, previousDeclarationStatement, false); } void replace(@NotNull String variableText) { PsiNewExpression newExpression = (PsiNewExpression)variable.getInitializer(); assert newExpression != null; PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass(); assert anonymousClass != null; PsiElement lBrace = anonymousClass.getLBrace(); PsiElement rBrace = anonymousClass.getRBrace(); if (lBrace == null || rBrace == null) return; StringBuilder expressionText = new StringBuilder(); for (PsiElement child = newExpression.getFirstChild(); child != null; child = child.getNextSibling()) { if (child == anonymousClass) break; expressionText.append(child.getText()); } for (PsiElement child = anonymousClass.getFirstChild(); child != null; child = child.getNextSibling()) { if (!isBefore && child == rBrace) expressionText.append(variableText); expressionText.append(child.getText()); if (isBefore && child == lBrace) expressionText.append(variableText); } PsiElementFactory factory = JavaPsiFacade.getElementFactory(variable.getProject()); variable.setInitializer(factory.createExpressionFromText(expressionText.toString(), variable)); PsiTypeElement typeElement = variable.getTypeElement(); if (!typeElement.isInferredType()) { typeElement.replace(factory.createTypeElementFromText("var", variable)); } } @Nullable private static DeclarationInfo findExistingAnonymousClass(@NotNull PsiVariable variable, @Nullable PsiDeclarationStatement declarationStatement, boolean isBefore) { if (declarationStatement == null) return null; PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); if (declaredElements.length != 1) return null; PsiLocalVariable localVariable = tryCast(declaredElements[0], PsiLocalVariable.class); if (localVariable == null) return null; String boxName = localVariable.getName(); PsiNewExpression newExpression = tryCast(localVariable.getInitializer(), PsiNewExpression.class); if (newExpression == null) return null; PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass(); if (anonymousClass == null) return null; String variableName = variable.getName(); if (variableName == null) return null; if (!TypeUtils.isJavaLangObject(anonymousClass.getBaseClassType())) return null; if (Arrays.stream(anonymousClass.getFields()) .map(field -> field.getName()) .anyMatch(name -> name.equals(variableName))) { return null; } return new DeclarationInfo(isBefore, localVariable, boxName); } }