void () { @Language("JAVA") String text = "class Foo extends Bar {}"; PsiFile file = configureByText(JavaFileType.INSTANCE, text); PsiJavaCodeReferenceElement ref = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("Bar"), PsiJavaCodeReferenceElement.class, false); SmartPointerEx pointer = createPointer(ref); //noinspection UnusedAssignment ref = null; gcPointerCache(pointer); ref = PsiTreeUtil.findElementOfClassAtOffset(file, file.getText().indexOf("Bar"), PsiJavaCodeReferenceElement.class, false); final PsiJavaCodeReferenceElement finalRef = ref; ApplicationManager.getApplication().runWriteAction(() -> { finalRef.handleElementRename("BarImpl"); }); assertNotNull(pointer.getElement()); }