void () { final PsiClass aClass = myJavaFacade.findClass("AClass",GlobalSearchScope.allScope(getProject())); assertNotNull(aClass); Document document = PsiDocumentManager.getInstance(myProject).getDocument(aClass.getContainingFile()); final SmartPsiElementPointer[] pointer = new SmartPsiElementPointer[1]; int offset = aClass.getTextOffset(); DocumentListener listener = new DocumentListener() { @Override public void beforeDocumentChange(@NotNull DocumentEvent event) { pointer[0] = createPointer(aClass); } }; EditorEventMulticaster multicaster = EditorFactory.getInstance().getEventMulticaster(); multicaster.addDocumentListener(listener, getTestRootDisposable()); try { insertString(document, offset, "/******/"); } finally { multicaster.removeDocumentListener(listener); } pointer[0].getElement(); insertString(document, 0, "/**/"); PsiDocumentManager.getInstance(myProject).commitAllDocuments(); PsiElement element = pointer[0].getElement(); assertNotNull(element); assertTrue(element instanceof PsiClass); assertTrue(element.isValid()); }