void (@NotNull PsiReferenceExpression ref, @NotNull String newName) { PsiElement nameElement = ref.getReferenceNameElement(); if(nameElement == null) { throw new IllegalStateException("Name element is null: "+ref); } if(newName.equals(nameElement.getText())) return; PsiClass aClass = null; if(ref.getQualifierExpression() == null) { PsiMember member = tryCast(ref.resolve(), PsiMember.class); if (member != null && ImportUtils.isStaticallyImported(member, ref)) { aClass = member.getContainingClass(); } } PsiElementFactory factory = JavaPsiFacade.getElementFactory(ref.getProject()); PsiIdentifier identifier = factory.createIdentifier(newName); nameElement.replace(identifier); if(aClass != null) { PsiMember member = tryCast(ref.resolve(), PsiMember.class); if (member == null || member.getContainingClass() != aClass) { ref.setQualifierExpression(factory.createReferenceExpression(aClass)); } } }