void () { //be sure not to load tree PsiManagerEx.getInstanceEx(getProject()).setAssertOnFileLoadingFilter(VirtualFileFilter.ALL, getTestRootDisposable()); PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.allScope(myProject)); assertNotNull(aClass); final PsiAnnotation[] annotations = aClass.getModifierList().getAnnotations(); assertEquals(1, annotations.length); assertEquals("A", annotations[0].getNameReferenceElement().getReferenceName()); final PsiAnnotation newAnnotation = myJavaFacade.getElementFactory().createAnnotationFromText("@B", null); //here the tree is going to be loaded PsiManagerEx.getInstanceEx(getProject()).setAssertOnFileLoadingFilter(VirtualFileFilter.NONE, getTestRootDisposable()); CommandProcessor.getInstance().executeCommand(myProject, () -> WriteCommandAction.runWriteCommandAction(null, () -> { try { annotations[0].replace(newAnnotation); } catch (IncorrectOperationException e) { LOG.error(e); } }), null, null); assertEquals("@B", aClass.getModifierList().getText()); }