ModCommand (@NotNull ActionContext context) { PsiElement leaf = context.findLeaf(); final PsiClass targetClass = Objects.requireNonNull(getTargetClass(leaf)); return ModCommand.psiUpdate(context, updater -> { final Map<PsiJavaFile, Map<PsiField, Set<PsiElement>>> refs = new HashMap<>(); for (PsiField field : targetClass.getAllFields()) { final PsiClass containingClass = field.getContainingClass(); for (PsiReference reference : ReferencesSearch.search(field)) { if (reference == null) continue; final PsiElement refElement = updater.getWritable(reference.getElement()); if (encodeQualifier(containingClass, refElement, targetClass)) continue; PsiFile psiFile = refElement.getContainingFile(); if (psiFile instanceof PsiJavaFile javaFile) { Map<PsiField, Set<PsiElement>> references = refs.computeIfAbsent(javaFile, k -> new HashMap<>()); Set<PsiElement> fieldsRefs = references.computeIfAbsent(field, k -> new HashSet<>()); fieldsRefs.add(refElement); } } } final Set<PsiJavaCodeReferenceElement> refs2Unimplement = new HashSet<>(); for (PsiClass psiClass : DirectClassInheritorsSearch.search(targetClass)) { PsiFile containingFile = psiClass.getContainingFile(); if (!(containingFile instanceof PsiJavaFile javaFile)) continue; refs.computeIfAbsent(updater.getWritable(javaFile), k -> new HashMap<>()); if (collectExtendsImplements(targetClass, psiClass.getExtendsList(), refs2Unimplement)) continue; collectExtendsImplements(targetClass, psiClass.getImplementsList(), refs2Unimplement); } List<PsiJavaCodeReferenceElement> writableRefsToUnimplement = ContainerUtil.map(refs2Unimplement, updater::getWritable); for (PsiJavaFile psiFile : refs.keySet()) { final Map<PsiField, Set<PsiElement>> map = refs.get(psiFile); for (PsiField psiField : map.keySet()) { final PsiClass containingClass = psiField.getContainingClass(); final String fieldName = psiField.getName(); for (PsiElement reference : map.get(psiField)) { bindReference(psiFile, psiField, containingClass, fieldName, reference, context.project()); } } } for (PsiJavaCodeReferenceElement referenceElement : writableRefsToUnimplement) { referenceElement.delete(); } final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.project()); for (PsiFile psiFile : refs.keySet()) { if (psiFile instanceof PsiJavaFile javaFile) { PsiImportList oldImports = javaFile.getImportList(); final PsiImportList prepared = codeStyleManager.prepareOptimizeImportsResult(javaFile); if (oldImports != null && prepared != null) { oldImports.replace(prepared); } } } }); }