Isn't it better:
while(folder instanceof IFolder && !folder.exists()) {
        folders.add((IFolder)folder);
        folder = folder.getParent();
}