void (PsiModifierListOwner original, PsiModifierListOwner query, PsiModifierListOwner replacement) { final PsiModifierList originalModifierList = original.getModifierList(); final PsiModifierList queryModifierList = query.getModifierList(); final PsiModifierList replacementModifierList = replacement.getModifierList(); if (originalModifierList == null || queryModifierList == null || replacementModifierList == null) { return; } if (queryModifierList.getTextLength() == 0 && replacementModifierList.getTextLength() == 0) { replacementModifierList.replace(originalModifierList); return; } final List<? extends PsiElement> unmatchedAnnotations = originalModifierList.getUserData(GlobalMatchingVisitor.UNMATCHED_ELEMENTS_KEY); final PsiElement anchor = replacementModifierList.getFirstChild(); boolean append = anchor == null; PsiElement child = originalModifierList.getFirstChild(); while (child != null) { if (child instanceof PsiKeyword) { append = true; @PsiModifier.ModifierConstant final String modifierText = child.getText(); if (isCompatibleModifier(modifierText, replacementModifierList) && !queryModifierList.hasExplicitModifier(modifierText)) { if (anchor != null) replacementModifierList.add(whiteSpace(child.getPrevSibling(), " ")); replacementModifierList.add(child); } } else if (child instanceof PsiAnnotation && (unmatchedAnnotations == null || unmatchedAnnotations.contains(child))) { if (append) { if (anchor != null) replacementModifierList.add(whiteSpace(child.getPrevSibling(), " ")); replacementModifierList.add(child); } else { final PsiElement next = replacementModifierList.addBefore(child, anchor).getNextSibling(); final PsiWhiteSpace whiteSpace = whiteSpace(child.getNextSibling(), " "); if (!(next instanceof PsiWhiteSpace)) { replacementModifierList.addBefore(whiteSpace, anchor); } else { next.replace(whiteSpace); } } } child = child.getNextSibling(); } }