void () { PsiElementFactory factory = myJavaFacade.getElementFactory(); PsiStatement statement = factory.createStatementFromText("String s;", null); PsiLocalVariable var = (PsiLocalVariable)((PsiDeclarationStatement)statement).getDeclaredElements()[0]; PsiType type = var.getType(); assertTrue(type.isValid()); Ref<PsiTypeElement> ref = Ref.create(var.getTypeElement()); ref.get().replace(factory.createTypeElement(PsiTypes.intType())); assertFalse(type.isValid()); GCWatcher.fromClearedRef(ref).ensureCollected(); assertFalse(type.isValid()); // shouldn't throw }