void () { // to use weak refs myPsiManager.runInBatchFilesMode(() -> { final PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject())); assertNotNull(aClass); VirtualFile vFile = myRoot.findChild("AClass.java"); assertNotNull(vFile); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject); Document document = FileDocumentManager.getInstance().getDocument(vFile); final SmartPsiElementPointer pointer = createPointer(aClass); System.gc(); /* PsiFile psiFile = myPsiManager.getFileManager().getCachedPsiFile(vFile); assertNull(psiFile); */ insertString(document, 0, "class Foo{}\n"); PsiElement element = pointer.getElement(); assertEquals(aClass, element); insertString(document, 0, "/**/"); psiDocumentManager.commitAllDocuments(); if (aClass.isValid()) { aClass.getChildren(); } element = pointer.getElement(); assertNotNull(element); assertTrue(element instanceof PsiClass); assertTrue(element.isValid()); return null; }); }